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