diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-07-21 15:54:26 +0300 |
---|---|---|
committer | Ivo van Dongen <info@ivovandongen.nl> | 2017-09-13 14:37:10 +0300 |
commit | bbb57b7f927abf2552503548b2b8871e5e8176bb (patch) | |
tree | 7f1d3952433af2052b8341d3e7daf6eb721015cd | |
parent | b092c7715d0c578d7a5ed0ae3cece1c328b73058 (diff) | |
download | qtlocation-mapboxgl-bbb57b7f927abf2552503548b2b8871e5e8176bb.tar.gz |
[core] allow safe direct access to actor on single threaded scheduler
-rw-r--r-- | include/mbgl/actor/actor.hpp | 8 | ||||
-rw-r--r-- | include/mbgl/util/thread.hpp | 29 |
2 files changed, 37 insertions, 0 deletions
diff --git a/include/mbgl/actor/actor.hpp b/include/mbgl/actor/actor.hpp index a0df19208e..93de4a948f 100644 --- a/include/mbgl/actor/actor.hpp +++ b/include/mbgl/actor/actor.hpp @@ -45,6 +45,11 @@ namespace mbgl { purpose of the actor model: prohibiting direct concurrent access to shared state. */ + +namespace util { +template <class> class Thread; +} // namespace util + template <class Object> class Actor : public util::noncopyable { public: @@ -91,6 +96,9 @@ public: } private: + template<typename U> + friend class util::Thread; + std::shared_ptr<Mailbox> mailbox; Object object; }; diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp index 2a58e618da..6f6fc13f0a 100644 --- a/include/mbgl/util/thread.hpp +++ b/include/mbgl/util/thread.hpp @@ -125,6 +125,13 @@ public: } private: + template <class U> + friend class BlockingThreadGuard; + + Object& getObject() { + return object->object; + } + MBGL_STORE_THREAD(tid); void schedule(std::weak_ptr<Mailbox> mailbox) override { @@ -151,11 +158,33 @@ private: std::thread thread; std::unique_ptr<Actor<Object>> object; + std::mutex pauseMutex; std::unique_ptr<std::promise<void>> paused; std::unique_ptr<std::promise<void>> resumed; util::RunLoop* loop = nullptr; }; + +template <class Object> +class BlockingThreadGuard { +public: + BlockingThreadGuard(Thread<Object>& thread_) + : thread(thread_) { + thread.pause(); + } + + ~BlockingThreadGuard() { + thread.resume(); + } + + Object& object() { + return thread.getObject(); + } + +private: + Thread<Object>& thread; +}; + } // namespace util } // namespace mbgl |