| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_MESSAGE_LOOP_PROXY_H_ | 5 #ifndef BASE_EXECUTOR_H_ |
| 6 #define BASE_MESSAGE_LOOP_PROXY_H_ | 6 #define BASE_EXECUTOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/base_export.h" | 9 #include "base/base_export.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/executor_helpers.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/message_loop_helpers.h" | |
| 14 | 14 |
| 15 namespace tracked_objects { | 15 namespace tracked_objects { |
| 16 class Location; | 16 class Location; |
| 17 } // namespace tracked_objects | 17 } // namespace tracked_objects |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 | 20 |
| 21 struct MessageLoopProxyTraits; | 21 struct ExecutorTraits; |
| 22 | 22 |
| 23 // This class provides a thread-safe refcounted interface to the Post* methods | 23 // An Executor is an object that executes submitted tasks (in the form |
| 24 // of a message loop. This class can outlive the target message loop. | 24 // of Closure objects). The Executor interface provides a way of |
| 25 // MessageLoopProxy objects are constructed automatically for all MessageLoops. | 25 // decoupling task submission from the mechanics of how each task will |
| 26 // So, to access them, you can use any of the following: | 26 // be run. Executor provides no guarantees as to how submitted tasks |
| 27 // Thread::message_loop_proxy() | 27 // are run (or if they're run at all), and in particular the order in |
| 28 // MessageLoop::current()->message_loop_proxy() | 28 // which the tasks are run and whether they're run on a particular |
| 29 // MessageLoopProxy::current() | 29 // thread. |
| 30 class BASE_EXPORT MessageLoopProxy | 30 // |
| 31 : public base::RefCountedThreadSafe<MessageLoopProxy, | 31 // Implementations of Executor should be thread-safe in that all |
| 32 MessageLoopProxyTraits> { | 32 // methods must be safe to call on any thread. Ownership semantics |
| 33 // for Executors are in general not clear, which is why the interface |
| 34 // itself is RefCountedThreadSafe. |
| 35 class BASE_EXPORT Executor |
| 36 : public RefCountedThreadSafe<Executor, ExecutorTraits> { |
| 33 public: | 37 public: |
| 34 // These methods are the same as in message_loop.h, but are guaranteed to | 38 // Submits the given task for execution. Returns true if the task |
| 35 // either post the Task to the MessageLoop (if it's still alive), or the task | 39 // may be executed immediately or at some point in the future, and |
| 36 // is discarded. | 40 // false if the task definitely will not be executed. |
| 37 // They return true iff the thread existed and the task was posted. Note that | |
| 38 // even if the task is posted, there's no guarantee that it will run; for | |
| 39 // example the target loop may already be quitting, or in the case of a | |
| 40 // delayed task a Quit message may preempt it in the message loop queue. | |
| 41 // Conversely, a return value of false is a guarantee the task will not run. | |
| 42 virtual bool PostTask(const tracked_objects::Location& from_here, | 41 virtual bool PostTask(const tracked_objects::Location& from_here, |
| 43 const base::Closure& task) = 0; | 42 const Closure& task) = 0; |
| 43 |
| 44 // TODO(akalin): Make Post*DelayedTask use TimeDelta instead. |
| 45 |
| 46 // Like PostTask, but tries to run the submitted task only after |
| 47 // |delay_ms| has passed. |
| 44 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 48 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 45 const base::Closure& task, | 49 const Closure& task, |
| 46 int64 delay_ms) = 0; | 50 int64 delay_ms) = 0; |
| 51 |
| 52 // Like PostTask, but guarantees that the submitted task will not |
| 53 // execute nested within an already-executing task. |
| 47 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, | 54 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, |
| 48 const base::Closure& task) = 0; | 55 const Closure& task) = 0; |
| 56 |
| 57 // Like PostNonNestableTask, but tries to run the submitted task |
| 58 // only after |delay_ms| has passed. |
| 49 virtual bool PostNonNestableDelayedTask( | 59 virtual bool PostNonNestableDelayedTask( |
| 50 const tracked_objects::Location& from_here, | 60 const tracked_objects::Location& from_here, |
| 51 const base::Closure& task, | 61 const Closure& task, |
| 52 int64 delay_ms) = 0; | 62 int64 delay_ms) = 0; |
| 53 | 63 |
| 54 // A method which checks if the caller is currently running in the thread that | 64 // Posts |task| on the current Executor. On completion, |reply| is |
| 55 // this proxy represents. | 65 // posted to the thread that called PostTaskAndReply(). Both |task| |
| 56 virtual bool BelongsToCurrentThread() = 0; | 66 // and |reply| are guaranteed to be deleted on the thread from which |
| 57 | 67 // PostTaskAndReply() is invoked. This allows objects that must be |
| 58 // Executes |task| on the given MessageLoopProxy. On completion, |reply| | 68 // deleted on the originating thread to be bound into the |task| and |
| 59 // is passed back to the MessageLoopProxy for the thread that called | 69 // |reply| Closures. In particular, it can be useful to use |
| 60 // PostTaskAndReply(). Both |task| and |reply| are guaranteed to be deleted | 70 // WeakPtr<> in the |reply| Closure so that the reply operation can |
| 61 // on the thread from which PostTaskAndReply() is invoked. This allows | 71 // be canceled. See the following pseudo-code: |
| 62 // objects that must be deleted on the originating thread to be bound into the | |
| 63 // |task| and |reply| Closures. In particular, it can be useful to use | |
| 64 // WeakPtr<> in the |reply| Closure so that the reply operation can be | |
| 65 // canceled. See the following pseudo-code: | |
| 66 // | 72 // |
| 67 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { | 73 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { |
| 68 // public: | 74 // public: |
| 69 // // Called to add data into a buffer. | 75 // // Called to add data into a buffer. |
| 70 // void AddData(void* buf, size_t length); | 76 // void AddData(void* buf, size_t length); |
| 71 // ... | 77 // ... |
| 72 // }; | 78 // }; |
| 73 // | 79 // |
| 74 // | 80 // |
| 75 // class DataLoader : public SupportsWeakPtr<DataLoader> { | 81 // class DataLoader : public SupportsWeakPtr<DataLoader> { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 93 // * Results of |task| are shared with |reply| by binding a shared argument | 99 // * Results of |task| are shared with |reply| by binding a shared argument |
| 94 // (a DataBuffer instance). | 100 // (a DataBuffer instance). |
| 95 // * The DataLoader object has no special thread safety. | 101 // * The DataLoader object has no special thread safety. |
| 96 // * The DataLoader object can be deleted while |task| is still running, | 102 // * The DataLoader object can be deleted while |task| is still running, |
| 97 // and the reply will cancel itself safely because it is bound to a | 103 // and the reply will cancel itself safely because it is bound to a |
| 98 // WeakPtr<>. | 104 // WeakPtr<>. |
| 99 bool PostTaskAndReply(const tracked_objects::Location& from_here, | 105 bool PostTaskAndReply(const tracked_objects::Location& from_here, |
| 100 const Closure& task, | 106 const Closure& task, |
| 101 const Closure& reply); | 107 const Closure& reply); |
| 102 | 108 |
| 109 // Submits a task to delete the given object. Returns true if the |
| 110 // object may be deleted immediately or at some point in the future, |
| 111 // and false if the object definitely will not be deleted. |
| 103 template <class T> | 112 template <class T> |
| 104 bool DeleteSoon(const tracked_objects::Location& from_here, | 113 bool DeleteSoon(const tracked_objects::Location& from_here, |
| 105 const T* object) { | 114 const T* object) { |
| 106 return subtle::DeleteHelperInternal<T, bool>::DeleteOnMessageLoop( | 115 return subtle::DeleteHelperInternal<T, bool>::DeleteOnExecutor( |
| 107 this, from_here, object); | 116 this, from_here, object); |
| 108 } | 117 } |
| 118 |
| 119 // Submits a task to release the given object. Returns true if the |
| 120 // object may be released immediately or at some point in the |
| 121 // future, and false if the object definitely will not be released. |
| 109 template <class T> | 122 template <class T> |
| 110 bool ReleaseSoon(const tracked_objects::Location& from_here, | 123 bool ReleaseSoon(const tracked_objects::Location& from_here, |
| 111 T* object) { | 124 T* object) { |
| 112 return subtle::ReleaseHelperInternal<T, bool>::ReleaseOnMessageLoop( | 125 return subtle::ReleaseHelperInternal<T, bool>::ReleaseOnExecutor( |
| 113 this, from_here, object); | 126 this, from_here, object); |
| 114 } | 127 } |
| 115 | 128 |
| 116 // Gets the MessageLoopProxy for the current message loop, creating one if | 129 protected: |
| 117 // needed. | 130 friend struct ExecutorTraits; |
| 118 static scoped_refptr<MessageLoopProxy> current(); | |
| 119 | 131 |
| 120 protected: | 132 Executor(); |
| 121 friend class RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits>; | 133 virtual ~Executor(); |
| 122 friend struct MessageLoopProxyTraits; | |
| 123 | 134 |
| 124 MessageLoopProxy(); | 135 // Called when this object should be destroyed. By default simply |
| 125 virtual ~MessageLoopProxy(); | 136 // deletes |this|, but can be overridden to do something else, like |
| 126 | 137 // delete on a certain thread. |
| 127 // Called when the proxy is about to be deleted. Subclasses can override this | |
| 128 // to provide deletion on specific threads. | |
| 129 virtual void OnDestruct() const; | 138 virtual void OnDestruct() const; |
| 130 | 139 |
| 131 private: | 140 private: |
| 132 template <class T, class R> friend class subtle::DeleteHelperInternal; | 141 template <class T, class R> friend class subtle::DeleteHelperInternal; |
| 133 template <class T, class R> friend class subtle::ReleaseHelperInternal; | 142 template <class T, class R> friend class subtle::ReleaseHelperInternal; |
| 143 |
| 134 bool DeleteSoonInternal(const tracked_objects::Location& from_here, | 144 bool DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 135 void(*deleter)(const void*), | 145 void(*deleter)(const void*), |
| 136 const void* object); | 146 const void* object); |
| 147 |
| 137 bool ReleaseSoonInternal(const tracked_objects::Location& from_here, | 148 bool ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 138 void(*releaser)(const void*), | 149 void(*releaser)(const void*), |
| 139 const void* object); | 150 const void* object); |
| 140 }; | 151 }; |
| 141 | 152 |
| 142 struct MessageLoopProxyTraits { | 153 struct BASE_EXPORT ExecutorTraits { |
| 143 static void Destruct(const MessageLoopProxy* proxy) { | 154 static void Destruct(const Executor* executor); |
| 144 proxy->OnDestruct(); | |
| 145 } | |
| 146 }; | 155 }; |
| 147 | 156 |
| 148 } // namespace base | 157 } // namespace base |
| 149 | 158 |
| 150 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 159 #endif // BASE_EXECUTOR_H_ |
| OLD | NEW |