michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_TASK_RUNNER_H_ michael@0: #define BASE_TASK_RUNNER_H_ michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/callback_forward.h" michael@0: #include "base/memory/ref_counted.h" michael@0: #include "base/time/time.h" michael@0: michael@0: namespace tracked_objects { michael@0: class Location; michael@0: } // namespace tracked_objects michael@0: michael@0: namespace base { michael@0: michael@0: struct TaskRunnerTraits; michael@0: michael@0: // A TaskRunner is an object that runs posted tasks (in the form of michael@0: // Closure objects). The TaskRunner interface provides a way of michael@0: // decoupling task posting from the mechanics of how each task will be michael@0: // run. TaskRunner provides very weak guarantees as to how posted michael@0: // tasks are run (or if they're run at all). In particular, it only michael@0: // guarantees: michael@0: // michael@0: // - Posting a task will not run it synchronously. That is, no michael@0: // Post*Task method will call task.Run() directly. michael@0: // michael@0: // - Increasing the delay can only delay when the task gets run. michael@0: // That is, increasing the delay may not affect when the task gets michael@0: // run, or it could make it run later than it normally would, but michael@0: // it won't make it run earlier than it normally would. michael@0: // michael@0: // TaskRunner does not guarantee the order in which posted tasks are michael@0: // run, whether tasks overlap, or whether they're run on a particular michael@0: // thread. Also it does not guarantee a memory model for shared data michael@0: // between tasks. (In other words, you should use your own michael@0: // synchronization/locking primitives if you need to share data michael@0: // between tasks.) michael@0: // michael@0: // Implementations of TaskRunner should be thread-safe in that all michael@0: // methods must be safe to call on any thread. Ownership semantics michael@0: // for TaskRunners are in general not clear, which is why the michael@0: // interface itself is RefCountedThreadSafe. michael@0: // michael@0: // Some theoretical implementations of TaskRunner: michael@0: // michael@0: // - A TaskRunner that uses a thread pool to run posted tasks. michael@0: // michael@0: // - A TaskRunner that, for each task, spawns a non-joinable thread michael@0: // to run that task and immediately quit. michael@0: // michael@0: // - A TaskRunner that stores the list of posted tasks and has a michael@0: // method Run() that runs each runnable task in random order. michael@0: class BASE_EXPORT TaskRunner michael@0: : public RefCountedThreadSafe { michael@0: public: michael@0: // Posts the given task to be run. Returns true if the task may be michael@0: // run at some point in the future, and false if the task definitely michael@0: // will not be run. michael@0: // michael@0: // Equivalent to PostDelayedTask(from_here, task, 0). michael@0: bool PostTask(const tracked_objects::Location& from_here, michael@0: const Closure& task); michael@0: michael@0: // Like PostTask, but tries to run the posted task only after michael@0: // |delay_ms| has passed. michael@0: // michael@0: // It is valid for an implementation to ignore |delay_ms|; that is, michael@0: // to have PostDelayedTask behave the same as PostTask. michael@0: virtual bool PostDelayedTask(const tracked_objects::Location& from_here, michael@0: const Closure& task, michael@0: base::TimeDelta delay) = 0; michael@0: michael@0: // Returns true if the current thread is a thread on which a task michael@0: // may be run, and false if no task will be run on the current michael@0: // thread. michael@0: // michael@0: // It is valid for an implementation to always return true, or in michael@0: // general to use 'true' as a default value. michael@0: virtual bool RunsTasksOnCurrentThread() const = 0; michael@0: michael@0: // Posts |task| on the current TaskRunner. On completion, |reply| michael@0: // is posted to the thread that called PostTaskAndReply(). Both michael@0: // |task| and |reply| are guaranteed to be deleted on the thread michael@0: // from which PostTaskAndReply() is invoked. This allows objects michael@0: // that must be deleted on the originating thread to be bound into michael@0: // the |task| and |reply| Closures. In particular, it can be useful michael@0: // to use WeakPtr<> in the |reply| Closure so that the reply michael@0: // operation can be canceled. See the following pseudo-code: michael@0: // michael@0: // class DataBuffer : public RefCountedThreadSafe { michael@0: // public: michael@0: // // Called to add data into a buffer. michael@0: // void AddData(void* buf, size_t length); michael@0: // ... michael@0: // }; michael@0: // michael@0: // michael@0: // class DataLoader : public SupportsWeakPtr { michael@0: // public: michael@0: // void GetData() { michael@0: // scoped_refptr buffer = new DataBuffer(); michael@0: // target_thread_.message_loop_proxy()->PostTaskAndReply( michael@0: // FROM_HERE, michael@0: // base::Bind(&DataBuffer::AddData, buffer), michael@0: // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); michael@0: // } michael@0: // michael@0: // private: michael@0: // void OnDataReceived(scoped_refptr buffer) { michael@0: // // Do something with buffer. michael@0: // } michael@0: // }; michael@0: // michael@0: // michael@0: // Things to notice: michael@0: // * Results of |task| are shared with |reply| by binding a shared argument michael@0: // (a DataBuffer instance). michael@0: // * The DataLoader object has no special thread safety. michael@0: // * The DataLoader object can be deleted while |task| is still running, michael@0: // and the reply will cancel itself safely because it is bound to a michael@0: // WeakPtr<>. michael@0: bool PostTaskAndReply(const tracked_objects::Location& from_here, michael@0: const Closure& task, michael@0: const Closure& reply); michael@0: michael@0: protected: michael@0: friend struct TaskRunnerTraits; michael@0: michael@0: // Only the Windows debug build seems to need this: see michael@0: // http://crbug.com/112250. michael@0: friend class RefCountedThreadSafe; michael@0: michael@0: TaskRunner(); michael@0: virtual ~TaskRunner(); michael@0: michael@0: // Called when this object should be destroyed. By default simply michael@0: // deletes |this|, but can be overridden to do something else, like michael@0: // delete on a certain thread. michael@0: virtual void OnDestruct() const; michael@0: }; michael@0: michael@0: struct BASE_EXPORT TaskRunnerTraits { michael@0: static void Destruct(const TaskRunner* task_runner); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_TASK_RUNNER_H_