Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(932)

Side by Side Diff: ppapi/shared_impl/callback_tracker.cc

Issue 923263003: PPAPI: Make TrackedCallback more threadsafe (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/tracked_callback.h" 14 #include "ppapi/shared_impl/tracked_callback.h"
15 15
16 namespace ppapi { 16 namespace ppapi {
17 17
18 // CallbackTracker ------------------------------------------------------------- 18 // CallbackTracker -------------------------------------------------------------
19 19
20 CallbackTracker::CallbackTracker() {} 20 CallbackTracker::CallbackTracker() {}
21 21
22 void CallbackTracker::AbortAll() { 22 void CallbackTracker::AbortAll() {
23 // Iterate over a copy since |Abort()| calls |Remove()| (indirectly). 23 // Iterate over a copy:
24 // TODO(viettrungluu): This obviously isn't so efficient. 24 // 1) because |Abort()| calls |Remove()| (indirectly).
25 CallbackSetMap pending_callbacks_copy = pending_callbacks_; 25 // 2) So we can drop the lock before calling in to TrackedCallback.
26 CallbackSetMap pending_callbacks_copy;
27 {
28 base::AutoLock acquire(lock_);
29 pending_callbacks_copy = pending_callbacks_;
30 }
bbudge 2015/03/05 01:29:35 Reading this code, it seems possible for a callbac
dmichael (off chromium) 2015/03/18 15:51:48 Done, thanks.
26 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin(); 31 for (CallbackSetMap::iterator it1 = pending_callbacks_copy.begin();
27 it1 != pending_callbacks_copy.end(); 32 it1 != pending_callbacks_copy.end();
28 ++it1) { 33 ++it1) {
29 for (CallbackSet::iterator it2 = it1->second.begin(); 34 for (CallbackSet::iterator it2 = it1->second.begin();
30 it2 != it1->second.end(); 35 it2 != it1->second.end();
31 ++it2) { 36 ++it2) {
32 (*it2)->Abort(); 37 (*it2)->Abort();
33 } 38 }
34 } 39 }
35 } 40 }
36 41
37 void CallbackTracker::PostAbortForResource(PP_Resource resource_id) { 42 void CallbackTracker::PostAbortForResource(PP_Resource resource_id) {
38 CHECK(resource_id != 0); 43 CHECK_NE(resource_id, 0);
39 CallbackSetMap::iterator it1 = pending_callbacks_.find(resource_id); 44 CallbackSet callbacks_for_resource;
40 if (it1 == pending_callbacks_.end()) 45 {
41 return; 46 base::AutoLock acquire(lock_);
42 for (CallbackSet::iterator it2 = it1->second.begin(); 47 CallbackSetMap::iterator iter = pending_callbacks_.find(resource_id);
43 it2 != it1->second.end(); 48 // The resource may have no callbacks, so it won't be found, and we're done.
44 ++it2) { 49 if (iter == pending_callbacks_.end())
45 // Post the abort. 50 return;
46 (*it2)->PostAbort(); 51 // Copy the set so we can drop the lock before calling in to
52 // TrackedCallback.
53 callbacks_for_resource = iter->second;
54 }
55 for (const auto& iter : callbacks_for_resource) {
56 iter->PostAbort();
47 } 57 }
48 } 58 }
49 59
50 CallbackTracker::~CallbackTracker() { 60 CallbackTracker::~CallbackTracker() {
51 // All callbacks must be aborted before destruction. 61 // All callbacks must be aborted before destruction.
52 CHECK_EQ(0u, pending_callbacks_.size()); 62 CHECK_EQ(0u, pending_callbacks_.size());
53 } 63 }
54 64
55 void CallbackTracker::Add( 65 void CallbackTracker::Add(
56 const scoped_refptr<TrackedCallback>& tracked_callback) { 66 const scoped_refptr<TrackedCallback>& tracked_callback) {
67 base::AutoLock acquire(lock_);
57 PP_Resource resource_id = tracked_callback->resource_id(); 68 PP_Resource resource_id = tracked_callback->resource_id();
58 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) == 69 DCHECK(pending_callbacks_[resource_id].find(tracked_callback) ==
59 pending_callbacks_[resource_id].end()); 70 pending_callbacks_[resource_id].end());
60 pending_callbacks_[resource_id].insert(tracked_callback); 71 pending_callbacks_[resource_id].insert(tracked_callback);
61 } 72 }
62 73
63 void CallbackTracker::Remove( 74 void CallbackTracker::Remove(
64 const scoped_refptr<TrackedCallback>& tracked_callback) { 75 const scoped_refptr<TrackedCallback>& tracked_callback) {
76 base::AutoLock acquire(lock_);
65 CallbackSetMap::iterator map_it = 77 CallbackSetMap::iterator map_it =
66 pending_callbacks_.find(tracked_callback->resource_id()); 78 pending_callbacks_.find(tracked_callback->resource_id());
67 DCHECK(map_it != pending_callbacks_.end()); 79 DCHECK(map_it != pending_callbacks_.end());
68 CallbackSet::iterator it = map_it->second.find(tracked_callback); 80 CallbackSet::iterator it = map_it->second.find(tracked_callback);
69 DCHECK(it != map_it->second.end()); 81 DCHECK(it != map_it->second.end());
70 map_it->second.erase(it); 82 map_it->second.erase(it);
71 83
72 // If there are no pending callbacks left for this ID, get rid of the entry. 84 // If there are no pending callbacks left for this ID, get rid of the entry.
73 if (map_it->second.empty()) 85 if (map_it->second.empty())
74 pending_callbacks_.erase(map_it); 86 pending_callbacks_.erase(map_it);
75 } 87 }
76 88
77 } // namespace ppapi 89 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698