simple_thread.h 9.0 KB

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