| 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 29 matching lines...) Expand all Loading... |
| 40 #ifndef BASE_THREADING_SIMPLE_THREAD_H_ | 40 #ifndef BASE_THREADING_SIMPLE_THREAD_H_ |
| 41 #define BASE_THREADING_SIMPLE_THREAD_H_ | 41 #define BASE_THREADING_SIMPLE_THREAD_H_ |
| 42 #pragma once | 42 #pragma once |
| 43 | 43 |
| 44 #include <string> | 44 #include <string> |
| 45 #include <queue> | 45 #include <queue> |
| 46 #include <vector> | 46 #include <vector> |
| 47 | 47 |
| 48 #include "base/base_export.h" | 48 #include "base/base_export.h" |
| 49 #include "base/basictypes.h" | 49 #include "base/basictypes.h" |
| 50 #include "base/compiler_specific.h" |
| 50 #include "base/threading/platform_thread.h" | 51 #include "base/threading/platform_thread.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 | 54 |
| 54 namespace base { | 55 namespace base { |
| 55 | 56 |
| 56 // 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 |
| 57 // virtual Run method, or you can use the DelegateSimpleThread interface. | 58 // virtual Run method, or you can use the DelegateSimpleThread interface. |
| 58 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { | 59 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
| 59 public: | 60 public: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // Return the thread id, only valid after Start(). | 96 // Return the thread id, only valid after Start(). |
| 96 PlatformThreadId tid() { return tid_; } | 97 PlatformThreadId tid() { return tid_; } |
| 97 | 98 |
| 98 // Return True if Start() has ever been called. | 99 // Return True if Start() has ever been called. |
| 99 bool HasBeenStarted() { return event_.IsSignaled(); } | 100 bool HasBeenStarted() { return event_.IsSignaled(); } |
| 100 | 101 |
| 101 // Return True if Join() has evern been called. | 102 // Return True if Join() has evern been called. |
| 102 bool HasBeenJoined() { return joined_; } | 103 bool HasBeenJoined() { return joined_; } |
| 103 | 104 |
| 104 // Overridden from PlatformThread::Delegate: | 105 // Overridden from PlatformThread::Delegate: |
| 105 virtual void ThreadMain(); | 106 virtual void ThreadMain() OVERRIDE; |
| 106 | 107 |
| 107 // Only set priorities with a careful understanding of the consequences. | 108 // Only set priorities with a careful understanding of the consequences. |
| 108 // This is meant for very limited use cases. | 109 // This is meant for very limited use cases. |
| 109 void SetThreadPriority(ThreadPriority priority) { | 110 void SetThreadPriority(ThreadPriority priority) { |
| 110 PlatformThread::SetThreadPriority(thread_, priority); | 111 PlatformThread::SetThreadPriority(thread_, priority); |
| 111 } | 112 } |
| 112 | 113 |
| 113 private: | 114 private: |
| 114 const std::string name_prefix_; | 115 const std::string name_prefix_; |
| 115 std::string name_; | 116 std::string name_; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 129 virtual void Run() = 0; | 130 virtual void Run() = 0; |
| 130 }; | 131 }; |
| 131 | 132 |
| 132 DelegateSimpleThread(Delegate* delegate, | 133 DelegateSimpleThread(Delegate* delegate, |
| 133 const std::string& name_prefix); | 134 const std::string& name_prefix); |
| 134 DelegateSimpleThread(Delegate* delegate, | 135 DelegateSimpleThread(Delegate* delegate, |
| 135 const std::string& name_prefix, | 136 const std::string& name_prefix, |
| 136 const Options& options); | 137 const Options& options); |
| 137 | 138 |
| 138 virtual ~DelegateSimpleThread(); | 139 virtual ~DelegateSimpleThread(); |
| 139 virtual void Run(); | 140 virtual void Run() OVERRIDE; |
| 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. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 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(); | 178 virtual 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 |