• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_forward.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,
65                 const Closure& task);
66 
67   // Like PostTask, but tries to run the posted task only after
68   // |delay_ms| has passed.
69   //
70   // It is valid for an implementation to ignore |delay_ms|; that is,
71   // to have PostDelayedTask behave the same as PostTask.
72   virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
73                                const Closure& task,
74                                base::TimeDelta delay) = 0;
75 
76   // Returns true if the current thread is a thread on which a task
77   // may be run, and false if no task will be run on the current
78   // thread.
79   //
80   // It is valid for an implementation to always return true, or in
81   // general to use 'true' as a default value.
82   virtual bool RunsTasksOnCurrentThread() const = 0;
83 
84   // Posts |task| on the current TaskRunner.  On completion, |reply|
85   // is posted to the thread that called PostTaskAndReply().  Both
86   // |task| and |reply| are guaranteed to be deleted on the thread
87   // from which PostTaskAndReply() is invoked.  This allows objects
88   // that must be deleted on the originating thread to be bound into
89   // the |task| and |reply| Closures.  In particular, it can be useful
90   // to use WeakPtr<> in the |reply| Closure so that the reply
91   // operation can be canceled. See the following pseudo-code:
92   //
93   // class DataBuffer : public RefCountedThreadSafe<DataBuffer> {
94   //  public:
95   //   // Called to add data into a buffer.
96   //   void AddData(void* buf, size_t length);
97   //   ...
98   // };
99   //
100   //
101   // class DataLoader : public SupportsWeakPtr<DataLoader> {
102   //  public:
103   //    void GetData() {
104   //      scoped_refptr<DataBuffer> buffer = new DataBuffer();
105   //      target_thread_.task_runner()->PostTaskAndReply(
106   //          FROM_HERE,
107   //          base::Bind(&DataBuffer::AddData, buffer),
108   //          base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer));
109   //    }
110   //
111   //  private:
112   //    void OnDataReceived(scoped_refptr<DataBuffer> buffer) {
113   //      // Do something with buffer.
114   //    }
115   // };
116   //
117   //
118   // Things to notice:
119   //   * Results of |task| are shared with |reply| by binding a shared argument
120   //     (a DataBuffer instance).
121   //   * The DataLoader object has no special thread safety.
122   //   * The DataLoader object can be deleted while |task| is still running,
123   //     and the reply will cancel itself safely because it is bound to a
124   //     WeakPtr<>.
125   bool PostTaskAndReply(const tracked_objects::Location& from_here,
126                         const Closure& task,
127                         const Closure& reply);
128 
129  protected:
130   friend struct TaskRunnerTraits;
131 
132   // Only the Windows debug build seems to need this: see
133   // http://crbug.com/112250.
134   friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>;
135 
136   TaskRunner();
137   virtual ~TaskRunner();
138 
139   // Called when this object should be destroyed.  By default simply
140   // deletes |this|, but can be overridden to do something else, like
141   // delete on a certain thread.
142   virtual void OnDestruct() const;
143 };
144 
145 struct BASE_EXPORT TaskRunnerTraits {
146   static void Destruct(const TaskRunner* task_runner);
147 };
148 
149 }  // namespace base
150 
151 #endif  // BASE_TASK_RUNNER_H_
152