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" | |
51 #include "base/synchronization/lock.h" | 52 #include "base/synchronization/lock.h" |
52 #include "base/synchronization/waitable_event.h" | 53 #include "base/synchronization/waitable_event.h" |
53 #include "base/threading/platform_thread.h" | 54 #include "base/threading/platform_thread.h" |
54 | 55 |
55 namespace base { | 56 namespace base { |
56 | 57 |
57 // This is the base SimpleThread. You can derive from it and implement the | 58 // This is the base SimpleThread. You can derive from it and implement the |
58 // virtual Run method, or you can use the DelegateSimpleThread interface. | 59 // virtual Run method, or you can use the DelegateSimpleThread interface. |
59 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { | 60 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
60 public: | 61 public: |
61 class BASE_EXPORT Options { | 62 struct BASE_EXPORT Options { |
62 public: | 63 public: |
63 Options() : stack_size_(0), priority_(ThreadPriority::NORMAL) {} | 64 Options() = default; |
64 explicit Options(ThreadPriority priority) | 65 explicit Options(ThreadPriority priority_in) : priority(priority_in) {} |
65 : stack_size_(0), priority_(priority) {} | 66 ~Options() = default; |
66 ~Options() {} | |
67 | 67 |
68 // We use the standard compiler-supplied copy constructor. | 68 // Allow copies. |
69 Options(const Options& other) = default; | |
70 Options& operator=(const Options& other) = default; | |
69 | 71 |
70 // A custom stack size, or 0 for the system default. | 72 // A custom stack size, or 0 for the system default. |
71 void set_stack_size(size_t size) { stack_size_ = size; } | 73 size_t stack_size = 0; |
72 size_t stack_size() const { return stack_size_; } | |
73 | 74 |
74 // A custom thread priority. | 75 ThreadPriority priority = ThreadPriority::NORMAL; |
75 void set_priority(ThreadPriority priority) { priority_ = priority; } | 76 |
76 ThreadPriority priority() const { return priority_; } | 77 // If false, the underlying thread's PlatformThreadHandle will not be kept |
77 private: | 78 // around and as such the SimpleThread instance will not be Join()able and |
78 size_t stack_size_; | 79 // must not be deleted before Run() is invoked. After that, it's up to |
79 ThreadPriority priority_; | 80 // the subclass to determine when it is safe to delete itself. |
81 bool joinable = true; | |
80 }; | 82 }; |
81 | 83 |
82 // Create a SimpleThread. |options| should be used to manage any specific | 84 // Create a SimpleThread. |options| should be used to manage any specific |
83 // configuration involving the thread creation and management. | 85 // configuration involving the thread creation and management. |
84 // Every thread has a name, in the form of |name_prefix|/TID, for example | 86 // Every thread has a name, in the form of |name_prefix|/TID, for example |
85 // "my_thread/321". The thread will not be created until Start() is called. | 87 // "my_thread/321". The thread will not be created until Start() is called. |
86 explicit SimpleThread(const std::string& name_prefix); | 88 explicit SimpleThread(const std::string& name_prefix); |
87 SimpleThread(const std::string& name_prefix, const Options& options); | 89 SimpleThread(const std::string& name_prefix, const Options& options); |
88 | 90 |
89 ~SimpleThread() override; | 91 ~SimpleThread() override; |
90 | 92 |
91 virtual void Start(); | 93 virtual void Start(); |
92 virtual void Join(); | 94 virtual void Join(); |
93 | 95 |
94 // Subclasses should override the Run method. | 96 // Subclasses should override the Run method. |
95 virtual void Run() = 0; | 97 virtual void Run() = 0; |
96 | 98 |
97 // Return the thread name prefix, or "unnamed" if none was supplied. | 99 // Return the thread name prefix, or "unnamed" if none was supplied. |
98 std::string name_prefix() { return name_prefix_; } | 100 std::string name_prefix() { return name_prefix_; } |
99 | 101 |
100 // Return the completed name including TID, only valid after Start(). | 102 // Return the completed name including TID, only valid after Start(). |
101 std::string name() { return name_; } | 103 std::string name() { return name_; } |
102 | 104 |
103 // Return the thread id, only valid after Start(). | 105 // Return the thread id, only valid after Start(). |
104 PlatformThreadId tid() { return tid_; } | 106 PlatformThreadId tid() { return tid_; } |
105 | 107 |
106 // Return True if Start() has ever been called. | 108 // Return True if Start() has ever been called. |
107 bool HasBeenStarted(); | 109 bool HasBeenStarted(); |
108 | 110 |
109 // Return True if Join() has evern been called. | 111 // Return True if Join() has ever been called. |
110 bool HasBeenJoined() { return joined_; } | 112 bool HasBeenJoined() { return joined_; } |
111 | 113 |
112 // Overridden from PlatformThread::Delegate: | 114 // Overridden from PlatformThread::Delegate: |
113 void ThreadMain() override; | 115 void ThreadMain() override; |
114 | 116 |
115 private: | 117 private: |
116 const std::string name_prefix_; | 118 const std::string name_prefix_; |
117 std::string name_; | 119 std::string name_; |
118 const Options options_; | 120 const Options options_; |
119 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! | 121 PlatformThreadHandle thread_; // PlatformThread handle, reset after Join. |
120 WaitableEvent event_; // Signaled if Start() was ever called. | 122 WaitableEvent event_; // Signaled if Start() was ever called. |
121 PlatformThreadId tid_; // The backing thread's id. | 123 PlatformThreadId tid_ = kInvalidThreadId; // The backing thread's id. |
122 bool joined_; // True if Join has been called. | 124 bool joined_ = false; // True if Join has been called. |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(SimpleThread); | |
123 }; | 127 }; |
124 | 128 |
129 // A SimpleThread which delegates Run() to its Delegate. Non-joinable | |
130 // DelegateSimpleThread are safe to delete after Start(), their Delegates are | |
131 // also safe to delete after Start() from this class' point of view (although | |
132 // implementations must of course make sure that Run() will not use member state | |
133 // after this deletion). | |
125 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { | 134 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { |
126 public: | 135 public: |
127 class BASE_EXPORT Delegate { | 136 class BASE_EXPORT Delegate { |
128 public: | 137 public: |
129 Delegate() { } | 138 Delegate() = default; |
danakj
2016/08/12 23:12:25
this not needed at all right?
gab
2016/08/12 23:29:50
Done.
| |
130 virtual ~Delegate() { } | 139 virtual ~Delegate() = default; |
131 virtual void Run() = 0; | 140 virtual void Run() = 0; |
141 | |
142 private: | |
143 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
danakj
2016/08/12 23:12:25
not needed, just an interface
gab
2016/08/12 23:29:50
Done.
| |
132 }; | 144 }; |
133 | 145 |
134 DelegateSimpleThread(Delegate* delegate, | 146 DelegateSimpleThread(Delegate* delegate, |
135 const std::string& name_prefix); | 147 const std::string& name_prefix); |
136 DelegateSimpleThread(Delegate* delegate, | 148 DelegateSimpleThread(Delegate* delegate, |
137 const std::string& name_prefix, | 149 const std::string& name_prefix, |
138 const Options& options); | 150 const Options& options); |
139 | 151 |
140 ~DelegateSimpleThread() override; | 152 ~DelegateSimpleThread() override; |
141 void Run() override; | 153 void Run() override; |
142 | 154 |
143 private: | 155 private: |
144 Delegate* delegate_; | 156 Delegate* delegate_; |
157 | |
158 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThread); | |
145 }; | 159 }; |
146 | 160 |
147 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, | 161 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, |
148 // and then add jobs which will be dispatched to the threads. This is | 162 // and then add jobs which will be dispatched to the threads. This is |
149 // convenient when you have a lot of small work that you want done | 163 // convenient when you have a lot of small work that you want done |
150 // multi-threaded, but don't want to spawn a thread for each small bit of work. | 164 // multi-threaded, but don't want to spawn a thread for each small bit of work. |
151 // | 165 // |
152 // You just call AddWork() to add a delegate to the list of work to be done. | 166 // You just call AddWork() to add a delegate to the list of work to be done. |
153 // JoinAll() will make sure that all outstanding work is processed, and wait | 167 // JoinAll() will make sure that all outstanding work is processed, and wait |
154 // for everything to finish. You can reuse a pool, so you can call Start() | 168 // for everything to finish. You can reuse a pool, so you can call Start() |
(...skipping 24 matching lines...) Expand all Loading... | |
179 // We implement the Delegate interface, for running our internal threads. | 193 // We implement the Delegate interface, for running our internal threads. |
180 void Run() override; | 194 void Run() override; |
181 | 195 |
182 private: | 196 private: |
183 const std::string name_prefix_; | 197 const std::string name_prefix_; |
184 int num_threads_; | 198 int num_threads_; |
185 std::vector<DelegateSimpleThread*> threads_; | 199 std::vector<DelegateSimpleThread*> threads_; |
186 std::queue<Delegate*> delegates_; | 200 std::queue<Delegate*> delegates_; |
187 base::Lock lock_; // Locks delegates_ | 201 base::Lock lock_; // Locks delegates_ |
188 WaitableEvent dry_; // Not signaled when there is no work to do. | 202 WaitableEvent dry_; // Not signaled when there is no work to do. |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); | |
189 }; | 205 }; |
190 | 206 |
191 } // namespace base | 207 } // namespace base |
192 | 208 |
193 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 209 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
OLD | NEW |