OLD | NEW |
---|---|
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 |
31 // ABORT counts as the callback's one completion. | |
29 // - Before |PPP_ShutdownModule()| is called, every pending callback (for that | 32 // - Before |PPP_ShutdownModule()| is called, every pending callback (for that |
30 // module) will be aborted. | 33 // instance) will be aborted. |
bbudge
2015/03/05 01:29:35
how about s/that instance/every instance of the mo
dmichael (off chromium)
2015/03/18 15:51:48
Ah, good catch, I had "fixed" the comment and made
| |
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. (If background threads are set up to write into such buffers, |
47 // release operation should not return into the plugin until it can | 50 // the final release operation should not return into the plugin until it can |
48 // guaranteed that those threads will no longer write into the buffers.) | 51 // guaranteed that those threads will no longer write into the buffers.) |
bbudge
2015/03/05 01:29:35
s/guaranteed/be guaranteed
dmichael (off chromium)
2015/03/18 15:51:48
I think Trung wrote comment before we did backgrou
| |
49 // | 52 // |
50 // Thread-safety notes: | 53 // Thread-safety notes: |
51 // Currently, everything should happen on the main thread. The objects are | 54 // |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 | 55 // is primarily to allow the safe removal of callbacks from any thread without |
53 // references. Releasing a reference to |TrackedCallback| on a different thread | 56 // requiring that the |ProxyLock| is held. Methods that may invoke a callback |
54 // (possibly causing destruction) is also okay. Otherwise, all methods should be | 57 // need to have the |ProxyLock| (and those methods assert that it's acquired). |
55 // called only from the main thread. | 58 // The callbacks are thread-safe ref-counted, so objects which live on different |
56 | 59 // threads may keep references. Releasing a reference to |TrackedCallback| on a |
57 // |CallbackTracker| tracks pending Pepper callbacks for a single module. It | 60 // different thread (possibly causing destruction) is also okay. |
61 // | |
62 // |CallbackTracker| tracks pending Pepper callbacks for a single instance. It | |
58 // also tracks, for each resource ID, which callbacks are pending. When a | 63 // also tracks, for each resource ID, which callbacks are pending. When a |
59 // callback is (just about to be) completed, it is removed from the tracker. We | 64 // callback is (just about to be) completed, it is removed from the tracker. We |
bbudge
2015/03/05 01:29:35
nit: wording
s/When a callback is (just about to b
| |
60 // use |CallbackTracker| for two things: (1) to ensure that all callbacks are | 65 // use |CallbackTracker| for two things: (1) to ensure that all callbacks are |
61 // properly aborted before module shutdown, and (2) to ensure that all callbacks | 66 // properly aborted before instance shutdown, and (2) to ensure that all |
62 // associated to a given resource are aborted when a plugin (module) releases | 67 // callbacks associated to a given resource are aborted when a plugin instance |
bbudge
2015/03/05 01:29:35
nit: s/to/with
| |
63 // its last reference to that resource. | 68 // releases its last reference to that resource. |
64 class PPAPI_SHARED_EXPORT CallbackTracker | 69 class PPAPI_SHARED_EXPORT CallbackTracker |
65 : public base::RefCountedThreadSafe<CallbackTracker> { | 70 : public base::RefCountedThreadSafe<CallbackTracker> { |
66 public: | 71 public: |
67 CallbackTracker(); | 72 CallbackTracker(); |
68 | 73 |
69 // Abort all callbacks (synchronously). | 74 // Abort all callbacks (synchronously). |
70 void AbortAll(); | 75 void AbortAll(); |
71 | 76 |
72 // Abort all callbacks associated to the given resource ID (which must be | 77 // Abort all callbacks associated to the given resource ID (which must be |
73 // valid, i.e., nonzero) by posting a task (or tasks). | 78 // valid, i.e., nonzero) by posting a task (or tasks). |
(...skipping 11 matching lines...) Expand all Loading... | |
85 | 90 |
86 // For each resource ID with a pending callback, store a set with its pending | 91 // 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 | 92 // 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 | 93 // 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 | 94 // aborted callbacks corresponding to the original resource in that set; these |
90 // will be removed when they are completed (abortively). | 95 // will be removed when they are completed (abortively). |
91 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet; | 96 typedef std::set<scoped_refptr<TrackedCallback> > CallbackSet; |
92 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap; | 97 typedef std::map<PP_Resource, CallbackSet> CallbackSetMap; |
93 CallbackSetMap pending_callbacks_; | 98 CallbackSetMap pending_callbacks_; |
94 | 99 |
100 base::Lock lock_; | |
101 | |
95 DISALLOW_COPY_AND_ASSIGN(CallbackTracker); | 102 DISALLOW_COPY_AND_ASSIGN(CallbackTracker); |
96 }; | 103 }; |
97 | 104 |
98 } // namespace ppapi | 105 } // namespace ppapi |
99 | 106 |
100 #endif // PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ | 107 #endif // PPAPI_SHARED_IMPL_CALLBACK_TRACKER_H_ |
OLD | NEW |