Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_SEQUENCED_TASKRUNNER_H_ |
michael@0 | 6 | #define BASE_SEQUENCED_TASKRUNNER_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/base_export.h" |
michael@0 | 9 | #include "base/sequenced_task_runner_helpers.h" |
michael@0 | 10 | #include "base/task_runner.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace base { |
michael@0 | 13 | |
michael@0 | 14 | // A SequencedTaskRunner is a subclass of TaskRunner that provides |
michael@0 | 15 | // additional guarantees on the order that tasks are started, as well |
michael@0 | 16 | // as guarantees on when tasks are in sequence, i.e. one task finishes |
michael@0 | 17 | // before the other one starts. |
michael@0 | 18 | // |
michael@0 | 19 | // Summary |
michael@0 | 20 | // ------- |
michael@0 | 21 | // Non-nested tasks with the same delay will run one by one in FIFO |
michael@0 | 22 | // order. |
michael@0 | 23 | // |
michael@0 | 24 | // Detailed guarantees |
michael@0 | 25 | // ------------------- |
michael@0 | 26 | // |
michael@0 | 27 | // SequencedTaskRunner also adds additional methods for posting |
michael@0 | 28 | // non-nestable tasks. In general, an implementation of TaskRunner |
michael@0 | 29 | // may expose task-running methods which are themselves callable from |
michael@0 | 30 | // within tasks. A non-nestable task is one that is guaranteed to not |
michael@0 | 31 | // be run from within an already-running task. Conversely, a nestable |
michael@0 | 32 | // task (the default) is a task that can be run from within an |
michael@0 | 33 | // already-running task. |
michael@0 | 34 | // |
michael@0 | 35 | // The guarantees of SequencedTaskRunner are as follows: |
michael@0 | 36 | // |
michael@0 | 37 | // - Given two tasks T2 and T1, T2 will start after T1 starts if: |
michael@0 | 38 | // |
michael@0 | 39 | // * T2 is posted after T1; and |
michael@0 | 40 | // * T2 has equal or higher delay than T1; and |
michael@0 | 41 | // * T2 is non-nestable or T1 is nestable. |
michael@0 | 42 | // |
michael@0 | 43 | // - If T2 will start after T1 starts by the above guarantee, then |
michael@0 | 44 | // T2 will start after T1 finishes and is destroyed if: |
michael@0 | 45 | // |
michael@0 | 46 | // * T2 is non-nestable, or |
michael@0 | 47 | // * T1 doesn't call any task-running methods. |
michael@0 | 48 | // |
michael@0 | 49 | // - If T2 will start after T1 finishes by the above guarantee, then |
michael@0 | 50 | // all memory changes in T1 and T1's destruction will be visible |
michael@0 | 51 | // to T2. |
michael@0 | 52 | // |
michael@0 | 53 | // - If T2 runs nested within T1 via a call to the task-running |
michael@0 | 54 | // method M, then all memory changes in T1 up to the call to M |
michael@0 | 55 | // will be visible to T2, and all memory changes in T2 will be |
michael@0 | 56 | // visible to T1 from the return from M. |
michael@0 | 57 | // |
michael@0 | 58 | // Note that SequencedTaskRunner does not guarantee that tasks are run |
michael@0 | 59 | // on a single dedicated thread, although the above guarantees provide |
michael@0 | 60 | // most (but not all) of the same guarantees. If you do need to |
michael@0 | 61 | // guarantee that tasks are run on a single dedicated thread, see |
michael@0 | 62 | // SingleThreadTaskRunner (in single_thread_task_runner.h). |
michael@0 | 63 | // |
michael@0 | 64 | // Some corollaries to the above guarantees, assuming the tasks in |
michael@0 | 65 | // question don't call any task-running methods: |
michael@0 | 66 | // |
michael@0 | 67 | // - Tasks posted via PostTask are run in FIFO order. |
michael@0 | 68 | // |
michael@0 | 69 | // - Tasks posted via PostNonNestableTask are run in FIFO order. |
michael@0 | 70 | // |
michael@0 | 71 | // - Tasks posted with the same delay and the same nestable state |
michael@0 | 72 | // are run in FIFO order. |
michael@0 | 73 | // |
michael@0 | 74 | // - A list of tasks with the same nestable state posted in order of |
michael@0 | 75 | // non-decreasing delay is run in FIFO order. |
michael@0 | 76 | // |
michael@0 | 77 | // - A list of tasks posted in order of non-decreasing delay with at |
michael@0 | 78 | // most a single change in nestable state from nestable to |
michael@0 | 79 | // non-nestable is run in FIFO order. (This is equivalent to the |
michael@0 | 80 | // statement of the first guarantee above.) |
michael@0 | 81 | // |
michael@0 | 82 | // Some theoretical implementations of SequencedTaskRunner: |
michael@0 | 83 | // |
michael@0 | 84 | // - A SequencedTaskRunner that wraps a regular TaskRunner but makes |
michael@0 | 85 | // sure that only one task at a time is posted to the TaskRunner, |
michael@0 | 86 | // with appropriate memory barriers in between tasks. |
michael@0 | 87 | // |
michael@0 | 88 | // - A SequencedTaskRunner that, for each task, spawns a joinable |
michael@0 | 89 | // thread to run that task and immediately quit, and then |
michael@0 | 90 | // immediately joins that thread. |
michael@0 | 91 | // |
michael@0 | 92 | // - A SequencedTaskRunner that stores the list of posted tasks and |
michael@0 | 93 | // has a method Run() that runs each runnable task in FIFO order |
michael@0 | 94 | // that can be called from any thread, but only if another |
michael@0 | 95 | // (non-nested) Run() call isn't already happening. |
michael@0 | 96 | class BASE_EXPORT SequencedTaskRunner : public TaskRunner { |
michael@0 | 97 | public: |
michael@0 | 98 | // The two PostNonNestable*Task methods below are like their |
michael@0 | 99 | // nestable equivalents in TaskRunner, but they guarantee that the |
michael@0 | 100 | // posted task will not run nested within an already-running task. |
michael@0 | 101 | // |
michael@0 | 102 | // A simple corollary is that posting a task as non-nestable can |
michael@0 | 103 | // only delay when the task gets run. That is, posting a task as |
michael@0 | 104 | // non-nestable may not affect when the task gets run, or it could |
michael@0 | 105 | // make it run later than it normally would, but it won't make it |
michael@0 | 106 | // run earlier than it normally would. |
michael@0 | 107 | |
michael@0 | 108 | // TODO(akalin): Get rid of the boolean return value for the methods |
michael@0 | 109 | // below. |
michael@0 | 110 | |
michael@0 | 111 | bool PostNonNestableTask(const tracked_objects::Location& from_here, |
michael@0 | 112 | const Closure& task); |
michael@0 | 113 | |
michael@0 | 114 | virtual bool PostNonNestableDelayedTask( |
michael@0 | 115 | const tracked_objects::Location& from_here, |
michael@0 | 116 | const Closure& task, |
michael@0 | 117 | base::TimeDelta delay) = 0; |
michael@0 | 118 | |
michael@0 | 119 | // Submits a non-nestable task to delete the given object. Returns |
michael@0 | 120 | // true if the object may be deleted at some point in the future, |
michael@0 | 121 | // and false if the object definitely will not be deleted. |
michael@0 | 122 | template <class T> |
michael@0 | 123 | bool DeleteSoon(const tracked_objects::Location& from_here, |
michael@0 | 124 | const T* object) { |
michael@0 | 125 | return |
michael@0 | 126 | subtle::DeleteHelperInternal<T, bool>::DeleteViaSequencedTaskRunner( |
michael@0 | 127 | this, from_here, object); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | // Submits a non-nestable task to release the given object. Returns |
michael@0 | 131 | // true if the object may be released at some point in the future, |
michael@0 | 132 | // and false if the object definitely will not be released. |
michael@0 | 133 | template <class T> |
michael@0 | 134 | bool ReleaseSoon(const tracked_objects::Location& from_here, |
michael@0 | 135 | T* object) { |
michael@0 | 136 | return |
michael@0 | 137 | subtle::ReleaseHelperInternal<T, bool>::ReleaseViaSequencedTaskRunner( |
michael@0 | 138 | this, from_here, object); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | protected: |
michael@0 | 142 | virtual ~SequencedTaskRunner() {} |
michael@0 | 143 | |
michael@0 | 144 | private: |
michael@0 | 145 | template <class T, class R> friend class subtle::DeleteHelperInternal; |
michael@0 | 146 | template <class T, class R> friend class subtle::ReleaseHelperInternal; |
michael@0 | 147 | |
michael@0 | 148 | bool DeleteSoonInternal(const tracked_objects::Location& from_here, |
michael@0 | 149 | void(*deleter)(const void*), |
michael@0 | 150 | const void* object); |
michael@0 | 151 | |
michael@0 | 152 | bool ReleaseSoonInternal(const tracked_objects::Location& from_here, |
michael@0 | 153 | void(*releaser)(const void*), |
michael@0 | 154 | const void* object); |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | } // namespace base |
michael@0 | 158 | |
michael@0 | 159 | #endif // BASE_SEQUENCED_TASKRUNNER_H_ |