| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // WARNING: You should probably be using Thread (thread.h) instead. Thread is | 5 // WARNING: You should probably be using Thread (thread.h) instead. Thread is |
| 6 // Chrome's message-loop based Thread abstraction, and if you are a | 6 // Chrome's message-loop based Thread abstraction, and if you are a |
| 7 // thread running in the browser, there will likely be assumptions | 7 // thread running in the browser, there will likely be assumptions |
| 8 // that your thread will have an associated message loop. | 8 // that your thread will have an associated message loop. |
| 9 // | 9 // |
| 10 // This is a simple thread interface that backs to a native operating system | 10 // This is a simple thread interface that backs to a native operating system |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 #define BASE_THREADING_SIMPLE_THREAD_H_ | 41 #define BASE_THREADING_SIMPLE_THREAD_H_ |
| 42 | 42 |
| 43 #include <stddef.h> | 43 #include <stddef.h> |
| 44 | 44 |
| 45 #include <queue> | 45 #include <queue> |
| 46 #include <string> | 46 #include <string> |
| 47 #include <vector> | 47 #include <vector> |
| 48 | 48 |
| 49 #include "base/base_export.h" | 49 #include "base/base_export.h" |
| 50 #include "base/compiler_specific.h" | 50 #include "base/compiler_specific.h" |
| 51 #include "base/macros.h" | |
| 52 #include "base/synchronization/lock.h" | 51 #include "base/synchronization/lock.h" |
| 53 #include "base/synchronization/waitable_event.h" | 52 #include "base/synchronization/waitable_event.h" |
| 54 #include "base/threading/platform_thread.h" | 53 #include "base/threading/platform_thread.h" |
| 55 | 54 |
| 56 namespace base { | 55 namespace base { |
| 57 | 56 |
| 58 // This is the base SimpleThread. You can derive from it and implement the | 57 // This is the base SimpleThread. You can derive from it and implement the |
| 59 // virtual Run method, or you can use the DelegateSimpleThread interface. | 58 // virtual Run method, or you can use the DelegateSimpleThread interface. |
| 60 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { | 59 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
| 61 public: | 60 public: |
| 62 struct BASE_EXPORT Options { | 61 class BASE_EXPORT Options { |
| 63 public: | 62 public: |
| 64 Options() = default; | 63 Options() : stack_size_(0), priority_(ThreadPriority::NORMAL) {} |
| 65 explicit Options(ThreadPriority priority_in) : priority(priority_in) {} | 64 explicit Options(ThreadPriority priority) |
| 66 ~Options() = default; | 65 : stack_size_(0), priority_(priority) {} |
| 66 ~Options() {} |
| 67 | 67 |
| 68 // Allow copies. | 68 // We use the standard compiler-supplied copy constructor. |
| 69 Options(const Options& other) = default; | |
| 70 Options& operator=(const Options& other) = default; | |
| 71 | 69 |
| 72 // A custom stack size, or 0 for the system default. | 70 // A custom stack size, or 0 for the system default. |
| 73 size_t stack_size = 0; | 71 void set_stack_size(size_t size) { stack_size_ = size; } |
| 72 size_t stack_size() const { return stack_size_; } |
| 74 | 73 |
| 75 ThreadPriority priority = ThreadPriority::NORMAL; | 74 // A custom thread priority. |
| 76 | 75 void set_priority(ThreadPriority priority) { priority_ = priority; } |
| 77 // If false, the thread's PlatformThreadHandle will not be kept around and | 76 ThreadPriority priority() const { return priority_; } |
| 78 // the SimpleThread instance will not be required to be Join()'ed before | 77 private: |
| 79 // being destroyed | 78 size_t stack_size_; |
| 80 bool joinable = true; | 79 ThreadPriority priority_; |
| 81 }; | 80 }; |
| 82 | 81 |
| 83 // Create a SimpleThread. |options| should be used to manage any specific | 82 // Create a SimpleThread. |options| should be used to manage any specific |
| 84 // configuration involving the thread creation and management. | 83 // configuration involving the thread creation and management. |
| 85 // Every thread has a name, in the form of |name_prefix|/TID, for example | 84 // Every thread has a name, in the form of |name_prefix|/TID, for example |
| 86 // "my_thread/321". The thread will not be created until Start() is called. | 85 // "my_thread/321". The thread will not be created until Start() is called. |
| 87 explicit SimpleThread(const std::string& name_prefix); | 86 explicit SimpleThread(const std::string& name_prefix); |
| 88 SimpleThread(const std::string& name_prefix, const Options& options); | 87 SimpleThread(const std::string& name_prefix, const Options& options); |
| 89 | 88 |
| 90 ~SimpleThread() override; | 89 ~SimpleThread() override; |
| 91 | 90 |
| 92 virtual void Start(); | 91 virtual void Start(); |
| 93 virtual void Join(); | 92 virtual void Join(); |
| 94 | 93 |
| 95 // Subclasses should override the Run method. | 94 // Subclasses should override the Run method. |
| 96 virtual void Run() = 0; | 95 virtual void Run() = 0; |
| 97 | 96 |
| 98 // Return the thread name prefix, or "unnamed" if none was supplied. | 97 // Return the thread name prefix, or "unnamed" if none was supplied. |
| 99 std::string name_prefix() { return name_prefix_; } | 98 std::string name_prefix() { return name_prefix_; } |
| 100 | 99 |
| 101 // Return the completed name including TID, only valid after Start(). | 100 // Return the completed name including TID, only valid after Start(). |
| 102 std::string name() { return name_; } | 101 std::string name() { return name_; } |
| 103 | 102 |
| 104 // Return the thread id, only valid after Start(). | 103 // Return the thread id, only valid after Start(). |
| 105 PlatformThreadId tid() { return tid_; } | 104 PlatformThreadId tid() { return tid_; } |
| 106 | 105 |
| 107 // Return True if Start() has ever been called. | 106 // Return True if Start() has ever been called. |
| 108 bool HasBeenStarted(); | 107 bool HasBeenStarted(); |
| 109 | 108 |
| 110 // Return True if Join() has ever been called. | 109 // Return True if Join() has evern been called. |
| 111 bool HasBeenJoined() { return joined_; } | 110 bool HasBeenJoined() { return joined_; } |
| 112 | 111 |
| 113 // Overridden from PlatformThread::Delegate: | 112 // Overridden from PlatformThread::Delegate: |
| 114 void ThreadMain() override; | 113 void ThreadMain() override; |
| 115 | 114 |
| 116 private: | 115 private: |
| 117 const std::string name_prefix_; | 116 const std::string name_prefix_; |
| 118 std::string name_; | 117 std::string name_; |
| 119 const Options options_; | 118 const Options options_; |
| 120 PlatformThreadHandle thread_; // PlatformThread handle, reset after Join. | 119 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! |
| 121 WaitableEvent event_; // Signaled if Start() was ever called. | 120 WaitableEvent event_; // Signaled if Start() was ever called. |
| 122 PlatformThreadId tid_ = kInvalidThreadId; // The backing thread's id. | 121 PlatformThreadId tid_; // The backing thread's id. |
| 123 bool joined_ = false; // True if Join has been called. | 122 bool joined_; // True if Join has been called. |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(SimpleThread); | |
| 126 }; | 123 }; |
| 127 | 124 |
| 128 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { | 125 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { |
| 129 public: | 126 public: |
| 130 class BASE_EXPORT Delegate { | 127 class BASE_EXPORT Delegate { |
| 131 public: | 128 public: |
| 132 Delegate() { } | 129 Delegate() { } |
| 133 virtual ~Delegate() { } | 130 virtual ~Delegate() { } |
| 134 virtual void Run() = 0; | 131 virtual void Run() = 0; |
| 135 }; | 132 }; |
| 136 | 133 |
| 137 DelegateSimpleThread(Delegate* delegate, | 134 DelegateSimpleThread(Delegate* delegate, |
| 138 const std::string& name_prefix); | 135 const std::string& name_prefix); |
| 139 DelegateSimpleThread(Delegate* delegate, | 136 DelegateSimpleThread(Delegate* delegate, |
| 140 const std::string& name_prefix, | 137 const std::string& name_prefix, |
| 141 const Options& options); | 138 const Options& options); |
| 142 | 139 |
| 143 ~DelegateSimpleThread() override; | 140 ~DelegateSimpleThread() override; |
| 144 void Run() override; | 141 void Run() override; |
| 145 | 142 |
| 146 private: | 143 private: |
| 147 Delegate* delegate_; | 144 Delegate* delegate_; |
| 148 | |
| 149 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThread); | |
| 150 }; | 145 }; |
| 151 | 146 |
| 152 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, | 147 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, |
| 153 // and then add jobs which will be dispatched to the threads. This is | 148 // and then add jobs which will be dispatched to the threads. This is |
| 154 // convenient when you have a lot of small work that you want done | 149 // convenient when you have a lot of small work that you want done |
| 155 // multi-threaded, but don't want to spawn a thread for each small bit of work. | 150 // multi-threaded, but don't want to spawn a thread for each small bit of work. |
| 156 // | 151 // |
| 157 // You just call AddWork() to add a delegate to the list of work to be done. | 152 // You just call AddWork() to add a delegate to the list of work to be done. |
| 158 // JoinAll() will make sure that all outstanding work is processed, and wait | 153 // JoinAll() will make sure that all outstanding work is processed, and wait |
| 159 // for everything to finish. You can reuse a pool, so you can call Start() | 154 // for everything to finish. You can reuse a pool, so you can call Start() |
| (...skipping 24 matching lines...) Expand all Loading... |
| 184 // We implement the Delegate interface, for running our internal threads. | 179 // We implement the Delegate interface, for running our internal threads. |
| 185 void Run() override; | 180 void Run() override; |
| 186 | 181 |
| 187 private: | 182 private: |
| 188 const std::string name_prefix_; | 183 const std::string name_prefix_; |
| 189 int num_threads_; | 184 int num_threads_; |
| 190 std::vector<DelegateSimpleThread*> threads_; | 185 std::vector<DelegateSimpleThread*> threads_; |
| 191 std::queue<Delegate*> delegates_; | 186 std::queue<Delegate*> delegates_; |
| 192 base::Lock lock_; // Locks delegates_ | 187 base::Lock lock_; // Locks delegates_ |
| 193 WaitableEvent dry_; // Not signaled when there is no work to do. | 188 WaitableEvent dry_; // Not signaled when there is no work to do. |
| 194 | |
| 195 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); | |
| 196 }; | 189 }; |
| 197 | 190 |
| 198 } // namespace base | 191 } // namespace base |
| 199 | 192 |
| 200 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 193 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
| OLD | NEW |