Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/shared_impl/tracked_callback.h" | 5 #include "ppapi/shared_impl/tracked_callback.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 bool IsMainThread() { | 25 bool IsMainThread() { |
| 26 return PpapiGlobals::Get() | 26 return PpapiGlobals::Get() |
| 27 ->GetMainThreadMessageLoop() | 27 ->GetMainThreadMessageLoop() |
| 28 ->BelongsToCurrentThread(); | 28 ->BelongsToCurrentThread(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task, | 31 int32_t RunCompletionTask(TrackedCallback::CompletionTask completion_task, |
| 32 int32_t result) { | 32 int32_t result) { |
| 33 ProxyLock::AssertAcquired(); | |
| 33 int32_t task_result = completion_task.Run(result); | 34 int32_t task_result = completion_task.Run(result); |
| 34 if (result != PP_ERROR_ABORTED) | 35 if (result != PP_ERROR_ABORTED) |
| 35 result = task_result; | 36 result = task_result; |
| 36 return result; | 37 return result; |
| 37 } | 38 } |
| 38 | 39 |
| 39 } // namespace | 40 } // namespace |
| 40 | 41 |
| 41 // TrackedCallback ------------------------------------------------------------- | 42 // TrackedCallback ------------------------------------------------------------- |
| 42 | 43 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 57 // TODO(dmichael): Add tracking at the instance level, for callbacks that only | 58 // TODO(dmichael): Add tracking at the instance level, for callbacks that only |
| 58 // have an instance (e.g. for MouseLock). | 59 // have an instance (e.g. for MouseLock). |
| 59 if (resource) { | 60 if (resource) { |
| 60 tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance( | 61 tracker_ = PpapiGlobals::Get()->GetCallbackTrackerForInstance( |
| 61 resource->pp_instance()); | 62 resource->pp_instance()); |
| 62 tracker_->Add(make_scoped_refptr(this)); | 63 tracker_->Add(make_scoped_refptr(this)); |
| 63 } | 64 } |
| 64 | 65 |
| 65 base::Lock* proxy_lock = ProxyLock::Get(); | 66 base::Lock* proxy_lock = ProxyLock::Get(); |
| 66 if (proxy_lock) { | 67 if (proxy_lock) { |
| 68 ProxyLock::AssertAcquired(); | |
| 67 // If the proxy_lock is valid, we're running out-of-process, and locking | 69 // If the proxy_lock is valid, we're running out-of-process, and locking |
| 68 // is enabled. | 70 // is enabled. |
| 69 if (is_blocking()) { | 71 if (is_blocking()) { |
| 70 // This is a blocking completion callback, so we will need a condition | 72 // This is a blocking completion callback, so we will need a condition |
| 71 // variable for blocking & signalling the calling thread. | 73 // variable for blocking & signalling the calling thread. |
| 72 operation_completed_condvar_.reset( | 74 operation_completed_condvar_.reset( |
| 73 new base::ConditionVariable(proxy_lock)); | 75 new base::ConditionVariable(proxy_lock)); |
| 74 } else { | 76 } else { |
| 75 // It's a non-blocking callback, so we should have a MessageLoopResource | 77 // It's a non-blocking callback, so we should have a MessageLoopResource |
| 76 // to dispatch to. Note that we don't error check here, though. Later, | 78 // to dispatch to. Note that we don't error check here, though. Later, |
| 77 // EnterResource::SetResult will check to make sure the callback is valid | 79 // EnterResource::SetResult will check to make sure the callback is valid |
| 78 // and take appropriate action. | 80 // and take appropriate action. |
| 79 } | 81 } |
| 80 } | 82 } |
| 81 } | 83 } |
| 82 | 84 |
| 83 TrackedCallback::~TrackedCallback() {} | 85 TrackedCallback::~TrackedCallback() {} |
| 84 | 86 |
| 85 void TrackedCallback::Abort() { Run(PP_ERROR_ABORTED); } | 87 void TrackedCallback::Abort() { |
| 88 Run(PP_ERROR_ABORTED); | |
| 89 } | |
| 86 | 90 |
| 87 void TrackedCallback::PostAbort() { PostRun(PP_ERROR_ABORTED); } | 91 void TrackedCallback::PostAbort() { |
| 92 PostRun(PP_ERROR_ABORTED); | |
| 93 } | |
| 88 | 94 |
| 89 void TrackedCallback::Run(int32_t result) { | 95 void TrackedCallback::Run(int32_t result) { |
| 96 // Retain ourselves, since SignalBlockingCallback and MarkAsCompleted might | |
| 97 // otherwise cause |this| to be deleted. Do this before acquiring lock_ so | |
| 98 // that |this| is definitely valid at the time we release |lock_|. | |
| 99 scoped_refptr<TrackedCallback> thiz(this); | |
| 100 base::AutoLock acquire(lock_); | |
| 90 // Only allow the callback to be run once. Note that this also covers the case | 101 // Only allow the callback to be run once. Note that this also covers the case |
| 91 // where the callback was previously Aborted because its associated Resource | 102 // where the callback was previously Aborted because its associated Resource |
| 92 // went away. The callback may live on for a while because of a reference from | 103 // went away. The callback may live on for a while because of a reference from |
| 93 // a Closure. But when the Closure runs, Run() quietly does nothing, and the | 104 // a Closure. But when the Closure runs, Run() quietly does nothing, and the |
| 94 // callback will go away when all referring Closures go away. | 105 // callback will go away when all referring Closures go away. |
| 95 if (completed()) | 106 if (completed_) |
| 96 return; | 107 return; |
| 97 if (result == PP_ERROR_ABORTED) | 108 if (result == PP_ERROR_ABORTED) |
| 98 aborted_ = true; | 109 aborted_ = true; |
| 99 | 110 |
| 100 // Note that this call of Run() may have been scheduled prior to Abort() or | 111 // Note that this call of Run() may have been scheduled prior to Abort() or |
| 101 // PostAbort() being called. If we have been told to Abort, that always | 112 // PostAbort() being called. If we have been told to Abort, that always |
| 102 // trumps a result that was scheduled before, so we should make sure to pass | 113 // trumps a result that was scheduled before, so we should make sure to pass |
| 103 // PP_ERROR_ABORTED. | 114 // PP_ERROR_ABORTED. |
| 104 if (aborted()) | 115 if (aborted_) |
| 105 result = PP_ERROR_ABORTED; | 116 result = PP_ERROR_ABORTED; |
| 106 | 117 |
| 107 if (is_blocking()) { | 118 if (is_blocking()) { |
| 108 // If the condition variable is invalid, there are two possibilities. One, | 119 // This is a blocking callback; signal the condvar to wake up the thread. |
| 109 // we're running in-process, in which case the call should have come in on | 120 SignalBlockingCallback(result); |
| 110 // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD | |
| 111 // well before this. Otherwise, this callback was not created as a | |
| 112 // blocking callback. Either way, there's some internal error. | |
| 113 if (!operation_completed_condvar_.get()) { | |
| 114 NOTREACHED(); | |
| 115 return; | |
| 116 } | |
| 117 result_for_blocked_callback_ = result; | |
| 118 // Retain ourselves, since MarkAsCompleted will remove us from the | |
| 119 // tracker. Then MarkAsCompleted before waking up the blocked thread, | |
| 120 // which could potentially re-enter. | |
| 121 scoped_refptr<TrackedCallback> thiz(this); | |
| 122 MarkAsCompleted(); | |
| 123 // Wake up the blocked thread. See BlockUntilComplete for where the thread | |
| 124 // Wait()s. | |
| 125 operation_completed_condvar_->Signal(); | |
| 126 } else { | 121 } else { |
| 127 // If there's a target_loop_, and we're not on the right thread, we need to | 122 // If there's a target_loop_, and we're not on the right thread, we need to |
| 128 // post to target_loop_. | 123 // post to target_loop_. |
| 129 if (target_loop_.get() && | 124 if (target_loop_ && |
| 130 target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) { | 125 target_loop_.get() != PpapiGlobals::Get()->GetCurrentMessageLoop()) { |
| 131 PostRun(result); | 126 PostRunWithLock(result); |
| 132 return; | 127 return; |
| 133 } | 128 } |
| 129 // Do this before running the callback in case of reentrancy from running | |
| 130 // the completion callback. | |
| 131 MarkAsCompletedWithLock(); | |
| 134 | 132 |
| 135 // Copy callback fields now, since |MarkAsCompleted()| may delete us. | 133 if (!completion_task_.is_null()) |
| 136 PP_CompletionCallback callback = callback_; | 134 result = RunCompletionTask(completion_task_, result); |
| 137 CompletionTask completion_task = completion_task_; | |
| 138 completion_task_.Reset(); | |
| 139 // Do this before running the callback in case of reentrancy from running | |
| 140 // the completion task. | |
| 141 MarkAsCompleted(); | |
| 142 | 135 |
| 143 if (!completion_task.is_null()) | 136 { |
| 144 result = RunCompletionTask(completion_task, result); | 137 base::AutoUnlock release(lock_); |
| 145 | 138 // Call the callback without lock_ and without the ProxyLock. |
| 146 // TODO(dmichael): Associate a message loop with the callback; if it's not | 139 CallWhileUnlocked(PP_RunCompletionCallback, &callback_, result); |
| 147 // the same as the current thread's loop, then post it to the right loop. | 140 } |
| 148 CallWhileUnlocked(PP_RunCompletionCallback, &callback, result); | |
| 149 } | 141 } |
| 150 } | 142 } |
| 151 | 143 |
| 152 void TrackedCallback::PostRun(int32_t result) { | 144 void TrackedCallback::PostRun(int32_t result) { |
| 153 if (completed()) { | 145 base::AutoLock acquire(lock_); |
| 154 NOTREACHED(); | 146 PostRunWithLock(result); |
| 155 return; | |
| 156 } | |
| 157 if (result == PP_ERROR_ABORTED) | |
| 158 aborted_ = true; | |
| 159 // We might abort when there's already a scheduled callback, but callers | |
| 160 // should never try to PostRun more than once otherwise. | |
| 161 DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_); | |
| 162 | |
| 163 if (is_blocking()) { | |
| 164 // We might not have a MessageLoop to post to, so we must call Run() | |
| 165 // directly. | |
| 166 Run(result); | |
| 167 } else { | |
| 168 base::Closure callback_closure( | |
| 169 RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result))); | |
| 170 if (target_loop_.get()) { | |
| 171 target_loop_->PostClosure(FROM_HERE, callback_closure, 0); | |
| 172 } else { | |
| 173 // We must be running in-process and on the main thread (the Enter | |
| 174 // classes protect against having a null target_loop_ otherwise). | |
| 175 DCHECK(IsMainThread()); | |
| 176 DCHECK(PpapiGlobals::Get()->IsHostGlobals()); | |
| 177 base::MessageLoop::current()->PostTask(FROM_HERE, callback_closure); | |
| 178 } | |
| 179 } | |
| 180 is_scheduled_ = true; | |
| 181 } | 147 } |
| 182 | 148 |
| 183 void TrackedCallback::set_completion_task( | 149 void TrackedCallback::set_completion_task( |
| 184 const CompletionTask& completion_task) { | 150 const CompletionTask& completion_task) { |
| 151 base::AutoLock acquire(lock_); | |
| 185 DCHECK(completion_task_.is_null()); | 152 DCHECK(completion_task_.is_null()); |
| 186 completion_task_ = completion_task; | 153 completion_task_ = completion_task; |
| 187 } | 154 } |
| 188 | 155 |
| 189 // static | 156 // static |
| 190 bool TrackedCallback::IsPending( | 157 bool TrackedCallback::IsPending( |
| 191 const scoped_refptr<TrackedCallback>& callback) { | 158 const scoped_refptr<TrackedCallback>& callback) { |
| 192 if (!callback.get()) | 159 if (!callback) |
| 193 return false; | 160 return false; |
| 194 if (callback->aborted()) | 161 base::AutoLock acquire(callback->lock_); |
| 162 if (callback->aborted_) | |
| 195 return false; | 163 return false; |
| 196 return !callback->completed(); | 164 return !callback->completed_; |
| 197 } | 165 } |
| 198 | 166 |
| 199 // static | 167 // static |
| 200 bool TrackedCallback::IsScheduledToRun( | 168 bool TrackedCallback::IsScheduledToRun( |
| 201 const scoped_refptr<TrackedCallback>& callback) { | 169 const scoped_refptr<TrackedCallback>& callback) { |
| 202 return IsPending(callback) && callback->is_scheduled_; | 170 if (!callback) |
| 171 return false; | |
| 172 base::AutoLock acquire(callback->lock_); | |
| 173 if (callback->aborted_) | |
| 174 return false; | |
| 175 return !callback->completed_ && callback->is_scheduled_; | |
| 203 } | 176 } |
| 204 | 177 |
| 205 int32_t TrackedCallback::BlockUntilComplete() { | 178 int32_t TrackedCallback::BlockUntilComplete() { |
| 206 // Note, we are already holding the proxy lock in all these methods, including | 179 // Note, we are already holding the proxy lock in this method and many others |
| 207 // this one (see ppapi/thunk/enter.cc for where it gets acquired). | 180 // (see ppapi/thunk/enter.cc for where it gets acquired). |
| 181 ProxyLock::AssertAcquired(); | |
| 182 base::AutoLock acquire(lock_); | |
| 208 | 183 |
| 209 // It doesn't make sense to wait on a non-blocking callback. Furthermore, | 184 // It doesn't make sense to wait on a non-blocking callback. Furthermore, |
| 210 // BlockUntilComplete should never be called for in-process plugins, where | 185 // BlockUntilComplete should never be called for in-process plugins, where |
| 211 // blocking callbacks are not supported. | 186 // blocking callbacks are not supported. |
| 212 CHECK(operation_completed_condvar_.get()); | 187 CHECK(is_blocking() && operation_completed_condvar_); |
| 213 if (!is_blocking() || !operation_completed_condvar_.get()) { | 188 |
| 214 NOTREACHED(); | 189 // Protect us from being deleted to ensure operation_completed_condvar_ is |
| 215 return PP_ERROR_FAILED; | 190 // available to wait on when we drop our lock. |
| 191 scoped_refptr<TrackedCallback> thiz(this); | |
| 192 while (!completed_) { | |
| 193 // Unlock our lock temporarily; any thread that tries to signal us will need | |
| 194 // the lock. | |
| 195 lock_.Release(); | |
|
bbudge
2015/03/31 21:18:47
You could use base::AutoUnlock instead of the expl
dmichael (off chromium)
2015/03/31 21:57:27
As discussed over IM, I'll leave this as-is becaus
| |
| 196 operation_completed_condvar_->Wait(); | |
| 197 // Note that the condvar releases the ProxyLock during Wait and re-acquires | |
| 198 // the ProxyLock when it's signaled. We reacquire lock_ immediately after, | |
| 199 // preserving lock order. | |
| 200 ProxyLock::AssertAcquired(); | |
| 201 lock_.Acquire(); | |
| 216 } | 202 } |
| 217 | 203 |
| 218 while (!completed()) | |
| 219 operation_completed_condvar_->Wait(); | |
| 220 | |
| 221 if (!completion_task_.is_null()) { | 204 if (!completion_task_.is_null()) { |
| 222 result_for_blocked_callback_ = | 205 result_for_blocked_callback_ = |
| 223 RunCompletionTask(completion_task_, result_for_blocked_callback_); | 206 RunCompletionTask(completion_task_, result_for_blocked_callback_); |
| 224 completion_task_.Reset(); | 207 completion_task_.Reset(); |
| 225 } | 208 } |
| 226 return result_for_blocked_callback_; | 209 return result_for_blocked_callback_; |
| 227 } | 210 } |
| 228 | 211 |
| 229 void TrackedCallback::MarkAsCompleted() { | 212 void TrackedCallback::MarkAsCompleted() { |
| 230 DCHECK(!completed()); | 213 base::AutoLock acquire(lock_); |
| 214 MarkAsCompletedWithLock(); | |
| 215 } | |
| 216 | |
| 217 void TrackedCallback::MarkAsCompletedWithLock() { | |
| 218 lock_.AssertAcquired(); | |
| 219 DCHECK(!completed_); | |
| 231 | 220 |
| 232 // We will be removed; maintain a reference to ensure we won't be deleted | 221 // We will be removed; maintain a reference to ensure we won't be deleted |
| 233 // until we're done. | 222 // until we're done. |
| 234 scoped_refptr<TrackedCallback> thiz = this; | 223 scoped_refptr<TrackedCallback> thiz = this; |
| 235 completed_ = true; | 224 completed_ = true; |
| 236 // We may not have a valid resource, in which case we're not in the tracker. | 225 // We may not have a valid resource, in which case we're not in the tracker. |
| 237 if (resource_id_) | 226 if (resource_id_) |
| 238 tracker_->Remove(thiz); | 227 tracker_->Remove(thiz); |
| 239 tracker_ = NULL; | 228 tracker_ = NULL; |
| 229 target_loop_ = NULL; | |
| 230 } | |
| 231 | |
| 232 void TrackedCallback::PostRunWithLock(int32_t result) { | |
| 233 lock_.AssertAcquired(); | |
| 234 if (completed_) { | |
| 235 NOTREACHED(); | |
| 236 return; | |
| 237 } | |
| 238 if (result == PP_ERROR_ABORTED) | |
| 239 aborted_ = true; | |
| 240 // We might abort when there's already a scheduled callback, but callers | |
| 241 // should never try to PostRun more than once otherwise. | |
| 242 DCHECK(result == PP_ERROR_ABORTED || !is_scheduled_); | |
| 243 | |
| 244 if (is_blocking()) { | |
| 245 // We might not have a MessageLoop to post to, so we must Signal | |
| 246 // directly. | |
| 247 SignalBlockingCallback(result); | |
| 248 } else { | |
| 249 base::Closure callback_closure( | |
| 250 RunWhileLocked(base::Bind(&TrackedCallback::Run, this, result))); | |
| 251 if (target_loop_) { | |
| 252 target_loop_->PostClosure(FROM_HERE, callback_closure, 0); | |
| 253 } else { | |
| 254 // We must be running in-process and on the main thread (the Enter | |
| 255 // classes protect against having a null target_loop_ otherwise). | |
| 256 DCHECK(IsMainThread()); | |
| 257 DCHECK(PpapiGlobals::Get()->IsHostGlobals()); | |
| 258 base::MessageLoop::current()->PostTask(FROM_HERE, callback_closure); | |
| 259 } | |
| 260 } | |
| 261 is_scheduled_ = true; | |
| 262 } | |
| 263 | |
| 264 void TrackedCallback::SignalBlockingCallback(int32_t result) { | |
| 265 lock_.AssertAcquired(); | |
| 266 DCHECK(is_blocking()); | |
| 267 if (!operation_completed_condvar_) { | |
| 268 // If the condition variable is invalid, there are two possibilities. One, | |
| 269 // we're running in-process, in which case the call should have come in on | |
| 270 // the main thread and we should have returned PP_ERROR_BLOCKS_MAIN_THREAD | |
| 271 // well before this. Otherwise, this callback was not created as a | |
| 272 // blocking callback. Either way, there's some internal error. | |
| 273 NOTREACHED(); | |
| 274 return; | |
| 275 } | |
| 276 result_for_blocked_callback_ = result; | |
| 277 // Retain ourselves, since MarkAsCompleted will remove us from the | |
| 278 // tracker. Then MarkAsCompleted before waking up the blocked thread, | |
| 279 // which could potentially re-enter. | |
| 280 scoped_refptr<TrackedCallback> thiz(this); | |
| 281 MarkAsCompletedWithLock(); | |
| 282 // Wake up the blocked thread. See BlockUntilComplete for where the thread | |
| 283 // Wait()s. | |
| 284 operation_completed_condvar_->Signal(); | |
| 240 } | 285 } |
| 241 | 286 |
| 242 } // namespace ppapi | 287 } // namespace ppapi |
| OLD | NEW |