diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-07-21 15:54:26 +0300 |
---|---|---|
committer | Ivo van Dongen <info@ivovandongen.nl> | 2017-08-14 23:10:10 +0300 |
commit | bfac9b6292fdb4fe167a69e652be5751b2e2f4b8 (patch) | |
tree | 9a9713221cff0f6809a0ee69b0e59342e2ec2780 /include/mbgl/util/thread.hpp | |
parent | 034551f1519491110b83f74662af7d4c0c509104 (diff) | |
download | qtlocation-mapboxgl-upstream/ivd-direct-actor-access.tar.gz |
[core] allow safe direct access to actor on single threaded schedulerupstream/ivd-direct-actor-access
Diffstat (limited to 'include/mbgl/util/thread.hpp')
-rw-r--r-- | include/mbgl/util/thread.hpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp index 06254569a9..c0cbc532ce 100644 --- a/include/mbgl/util/thread.hpp +++ b/include/mbgl/util/thread.hpp @@ -131,6 +131,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 { @@ -157,11 +164,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 |