diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-05-01 17:14:06 -0400 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-05-01 17:14:06 -0400 |
commit | 24695f6c0274cac8ae17df3765e0cc54c5668047 (patch) | |
tree | fdc9e4ecc85a61fabf3cf0619f1be44a4488e721 /src/mbgl | |
parent | dac4b7d3c95f84358bd475e01aae45e2ca52ddc0 (diff) | |
download | qtlocation-mapboxgl-24695f6c0274cac8ae17df3765e0cc54c5668047.tar.gz |
More bandaids to avoid #1309
Even though we only bind the shared_ptr in the after callback, the
worker thread was still copying the callback and therefore gaining
a shared reference. Here we try to eliminate the copies.
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/util/run_loop.hpp | 14 | ||||
-rw-r--r-- | src/mbgl/util/thread.hpp | 8 | ||||
-rw-r--r-- | src/mbgl/util/worker.cpp | 4 | ||||
-rw-r--r-- | src/mbgl/util/worker.hpp | 2 |
4 files changed, 14 insertions, 14 deletions
diff --git a/src/mbgl/util/run_loop.hpp b/src/mbgl/util/run_loop.hpp index 971ad2cac7..450718ebdb 100644 --- a/src/mbgl/util/run_loop.hpp +++ b/src/mbgl/util/run_loop.hpp @@ -29,7 +29,7 @@ public: // Invoke fn() in the runloop thread, then invoke callback(result) in the current thread. template <class Fn, class R> - void invokeWithResult(Fn&& fn, std::function<void (R)> callback) { + void invokeWithResult(Fn&& fn, std::function<void (R)>&& callback) { RunLoop* outer = current.get(); assert(outer); @@ -37,23 +37,23 @@ public: /* With C++14, we could write: - outer->invoke([callback, result = std::move(fn())] () mutable { - callback(std::move(result)); + outer->invoke([cb = std::move(callback), result = std::move(fn())] () mutable { + cb(std::move(result)); }); Instead we're using a workaround with std::bind to obtain move-capturing semantics with C++11: http://stackoverflow.com/a/12744730/52207 */ - outer->invoke(std::bind([callback] (R& result) { - callback(std::move(result)); - }, std::move(fn()))); + outer->invoke(std::bind([] (std::function<void (R)>& cb, R& result) { + cb(std::move(result)); + }, std::move(callback), std::move(fn()))); }); } // Invoke fn() in the runloop thread, then invoke callback() in the current thread. template <class Fn> - void invokeWithResult(Fn&& fn, std::function<void ()> callback) { + void invokeWithResult(Fn&& fn, std::function<void ()>&& callback) { RunLoop* outer = current.get(); assert(outer); diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 415bda38d8..189182464c 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -49,14 +49,14 @@ public: // Invoke object->fn(args...) in the runloop thread, then invoke callback(result) in the current thread. template <typename Fn, class R, class... Args> - void invokeWithResult(Fn fn, std::function<void (R)> callback, Args&&... args) { - loop->invokeWithResult(std::bind(fn, object, args...), callback); + void invokeWithResult(Fn fn, std::function<void (R)>&& callback, Args&&... args) { + loop->invokeWithResult(std::bind(fn, object, args...), std::move(callback)); } // Invoke object->fn(args...) in the runloop thread, then invoke callback() in the current thread. template <typename Fn, class... Args> - void invokeWithResult(Fn fn, std::function<void ()> callback, Args&&... args) { - loop->invokeWithResult(std::bind(fn, object, args...), callback); + void invokeWithResult(Fn fn, std::function<void ()>&& callback, Args&&... args) { + loop->invokeWithResult(std::bind(fn, object, args...), std::move(callback)); } // Invoke object->fn(args...) in the runloop thread, and wait for the result. diff --git a/src/mbgl/util/worker.cpp b/src/mbgl/util/worker.cpp index fcbe6f5df9..1a6eaf4d96 100644 --- a/src/mbgl/util/worker.cpp +++ b/src/mbgl/util/worker.cpp @@ -21,8 +21,8 @@ Worker::Worker(std::size_t count) { Worker::~Worker() = default; -void Worker::send(Fn work, Fn after) { - threads[current]->invokeWithResult(&Worker::Impl::doWork, after, work); +void Worker::send(Fn&& work, Fn&& after) { + threads[current]->invokeWithResult(&Worker::Impl::doWork, std::move(after), std::move(work)); current = (current + 1) % threads.size(); } diff --git a/src/mbgl/util/worker.hpp b/src/mbgl/util/worker.hpp index b71b4fd5b3..d8fbf6df7d 100644 --- a/src/mbgl/util/worker.hpp +++ b/src/mbgl/util/worker.hpp @@ -15,7 +15,7 @@ public: Worker(std::size_t count); ~Worker(); - void send(Fn work, Fn after); + void send(Fn&& work, Fn&& after); private: class Impl; |