security/sandbox/chromium/base/task_runner.h

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

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

mercurial