| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 #include "base/synchronization/waitable_event.h" | 52 #include "base/synchronization/waitable_event.h" |
| 53 | 53 |
| 54 namespace base { | 54 namespace base { |
| 55 | 55 |
| 56 // This is the base SimpleThread. You can derive from it and implement the | 56 // This is the base SimpleThread. You can derive from it and implement the |
| 57 // virtual Run method, or you can use the DelegateSimpleThread interface. | 57 // virtual Run method, or you can use the DelegateSimpleThread interface. |
| 58 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { | 58 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
| 59 public: | 59 public: |
| 60 class BASE_EXPORT Options { | 60 class BASE_EXPORT Options { |
| 61 public: | 61 public: |
| 62 Options() : stack_size_(0), priority_(ThreadPriority::NORMAL) { } | 62 Options() : stack_size_(0), priority_(ThreadPriority::NORMAL) {} |
| 63 ~Options() { } | 63 explicit Options(ThreadPriority priority) |
| 64 : stack_size_(0), priority_(priority) {} |
| 65 ~Options() {} |
| 64 | 66 |
| 65 // We use the standard compiler-supplied copy constructor. | 67 // We use the standard compiler-supplied copy constructor. |
| 66 | 68 |
| 67 // A custom stack size, or 0 for the system default. | 69 // A custom stack size, or 0 for the system default. |
| 68 void set_stack_size(size_t size) { stack_size_ = size; } | 70 void set_stack_size(size_t size) { stack_size_ = size; } |
| 69 size_t stack_size() const { return stack_size_; } | 71 size_t stack_size() const { return stack_size_; } |
| 70 | 72 |
| 71 // A custom thread priority. | 73 // A custom thread priority. |
| 72 void set_priority(ThreadPriority priority) { priority_ = priority; } | 74 void set_priority(ThreadPriority priority) { priority_ = priority; } |
| 73 ThreadPriority priority() const { return priority_; } | 75 ThreadPriority priority() const { return priority_; } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 102 | 104 |
| 103 // Return True if Start() has ever been called. | 105 // Return True if Start() has ever been called. |
| 104 bool HasBeenStarted(); | 106 bool HasBeenStarted(); |
| 105 | 107 |
| 106 // Return True if Join() has evern been called. | 108 // Return True if Join() has evern been called. |
| 107 bool HasBeenJoined() { return joined_; } | 109 bool HasBeenJoined() { return joined_; } |
| 108 | 110 |
| 109 // Overridden from PlatformThread::Delegate: | 111 // Overridden from PlatformThread::Delegate: |
| 110 void ThreadMain() override; | 112 void ThreadMain() override; |
| 111 | 113 |
| 112 // Only set priorities with a careful understanding of the consequences. | |
| 113 // This is meant for very limited use cases. | |
| 114 void SetThreadPriority(ThreadPriority priority) { | |
| 115 PlatformThread::SetThreadPriority(thread_, priority); | |
| 116 } | |
| 117 | |
| 118 private: | 114 private: |
| 119 const std::string name_prefix_; | 115 const std::string name_prefix_; |
| 120 std::string name_; | 116 std::string name_; |
| 121 const Options options_; | 117 const Options options_; |
| 122 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! | 118 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! |
| 123 WaitableEvent event_; // Signaled if Start() was ever called. | 119 WaitableEvent event_; // Signaled if Start() was ever called. |
| 124 PlatformThreadId tid_; // The backing thread's id. | 120 PlatformThreadId tid_; // The backing thread's id. |
| 125 bool joined_; // True if Join has been called. | 121 bool joined_; // True if Join has been called. |
| 126 }; | 122 }; |
| 127 | 123 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 int num_threads_; | 183 int num_threads_; |
| 188 std::vector<DelegateSimpleThread*> threads_; | 184 std::vector<DelegateSimpleThread*> threads_; |
| 189 std::queue<Delegate*> delegates_; | 185 std::queue<Delegate*> delegates_; |
| 190 base::Lock lock_; // Locks delegates_ | 186 base::Lock lock_; // Locks delegates_ |
| 191 WaitableEvent dry_; // Not signaled when there is no work to do. | 187 WaitableEvent dry_; // Not signaled when there is no work to do. |
| 192 }; | 188 }; |
| 193 | 189 |
| 194 } // namespace base | 190 } // namespace base |
| 195 | 191 |
| 196 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 192 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
| OLD | NEW |