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