Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/callback_tracker.h" | 5 #include "ppapi/shared_impl/callback_tracker.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "ppapi/c/pp_completion_callback.h" | 13 #include "ppapi/c/pp_completion_callback.h" |
| 14 #include "ppapi/shared_impl/proxy_lock.h" | |
| 14 #include "ppapi/shared_impl/tracked_callback.h" | 15 #include "ppapi/shared_impl/tracked_callback.h" |
| 15 | 16 |
| 16 namespace ppapi { | 17 namespace ppapi { |
| 17 | 18 |
| 18 // CallbackTracker ------------------------------------------------------------- | 19 // CallbackTracker ------------------------------------------------------------- |
| 19 | 20 |
| 20 CallbackTracker::CallbackTracker() {} | 21 CallbackTracker::CallbackTracker() {} |
| 21 | 22 |
| 22 void CallbackTracker::AbortAll() { | 23 void CallbackTracker::AbortAll() { |
| 23 // Iterate over a copy since |Abort()| calls |Remove()| (indirectly). | 24 // We must have the ProxyLock whenever this method is called; this ensures |
| 24 // TODO(viettrungluu): This obviously isn't so efficient. | 25 // that no callbacks are added while we're aborting them all (see Add). |
| 25 CallbackSetMap pending_callbacks_copy = pending_callbacks_; | 26 ProxyLock::AssertAcquired(); |
| 27 | |
| 28 // Iterate over a copy: | |
| 29 // 1) because |Abort()| calls |Remove()| (indirectly). | |
| 30 // 2) So we can drop the lock before calling in to TrackedCallback. | |
| 31 CallbackSetMap pending_callbacks_copy; | |
| 32 { | |
| 33 base::AutoLock acquire(lock_); | |
| 34 pending_callbacks_copy = pending_callbacks_; | |
| 35 } | |
| 26 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); | 36 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); |
| 27 it1 != pending_callbacks_copy.end(); | 37 it1 != pending_callbacks_copy.end(); |
| 28 ++it1) { | 38 ++it1) { |
| 29 for (CallbackSet::iterator it2 = it1->second.begin(); | 39 for (CallbackSet::iterator it2 = it1->second.begin(); |
| 30 it2 != it1->second.end(); | 40 it2 != it1->second.end(); |
| 31 ++it2) { | 41 ++it2) { |
| 32 (*it2)->Abort(); | 42 (*it2)->Abort(); |
| 33 } | 43 } |
| 34 } | 44 } |
| 35 } | 45 } |
| 36 | 46 |
| 37 void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { | 47 void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { |
| 38 CHECK(resource_id != 0); | 48 CHECK_NE(resource_id, 0); |
| 39 CallbackSetMap::iterator it1 = pending_callbacks_.find(resource_id); | 49 CallbackSet callbacks_for_resource; |
|
bbudge
2015/03/19 00:21:10
It seems like we need the proxy lock here for the
dmichael (off chromium)
2015/03/19 17:50:58
Hmm. The more I think about it, the more I think t
| |
| 40 if (it1 == pending_callbacks_.end()) | 50 { |
| 41 return; | 51 base::AutoLock acquire(lock_); |
| 42 for (CallbackSet::iterator it2 = it1->second.begin(); | 52 CallbackSetMap::iterator iter = pending_callbacks_.find(resource_id); |
| 43 it2 != it1->second.end(); | 53 // The resource may have no callbacks, so it won't be found, and we're done. |
| 44 ++it2) { | 54 if (iter == pending_callbacks_.end()) |
| 45 // Post the abort. | 55 return; |
| 46 (*it2)->PostAbort(); | 56 // Copy the set so we can drop the lock before calling in to |
| 57 // TrackedCallback. | |
| 58 callbacks_for_resource = iter->second; | |
| 59 } | |
| 60 for (const auto& iter : callbacks_for_resource) { | |
| 61 iter->PostAbort(); | |
| 47 } | 62 } |
| 48 } | 63 } |
| 49 | 64 |
| 50 CallbackTracker::~CallbackTracker() { | 65 CallbackTracker::~CallbackTracker() { |
| 51 // All callbacks must be aborted before destruction. | 66 // All callbacks must be aborted before destruction. |
| 52 CHECK_EQ(0u, pending_callbacks_.size()); | 67 CHECK_EQ(0u, pending_callbacks_.size()); |
| 53 } | 68 } |
| 54 | 69 |
| 55 void CallbackTracker::Add( | 70 void CallbackTracker::Add( |
| 56 const scoped_refptr<TrackedCallback>& tracked_callback) { | 71 const scoped_refptr<TrackedCallback>& tracked_callback) { |
| 72 // We must have the ProxyLock whenever this method is called; this ensures | |
| 73 // that no callbacks are added while we're aborting them all (see AbortAll). | |
|
bbudge
2015/03/19 00:21:10
This comment doesn't make sense here. Do we really
dmichael (off chromium)
2015/03/19 17:50:58
If we don't have it, having the proxy lock in Abor
bbudge
2015/03/27 23:46:20
OK, that makes sense.
| |
| 74 ProxyLock::AssertAcquired(); | |
| 75 | |
| 76 base::AutoLock acquire(lock_); | |
| 57 PP_Resource resource_id = tracked_callback->resource_id(); | 77 PP_Resource resource_id = tracked_callback->resource_id(); |
| 58 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == | 78 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == |
| 59 pending_callbacks_[resource_id].end()); | 79 pending_callbacks_[resource_id].end()); |
| 60 pending_callbacks_[resource_id].insert(tracked_callback); | 80 pending_callbacks_[resource_id].insert(tracked_callback); |
| 61 } | 81 } |
| 62 | 82 |
| 63 void CallbackTracker::Remove( | 83 void CallbackTracker::Remove( |
| 64 const scoped_refptr<TrackedCallback>& tracked_callback) { | 84 const scoped_refptr<TrackedCallback>& tracked_callback) { |
| 85 base::AutoLock acquire(lock_); | |
| 65 CallbackSetMap::iterator map_it = | 86 CallbackSetMap::iterator map_it = |
| 66 pending_callbacks_.find(tracked_callback->resource_id()); | 87 pending_callbacks_.find(tracked_callback->resource_id()); |
| 67 DCHECK(map_it != pending_callbacks_.end()); | 88 DCHECK(map_it != pending_callbacks_.end()); |
| 68 CallbackSet::iterator it = map_it->second.find(tracked_callback); | 89 CallbackSet::iterator it = map_it->second.find(tracked_callback); |
| 69 DCHECK(it != map_it->second.end()); | 90 DCHECK(it != map_it->second.end()); |
| 70 map_it->second.erase(it); | 91 map_it->second.erase(it); |
| 71 | 92 |
| 72 // If there are no pending callbacks left for this ID, get rid of the entry. | 93 // If there are no pending callbacks left for this ID, get rid of the entry. |
| 73 if (map_it->second.empty()) | 94 if (map_it->second.empty()) |
| 74 pending_callbacks_.erase(map_it); | 95 pending_callbacks_.erase(map_it); |
| 75 } | 96 } |
| 76 | 97 |
| 77 } // namespace ppapi | 98 } // namespace ppapi |
| OLD | NEW |