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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 size_t stack_size_; | 71 size_t stack_size_; |
72 }; | 72 }; |
73 | 73 |
74 // Create a SimpleThread. |options| should be used to manage any specific | 74 // Create a SimpleThread. |options| should be used to manage any specific |
75 // configuration involving the thread creation and management. | 75 // configuration involving the thread creation and management. |
76 // Every thread has a name, in the form of |name_prefix|/TID, for example | 76 // Every thread has a name, in the form of |name_prefix|/TID, for example |
77 // "my_thread/321". The thread will not be created until Start() is called. | 77 // "my_thread/321". The thread will not be created until Start() is called. |
78 explicit SimpleThread(const std::string& name_prefix); | 78 explicit SimpleThread(const std::string& name_prefix); |
79 SimpleThread(const std::string& name_prefix, const Options& options); | 79 SimpleThread(const std::string& name_prefix, const Options& options); |
80 | 80 |
81 virtual ~SimpleThread(); | 81 ~SimpleThread() override; |
82 | 82 |
83 virtual void Start(); | 83 virtual void Start(); |
84 virtual void Join(); | 84 virtual void Join(); |
85 | 85 |
86 // Subclasses should override the Run method. | 86 // Subclasses should override the Run method. |
87 virtual void Run() = 0; | 87 virtual void Run() = 0; |
88 | 88 |
89 // Return the thread name prefix, or "unnamed" if none was supplied. | 89 // Return the thread name prefix, or "unnamed" if none was supplied. |
90 std::string name_prefix() { return name_prefix_; } | 90 std::string name_prefix() { return name_prefix_; } |
91 | 91 |
92 // Return the completed name including TID, only valid after Start(). | 92 // Return the completed name including TID, only valid after Start(). |
93 std::string name() { return name_; } | 93 std::string name() { return name_; } |
94 | 94 |
95 // Return the thread id, only valid after Start(). | 95 // Return the thread id, only valid after Start(). |
96 PlatformThreadId tid() { return tid_; } | 96 PlatformThreadId tid() { return tid_; } |
97 | 97 |
98 // Return True if Start() has ever been called. | 98 // Return True if Start() has ever been called. |
99 bool HasBeenStarted(); | 99 bool HasBeenStarted(); |
100 | 100 |
101 // Return True if Join() has evern been called. | 101 // Return True if Join() has evern been called. |
102 bool HasBeenJoined() { return joined_; } | 102 bool HasBeenJoined() { return joined_; } |
103 | 103 |
104 // Overridden from PlatformThread::Delegate: | 104 // Overridden from PlatformThread::Delegate: |
105 virtual void ThreadMain() override; | 105 void ThreadMain() override; |
106 | 106 |
107 // Only set priorities with a careful understanding of the consequences. | 107 // Only set priorities with a careful understanding of the consequences. |
108 // This is meant for very limited use cases. | 108 // This is meant for very limited use cases. |
109 void SetThreadPriority(ThreadPriority priority) { | 109 void SetThreadPriority(ThreadPriority priority) { |
110 PlatformThread::SetThreadPriority(thread_, priority); | 110 PlatformThread::SetThreadPriority(thread_, priority); |
111 } | 111 } |
112 | 112 |
113 private: | 113 private: |
114 const std::string name_prefix_; | 114 const std::string name_prefix_; |
115 std::string name_; | 115 std::string name_; |
(...skipping 12 matching lines...) Expand all Loading... |
128 virtual ~Delegate() { } | 128 virtual ~Delegate() { } |
129 virtual void Run() = 0; | 129 virtual void Run() = 0; |
130 }; | 130 }; |
131 | 131 |
132 DelegateSimpleThread(Delegate* delegate, | 132 DelegateSimpleThread(Delegate* delegate, |
133 const std::string& name_prefix); | 133 const std::string& name_prefix); |
134 DelegateSimpleThread(Delegate* delegate, | 134 DelegateSimpleThread(Delegate* delegate, |
135 const std::string& name_prefix, | 135 const std::string& name_prefix, |
136 const Options& options); | 136 const Options& options); |
137 | 137 |
138 virtual ~DelegateSimpleThread(); | 138 ~DelegateSimpleThread() override; |
139 virtual void Run() override; | 139 void Run() override; |
| 140 |
140 private: | 141 private: |
141 Delegate* delegate_; | 142 Delegate* delegate_; |
142 }; | 143 }; |
143 | 144 |
144 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, | 145 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, |
145 // and then add jobs which will be dispatched to the threads. This is | 146 // and then add jobs which will be dispatched to the threads. This is |
146 // convenient when you have a lot of small work that you want done | 147 // convenient when you have a lot of small work that you want done |
147 // multi-threaded, but don't want to spawn a thread for each small bit of work. | 148 // multi-threaded, but don't want to spawn a thread for each small bit of work. |
148 // | 149 // |
149 // You just call AddWork() to add a delegate to the list of work to be done. | 150 // You just call AddWork() to add a delegate to the list of work to be done. |
150 // JoinAll() will make sure that all outstanding work is processed, and wait | 151 // JoinAll() will make sure that all outstanding work is processed, and wait |
151 // for everything to finish. You can reuse a pool, so you can call Start() | 152 // for everything to finish. You can reuse a pool, so you can call Start() |
152 // again after you've called JoinAll(). | 153 // again after you've called JoinAll(). |
153 class BASE_EXPORT DelegateSimpleThreadPool | 154 class BASE_EXPORT DelegateSimpleThreadPool |
154 : public DelegateSimpleThread::Delegate { | 155 : public DelegateSimpleThread::Delegate { |
155 public: | 156 public: |
156 typedef DelegateSimpleThread::Delegate Delegate; | 157 typedef DelegateSimpleThread::Delegate Delegate; |
157 | 158 |
158 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); | 159 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); |
159 virtual ~DelegateSimpleThreadPool(); | 160 ~DelegateSimpleThreadPool() override; |
160 | 161 |
161 // Start up all of the underlying threads, and start processing work if we | 162 // Start up all of the underlying threads, and start processing work if we |
162 // have any. | 163 // have any. |
163 void Start(); | 164 void Start(); |
164 | 165 |
165 // Make sure all outstanding work is finished, and wait for and destroy all | 166 // Make sure all outstanding work is finished, and wait for and destroy all |
166 // of the underlying threads in the pool. | 167 // of the underlying threads in the pool. |
167 void JoinAll(); | 168 void JoinAll(); |
168 | 169 |
169 // It is safe to AddWork() any time, before or after Start(). | 170 // It is safe to AddWork() any time, before or after Start(). |
170 // Delegate* should always be a valid pointer, NULL is reserved internally. | 171 // Delegate* should always be a valid pointer, NULL is reserved internally. |
171 void AddWork(Delegate* work, int repeat_count); | 172 void AddWork(Delegate* work, int repeat_count); |
172 void AddWork(Delegate* work) { | 173 void AddWork(Delegate* work) { |
173 AddWork(work, 1); | 174 AddWork(work, 1); |
174 } | 175 } |
175 | 176 |
176 // We implement the Delegate interface, for running our internal threads. | 177 // We implement the Delegate interface, for running our internal threads. |
177 virtual void Run() override; | 178 void Run() override; |
178 | 179 |
179 private: | 180 private: |
180 const std::string name_prefix_; | 181 const std::string name_prefix_; |
181 int num_threads_; | 182 int num_threads_; |
182 std::vector<DelegateSimpleThread*> threads_; | 183 std::vector<DelegateSimpleThread*> threads_; |
183 std::queue<Delegate*> delegates_; | 184 std::queue<Delegate*> delegates_; |
184 base::Lock lock_; // Locks delegates_ | 185 base::Lock lock_; // Locks delegates_ |
185 WaitableEvent dry_; // Not signaled when there is no work to do. | 186 WaitableEvent dry_; // Not signaled when there is no work to do. |
186 }; | 187 }; |
187 | 188 |
188 } // namespace base | 189 } // namespace base |
189 | 190 |
190 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 191 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
OLD | NEW |