| 6 |
| // Chrome's message-loop based Thread abstraction, and if you are a |
| 7 | // thread running in the browser, there will likely be assumptions |
| 8 | // that your thread will have an associated message loop. |
| 9 | // |
| 10 | // This is a simple thread interface that backs to a native operating system |
| 11 | // thread. You should use this only when you want a thread that does not have |
| 12 | // an associated MessageLoop. Unittesting is the best example of this. |
| 13 | // |
| 14 | // The simplest interface to use is DelegateSimpleThread, which will create |
| 15 | // a new thread, and execute the Delegate's virtual Run() in this new thread |
| 16 | // until it has completed, exiting the thread. |
| 17 | // |
| 18 | // NOTE: You *MUST* call Join on the thread to clean up the underlying thread |
| 19 | // resources. You are also responsible for destructing the SimpleThread object. |
| 20 | // It is invalid to destroy a SimpleThread while it is running, or without |
| 21 | // Start() having been called (and a thread never created). The Delegate |
| 22 | // object should live as long as a DelegateSimpleThread. |
| 23 | // |
| 24 | // Thread Safety: A SimpleThread is not completely thread safe. It is safe to |
| 25 | // access it from the creating thread or from the newly created thread. This |
| 26 | // implies that the creator thread should be the thread that calls Join. |
| 27 | // |
| 28 | // Example: |
| 29 | // class MyThreadRunner : public DelegateSimpleThread::Delegate { ... }; |
| 30 | // MyThreadRunner runner; |
| 31 | // DelegateSimpleThread thread(&runner, "good_name_here"); |
| 32 | // thread.Start(); |
| 33 | // // Start will return after the Thread has been successfully started and |
| 34 | // // initialized. The newly created thread will invoke runner->Run(), and |
| 35 | // // run until it returns. |
| 36 | // thread.Join(); // Wait until the thread has exited. You *MUST* Join! |
| 37 | // // The SimpleThread object is still valid, however you may not call Join |
| 38 | // // or Start again. |
| 39 |
| 40 | #ifndef BASE_THREADING_SIMPLE_THREAD_H_ |
| 41 | #define BASE_THREADING_SIMPLE_THREAD_H_ |
| 42 |
| 43 | #include <stddef.h> |
| 44 |
| 45 | #include <string> |
| 46 | #include <vector> |
| 47 |
| 48 | #include "base/base_export.h" |
| 49 | #include "base/compiler_specific.h" |
| 50 | #include "base/containers/queue.h" |
| 51 | #include "base/macros.h" |
| 52 | #include "base/synchronization/lock.h" |
| 53 | #include "base/synchronization/waitable_event.h" |
| 54 | #include "base/threading/platform_thread.h" |
| 55 |
| 56 | namespace base { |
| 57 |
| 58 | // This is the base SimpleThread. You can derive from it and implement the |
| 59 | // virtual Run method, or you can use the DelegateSimpleThread interface. |
| 60 | class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
| 61 | public: |
| 62 | struct BASE_EXPORT Options { |
| 63 | public: |
| 64 | Options() = default; |
| 65 | explicit Options(ThreadPriority priority_in) : priority(priority_in) {} |
| 66 | ~Options() = default; |
| 67 |
| 68 | // Allow copies. |
| 69 | Options(const Options& other) = default; |
| 70 | Options& operator=(const Options& other) = default; |
| 71 |
| 72 | // A custom stack size, or 0 for the system default. |
| 73 | size_t stack_size = 0; |
| 74 |
| 75 | ThreadPriority priority = ThreadPriority::NORMAL; |
| 76 |
| 77 | // If false, the underlying thread's PlatformThreadHandle will not be kept |
| 78 | // around and as such the SimpleThread instance will not be Join()able and |
| 79 | // must not be deleted before Run() is invoked. After that, it's up to |
| 80 | // the subclass to determine when it is safe to delete itself. |
| 81 | bool joinable = true; |
| 82 | }; |
| 83 |
| 84 | // Creates a SimpleThread. |options| should be used to manage any specific |
| 85 | // configuration involving the thread creation and management. |
| 86 | // Every thread has a name, which is a display string to identify the thread. |
| 87 | // The thread will not be created until Start() is called. |
| 88 | explicit SimpleThread(const std::string& name); |
| 89 | SimpleThread(const std::string& name, const Options& options); |
| 90 |
| 91 | ~SimpleThread() override; |
| 92 |
| 93 | // Starts the thread and returns only after the thread has started and |
| 94 | // initialized (i.e. ThreadMain() has been called). |
| 95 | void Start(); |
| 96 |
| 97 | // Joins the thread. If StartAsync() was used to start the thread, then this |
| 98 | // first waits for the thread to start cleanly, then it joins. |
| 99 | void Join(); |
| 100 |
| 101 | // Starts the thread, but returns immediately, without waiting for the thread |
| 102 | // to have initialized first (i.e. this does not wait for ThreadMain() to have |
| 103 | // been run first). |
| 104 | void StartAsync(); |
| 105 |
| 106 | // Subclasses should override the Run method. |
| 107 | virtual void Run() = 0; |
| 108 |
| 109 | // Returns the thread id, only valid after the thread has started. If the |
| 110 | // thread was started using Start(), then this will be valid after the call to |
| 111 | // Start(). If StartAsync() was used to start the thread, then this must not |
| 112 | // be called before HasBeenStarted() returns True. |
| 113 | PlatformThreadId tid(); |
| 114 |
| 115 | // Returns True if the thread has been started and initialized (i.e. if |
| 116 | // ThreadMain() has run). If the thread was started with StartAsync(), but it |
| 117 | // hasn't been initialized yet (i.e. ThreadMain() has not run), then this will |
| 118 | // return False. |
| 119 | bool HasBeenStarted(); |
| 120 |
| 121 | // Returns True if Join() has ever been called. |
| 122 | bool HasBeenJoined() const { return joined_; } |
| 123 |
| 124 | // Returns true if Start() or StartAsync() has been called. |
| 125 | bool HasStartBeenAttempted() { return start_called_; } |
| 126 |
| 127 | // Overridden from PlatformThread::Delegate: |
| 128 | void ThreadMain() override; |
| 129 |
| 130 | private: |
| 131 | // This is called just before the thread is started. This is called regardless |
| 132 | // of whether Start() or StartAsync() is used to start the thread. |
| 133 | virtual void BeforeStart() {} |
| 134 |
| 135 | // This is called just after the thread has been initialized and just before |
| 136 | // Run() is called. This is called on the newly started thread. |
| 137 | virtual void BeforeRun() {} |
| 138 |
| 139 | // This is called just before the thread is joined. The thread is started and |
| 140 | // has been initialized before this is called. |
| 141 | virtual void BeforeJoin() {} |
| 142 |
| 143 | const std::string name_; |
| 144 | const Options options_; |
| 145 | PlatformThreadHandle thread_; // PlatformThread handle, reset after Join. |
| 146 | WaitableEvent event_; // Signaled if Start() was ever called. |
| 147 | PlatformThreadId tid_ = kInvalidThreadId; // The backing thread's id. |
| 148 | bool joined_ = false; // True if Join has been called. |
| 149 | // Set to true when the platform-thread creation has started. |
| 150 | bool start_called_ = false; |
| 151 |
| 152 | DISALLOW_COPY_AND_ASSIGN(SimpleThread); |
| 153 | }; |
| 154 |
| 155 | // A SimpleThread which delegates Run() to its Delegate. Non-joinable |
| 156 | // DelegateSimpleThread are safe to delete after Run() was invoked, their |
| 157 | // Delegates are also safe to delete after that point from this class' point of |
| 158 | // view (although implementations must of course make sure that Run() will not |
| 159 | // use their Delegate's member state after its deletion). |
| 160 | class BASE_EXPORT DelegateSimpleThread : public SimpleThread { |
| 161 | public: |
| 162 | class BASE_EXPORT Delegate { |
| 163 | public: |
| 164 | virtual ~Delegate() = default; |
| 165 | virtual void Run() = 0; |
| 166 | }; |
| 167 |
| 168 | DelegateSimpleThread(Delegate* delegate, |
| 169 | const std::string& name_prefix); |
| 170 | DelegateSimpleThread(Delegate* delegate, |
| 171 | const std::string& name_prefix, |
| 172 | const Options& options); |
| 173 |
| 174 | ~DelegateSimpleThread() override; |
| 175 | void Run() override; |
| 176 |
| 177 | private: |
| 178 | Delegate* delegate_; |
| 179 |
| 180 | DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThread); |
| 181 | }; |
| 182 |
| 183 | // DelegateSimpleThreadPool allows you to start up a fixed number of threads, |
| 184 | // and then add jobs which will be dispatched to the threads. This is |
| 185 | // convenient when you have a lot of small work that you want done |
| 186 | // multi-threaded, but don't want to spawn a thread for each small bit of work. |
| 187 | // |
| 188 | // You just call AddWork() to add a delegate to the list of work to be done. |
| 189 | // JoinAll() will make sure that all outstanding work is processed, and wait |
| 190 | // for everything to finish. You can reuse a pool, so you can call Start() |
| 191 | // again after you've called JoinAll(). |
| 192 | class BASE_EXPORT DelegateSimpleThreadPool |
| 193 | : public DelegateSimpleThread::Delegate { |
| 194 | public: |
| 195 | typedef DelegateSimpleThread::Delegate Delegate; |
| 196 |
| 197 | DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); |
| 198 | ~DelegateSimpleThreadPool() override; |
| 199 |
| 200 | // Start up all of the underlying threads, and start processing work if we |
| 201 | // have any. |
| 202 | void Start(); |
| 203 |
| 204 | // Make sure all outstanding work is finished, and wait for and destroy all |
| 205 | // of the underlying threads in the pool. |
| 206 | void JoinAll(); |
| 207 |
| 208 | // It is safe to AddWork() any time, before or after Start(). |
| 209 | // Delegate* should always be a valid pointer, NULL is reserved internally. |
| 210 | void AddWork(Delegate* work, int repeat_count); |
| 211 | void AddWork(Delegate* work) { |
| 212 | AddWork(work, 1); |
| 213 | } |
| 214 |
| 215 | // We implement the Delegate interface, for running our internal threads. |
| 216 | void Run() override; |
| 217 |
| 218 | private: |
| 219 | const std::string name_prefix_; |
| 220 | int num_threads_; |
| 221 | std::vector<DelegateSimpleThread*> threads_; |
| 222 | base::queue<Delegate*> delegates_; |
| 223 | base::Lock lock_; // Locks delegates_ |
| 224 | WaitableEvent dry_; // Not signaled when there is no work to do. |
| 225 |
| 226 | DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); |
| 227 | }; |
| 228 |
| 229 | } // namespace base |
| 230 |
| 231 | #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
| 232 |