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

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

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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #ifndef PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ 5 #ifndef PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_
6 #define PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ 6 #define PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/lock.h"
13 #include "ppapi/c/pp_resource.h" 14 #include "ppapi/c/pp_resource.h"
14 #include "ppapi/shared_impl/ppapi_shared_export.h" 15 #include "ppapi/shared_impl/ppapi_shared_export.h"
15 16
16 namespace ppapi { 17 namespace ppapi {
17 18
18 class TrackedCallback; 19 class TrackedCallback;
19 20
20 // Pepper callbacks have the following semantics (unless otherwise specified; 21 // Pepper callbacks have the following semantics (unless otherwise specified;
21 // in particular, the below apply to all completion callbacks): 22 // in particular, the below apply to all completion callbacks):
22 // - Callbacks are always run on the main thread. 23 // - Callbacks are always run on the thread where the plugin called a Pepper
23 // - Callbacks are always called from the main message loop. In particular, 24 // function providing that callback.
24 // calling into Pepper will not result in the plugin being re-entered via a 25 // - Callbacks are always called from the message loop of the thread. In
25 // synchronously-run callback. 26 // particular, calling into Pepper will not result in the plugin being
27 // re-entered via a synchronously-run callback.
26 // - Each callback will be executed (a.k.a. completed) exactly once. 28 // - Each callback will be executed (a.k.a. completed) exactly once.
27 // - Each callback may be *aborted*, which means that it will be executed with 29 // - Each callback may be *aborted*, which means that it will be executed with
28 // result |PP_ERROR_ABORTED| (in the case of completion callbacks). 30 // result |PP_ERROR_ABORTED| (in the case of completion callbacks). The
29 // - Before |PPP_ShutdownModule()| is called, every pending callback (for that 31 // ABORT counts as the callback's one completion.
30 // module) will be aborted. 32 // - Before |PPP_ShutdownModule()| is called, every pending callback (for every
33 // instance of that module) will be aborted.
31 // - Callbacks are usually associated to a resource, whose "deletion" provides 34 // - Callbacks are usually associated to a resource, whose "deletion" provides
32 // a "cancellation" (or abort) mechanism -- see below. 35 // a "cancellation" (or abort) mechanism -- see below.
33 // - When a plugin releases its last reference to resource, all callbacks 36 // - When a plugin releases its last reference to resource, all callbacks
34 // associated to that resource are aborted. Even if a non-abortive completion 37 // associated to that resource are aborted. Even if a non-abortive completion
35 // of such a callback had previously been scheduled (i.e., posted), that 38 // of such a callback had previously been scheduled (i.e., posted), that
36 // callback must now be aborted. The aborts should be scheduled immediately 39 // callback must now be aborted. The aborts should be scheduled immediately
37 // (upon the last reference being given up) and should not rely on anything 40 // (upon the last reference being given up) and should not rely on anything
38 // else (e.g., a background task to complete or further action from the 41 // else (e.g., a background task to complete or further action from the
39 // plugin). 42 // plugin).
40 // - Abortive completion gives no information about the status of the 43 // - Abortive completion gives no information about the status of the
41 // asynchronous operation: The operation may have not yet begun, may be in 44 // asynchronous operation: The operation may have not yet begun, may be in
42 // progress, or may be completed (successfully or not). In fact, the 45 // progress, or may be completed (successfully or not). In fact, the
43 // operation may still proceed after the callback has been aborted. 46 // operation may still proceed after the callback has been aborted.
44 // - Any output data buffers provided to Pepper are associated with a resource. 47 // - Any output data buffers provided to Pepper are associated with a resource.
45 // Once that resource is released, no subsequent writes to those buffers. (If 48 // Once that resource is released, no subsequent writes to those buffers
46 // background threads are set up to write into such buffers, the final 49 // will occur. When operations occur on background threads, writing to the
47 // release operation should not return into the plugin until it can 50 // plugin's data buffers should be delayed to happen on the callback's thread
48 // guaranteed that those threads will no longer write into the buffers.) 51 // to ensure that we don't write to the buffers if the callback has been
52 // aborted (see TrackedCallback::set_completion_task()).
49 // 53 //
50 // Thread-safety notes: 54 // Thread-safety notes:
51 // Currently, everything should happen on the main thread. The objects are 55 // |CallbackTracker| uses a lock to protect its dictionary of callbacks. This
52 // thread-safe ref-counted, so objects which live on different threads may keep 56 // is primarily to allow the safe removal of callbacks from any thread without
53 // references. Releasing a reference to |TrackedCallback| on a different thread 57 // requiring that the |ProxyLock| is held. Methods that may invoke a callback
54 // (possibly causing destruction) is also okay. Otherwise, all methods should be 58 // need to have the |ProxyLock| (and those methods assert that it's acquired).
55 // called only from the main thread. 59 // The callbacks are thread-safe ref-counted, so objects which live on different
bbudge 2015/03/27 23:46:20 nit: Slightly clearer to say just "Callbacks are t
dmichael (off chromium) 2015/03/31 19:38:05 Done.
56 60 // threads may keep references. Releasing a reference to |TrackedCallback| on a
57 // |CallbackTracker| tracks pending Pepper callbacks for a single module. It 61 // different thread (possibly causing destruction) is also okay.
58 // also tracks, for each resource ID, which callbacks are pending. When a 62 //
59 // callback is (just about to be) completed, it is removed from the tracker. We 63 // |CallbackTracker| tracks pending Pepper callbacks for a single instance. It
60 // use |CallbackTracker| for two things: (1) to ensure that all callbacks are 64 // also tracks, for each resource ID, which callbacks are pending. Just before
61 // properly aborted before module shutdown, and (2) to ensure that all callbacks 65 // a callback is completed, it is removed from the tracker. We use
62 // associated to a given resource are aborted when a plugin (module) releases 66 // |CallbackTracker| for two things: (1) to ensure that all callbacks are
63 // its last reference to that resource. 67 // properly aborted before instance shutdown, and (2) to ensure that all
68 // callbacks associated with a given resource are aborted when a plugin instance
69 // releases its last reference to that resource.
64 class PPAPI_SHARED_EXPORT CallbackTracker 70 class PPAPI_SHARED_EXPORT CallbackTracker
65 : public base::RefCountedThreadSafe<CallbackTracker> { 71 : public base::RefCountedThreadSafe<CallbackTracker> {
66 public: 72 public:
67 CallbackTracker(); 73 CallbackTracker();
68 74
69 // Abort all callbacks (synchronously). 75 // Abort all callbacks (synchronously).
70 void AbortAll(); 76 void AbortAll();
71 77
72 // Abort all callbacks associated to the given resource ID (which must be 78 // Abort all callbacks associated to the given resource ID (which must be
73 // valid, i.e., nonzero) by posting a task (or tasks). 79 // valid, i.e., nonzero) by posting a task (or tasks).
(...skipping 11 matching lines...) Expand all
85 91
86 // For each resource ID with a pending callback, store a set with its pending 92 // For each resource ID with a pending callback, store a set with its pending
87 // callbacks. (Resource ID 0 is used for callbacks not associated to a valid 93 // callbacks. (Resource ID 0 is used for callbacks not associated to a valid
88 // resource.) If a resource ID is re-used for another resource, there may be 94 // resource.) If a resource ID is re-used for another resource, there may be
89 // aborted callbacks corresponding to the original resource in that set; these 95 // aborted callbacks corresponding to the original resource in that set; these
90 // will be removed when they are completed (abortively). 96 // will be removed when they are completed (abortively).
91 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet; 97 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet;
92 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap; 98 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap;
93 CallbackSetMap pending_callbacks_; 99 CallbackSetMap pending_callbacks_;
94 100
101 // Used to ensure we don't add any callbacks after AbortAll.
102 bool abort_all_called_;
103
104 base::Lock lock_;
105
95 DISALLOW_COPY_AND_ASSIGN(CallbackTracker); 106 DISALLOW_COPY_AND_ASSIGN(CallbackTracker);
96 }; 107 };
97 108
98 } // namespace ppapi 109 } // namespace ppapi
99 110
100 #endif // PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ 111 #endif // PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698