OLD | NEW |
(Empty) | |
| 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_TASK_RUNNER_H_ |
| 6 #define BASE_TASK_RUNNER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/base_export.h" |
| 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 |
| 14 namespace tracked_objects { |
| 15 class Location; |
| 16 } // namespace tracked_objects |
| 17 |
| 18 namespace base { |
| 19 |
| 20 struct TaskRunnerTraits; |
| 21 |
| 22 // A TaskRunner is an object that runs posted tasks (in the form of |
| 23 // Closure objects). The TaskRunner interface provides a way of |
| 24 // decoupling task posting from the mechanics of how each task will be |
| 25 // run. TaskRunner provides very weak guarantees as to how posted |
| 26 // tasks are run (or if they're run at all). In particular, it only |
| 27 // guarantees: |
| 28 // |
| 29 // - Posting a task will not run it synchronously. That is, no |
| 30 // Post*Task method will call task.Run() directly. |
| 31 // |
| 32 // - Increasing the delay can only delay when the task gets run. |
| 33 // That is, increasing the delay may not affect when the task gets |
| 34 // run, or it could make it run later than it normally would, but |
| 35 // it won't make it run earlier than it normally would. |
| 36 // |
| 37 // TaskRunner does not guarantee the order in which posted tasks are |
| 38 // run, whether tasks overlap, or whether they're run on a particular |
| 39 // thread. Also it does not guarantee a memory model for shared data |
| 40 // between tasks. (In other words, you should use your own |
| 41 // synchronization/locking primitives if you need to share data |
| 42 // between tasks.) |
| 43 // |
| 44 // Implementations of TaskRunner should be thread-safe in that all |
| 45 // methods must be safe to call on any thread. Ownership semantics |
| 46 // for TaskRunners are in general not clear, which is why the |
| 47 // interface itself is RefCountedThreadSafe. |
| 48 // |
| 49 // Some theoretical implementations of TaskRunner: |
| 50 // |
| 51 // - A TaskRunner that uses a thread pool to run posted tasks. |
| 52 // |
| 53 // - A TaskRunner that, for each task, spawns a non-joinable thread |
| 54 // to run that task and immediately quit. |
| 55 // |
| 56 // - A TaskRunner that stores the list of posted tasks and has a |
| 57 // method Run() that runs each runnable task in random order. |
| 58 class BASE_EXPORT TaskRunner |
| 59 : public RefCountedThreadSafe<TaskRunner, TaskRunnerTraits> { |
| 60 public: |
| 61 // Posts the given task to be run. Returns true if the task may be |
| 62 // run at some point in the future, and false if the task definitely |
| 63 // will not be run. |
| 64 // |
| 65 // Equivalent to PostDelayedTask(from_here, task, 0). |
| 66 bool PostTask(const tracked_objects::Location& from_here, |
| 67 const Closure& task); |
| 68 |
| 69 // Like PostTask, but tries to run the posted task only after |
| 70 // |delay_ms| has passed. |
| 71 // |
| 72 // It is valid for an implementation to ignore |delay_ms|; that is, |
| 73 // to have PostDelayedTask behave the same as PostTask. |
| 74 // |
| 75 // TODO(akalin): Make PostDelayedTask use TimeDelta instead. |
| 76 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 77 const Closure& task, |
| 78 int64 delay_ms) = 0; |
| 79 |
| 80 // Returns true if the current thread is a thread on which a task |
| 81 // may be run, and false if no task will be run on the current |
| 82 // thread. |
| 83 // |
| 84 // It is valid for an implementation to always return true, or in |
| 85 // general to use 'true' as a default value. |
| 86 virtual bool RunsTasksOnCurrentThread() const = 0; |
| 87 |
| 88 // Posts |task| on the current TaskRunner. On completion, |reply| |
| 89 // is posted to the thread that called PostTaskAndReply(). Both |
| 90 // |task| and |reply| are guaranteed to be deleted on the thread |
| 91 // from which PostTaskAndReply() is invoked. This allows objects |
| 92 // that must be deleted on the originating thread to be bound into |
| 93 // the |task| and |reply| Closures. In particular, it can be useful |
| 94 // to use WeakPtr<> in the |reply| Closure so that the reply |
| 95 // operation can be canceled. See the following pseudo-code: |
| 96 // |
| 97 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { |
| 98 // public: |
| 99 // // Called to add data into a buffer. |
| 100 // void AddData(void* buf, size_t length); |
| 101 // ... |
| 102 // }; |
| 103 // |
| 104 // |
| 105 // class DataLoader : public SupportsWeakPtr<DataLoader> { |
| 106 // public: |
| 107 // void GetData() { |
| 108 // scoped_refptr<DataBuffer> buffer = new DataBuffer(); |
| 109 // target_thread_.message_loop_proxy()->PostTaskAndReply( |
| 110 // FROM_HERE, |
| 111 // base::Bind(&DataBuffer::AddData, buffer), |
| 112 // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); |
| 113 // } |
| 114 // |
| 115 // private: |
| 116 // void OnDataReceived(scoped_refptr<DataBuffer> buffer) { |
| 117 // // Do something with buffer. |
| 118 // } |
| 119 // }; |
| 120 // |
| 121 // |
| 122 // Things to notice: |
| 123 // * Results of |task| are shared with |reply| by binding a shared argument |
| 124 // (a DataBuffer instance). |
| 125 // * The DataLoader object has no special thread safety. |
| 126 // * The DataLoader object can be deleted while |task| is still running, |
| 127 // and the reply will cancel itself safely because it is bound to a |
| 128 // WeakPtr<>. |
| 129 bool PostTaskAndReply(const tracked_objects::Location& from_here, |
| 130 const Closure& task, |
| 131 const Closure& reply); |
| 132 |
| 133 protected: |
| 134 friend struct TaskRunnerTraits; |
| 135 |
| 136 // Only the Windows debug build seems to need this: see |
| 137 // http://crbug.com/112250. |
| 138 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>; |
| 139 |
| 140 TaskRunner(); |
| 141 virtual ~TaskRunner(); |
| 142 |
| 143 // Called when this object should be destroyed. By default simply |
| 144 // deletes |this|, but can be overridden to do something else, like |
| 145 // delete on a certain thread. |
| 146 virtual void OnDestruct() const; |
| 147 }; |
| 148 |
| 149 struct BASE_EXPORT TaskRunnerTraits { |
| 150 static void Destruct(const TaskRunner* task_runner); |
| 151 }; |
| 152 |
| 153 } // namespace base |
| 154 |
| 155 #endif // BASE_TASK_RUNNER_H_ |
OLD | NEW |