Index: ppapi/shared_impl/proxy_lock.h |
diff --git a/ppapi/shared_impl/proxy_lock.h b/ppapi/shared_impl/proxy_lock.h |
index e0e97e4e38f4071ed592a4b673a4578265fadf17..11ff6e9ed92509faf96225f3cac6d3aecfde09cf 100644 |
--- a/ppapi/shared_impl/proxy_lock.h |
+++ b/ppapi/shared_impl/proxy_lock.h |
@@ -132,21 +132,105 @@ ReturnType CallWhileUnlocked(ReturnType (*function)(P1, P2, P3, P4, P5), |
} |
void PPAPI_SHARED_EXPORT CallWhileUnlocked(const base::Closure& closure); |
-// CallWhileLocked locks the ProxyLock and runs the given closure immediately. |
-// The lock is released when CallWhileLocked returns. This function assumes the |
-// lock is not held. This is mostly for use in RunWhileLocked; see below. |
-void PPAPI_SHARED_EXPORT CallWhileLocked(const base::Closure& closure); |
- |
-// RunWhileLocked binds the given closure with CallWhileLocked and returns the |
-// new Closure. This is for cases where you want to run a task, but you want to |
-// ensure that the ProxyLock is acquired for the duration of the task. |
+namespace internal { |
+ |
+template <typename RunType> |
+struct RunWhleLockedHelper; |
+ |
+template <> |
+struct RunWhleLockedHelper<void ()> { |
bbudge
2013/07/20 14:01:24
sp. RunWhileLockedHelper
|
+ typedef base::Callback<void()> CallbackType; |
+ explicit RunWhleLockedHelper(const CallbackType& callback) |
+ : callback_(new CallbackType(callback)) { |
+ } |
+ void CallWhileLocked() { |
+ ProxyAutoLock lock; |
+ { |
+ // Use a scope and local Callback to ensure that the callback is cleared |
+ // before the lock is released, even in the unlikely event that Run() |
+ // throws an exception. |
+ scoped_ptr<CallbackType> temp_callback(callback_.Pass()); |
+ temp_callback->Run(); |
+ } |
+ } |
+ private: |
+ scoped_ptr<CallbackType> callback_; |
+}; |
+template <typename P1> |
+struct RunWhleLockedHelper<void (P1)> { |
+ typedef base::Callback<void(P1)> CallbackType; |
+ explicit RunWhleLockedHelper(const CallbackType& callback) |
+ : callback_(new CallbackType(callback)) { |
+ } |
+ void CallWhileLocked(P1 p1) { |
+ ProxyAutoLock lock; |
+ { |
+ scoped_ptr<CallbackType> temp_callback(callback_.Pass()); |
+ temp_callback->Run(p1); |
+ } |
+ } |
+ private: |
+ scoped_ptr<CallbackType> callback_; |
+}; |
+template <typename P1, typename P2> |
+struct RunWhleLockedHelper<void (P1, P2)> { |
+ typedef base::Callback<void(P1, P2)> CallbackType; |
+ explicit RunWhleLockedHelper(const CallbackType& callback) |
+ : callback_(new CallbackType(callback)) { |
+ } |
+ void CallWhileLocked(P1 p1, P2 p2) { |
+ ProxyAutoLock lock; |
+ { |
+ scoped_ptr<CallbackType> temp_callback(callback_.Pass()); |
+ temp_callback->Run(p1, p2); |
+ } |
+ } |
+ private: |
+ scoped_ptr<CallbackType> callback_; |
+}; |
+template <typename P1, typename P2, typename P3> |
+struct RunWhleLockedHelper<void (P1, P2, P3)> { |
+ typedef base::Callback<void(P1, P2, P3)> CallbackType; |
+ explicit RunWhleLockedHelper(const CallbackType& callback) |
+ : callback_(new CallbackType(callback)) { |
+ } |
+ void CallWhileLocked(P1 p1, P2 p2, P3 p3) { |
+ ProxyAutoLock lock; |
+ { |
+ scoped_ptr<CallbackType> temp_callback(callback_.Pass()); |
+ temp_callback->Run(p1, p2, p3); |
+ } |
+ } |
+ private: |
+ scoped_ptr<CallbackType> callback_; |
+}; |
+ |
+} // namespace internal |
+ |
+// RunWhileLocked wraps the given Callback in a new Callback that, when invoked: |
+// 1) Locks the ProxyLock. |
+// 2) Runs the original Callback (forwarding arguments, if any). |
+// 3) Clears the original Callback (while the lock is held). |
+// 4) Unlocks the ProxyLock. |
+// Note that it's important that the callback is cleared in step (3), in case |
+// clearing the Callback causes a destructor (e.g., for a Resource) to run, |
+// which should hold the ProxyLock to avoid data races. |
+// |
+// This is for cases where you want to run a task or store a Callback, but you |
+// want to ensure that the ProxyLock is acquired for the duration of the task. |
// Example usage: |
// GetMainThreadMessageLoop()->PostDelayedTask( |
// FROM_HERE, |
// RunWhileLocked(base::Bind(&CallbackWrapper, callback, result)), |
// delay_in_ms); |
-inline base::Closure RunWhileLocked(const base::Closure& closure) { |
- return base::Bind(CallWhileLocked, closure); |
+template <class FunctionType> |
+inline base::Callback<FunctionType> |
+RunWhileLocked(const base::Callback<FunctionType>& callback) { |
+ internal::RunWhleLockedHelper<FunctionType>* helper = |
+ new internal::RunWhleLockedHelper<FunctionType>(callback); |
+ return base::Bind( |
+ &internal::RunWhleLockedHelper<FunctionType>::CallWhileLocked, |
+ base::Owned(helper)); |
} |
} // namespace ppapi |