| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 void set_stack_size(size_t size) { stack_size_ = size; } | 67 void set_stack_size(size_t size) { stack_size_ = size; } |
| 68 size_t stack_size() const { return stack_size_; } | 68 size_t stack_size() const { return stack_size_; } |
| 69 private: | 69 private: |
| 70 size_t stack_size_; | 70 size_t stack_size_; |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 // Create a SimpleThread. |options| should be used to manage any specific | 73 // Create a SimpleThread. |options| should be used to manage any specific |
| 74 // configuration involving the thread creation and management. | 74 // configuration involving the thread creation and management. |
| 75 // Every thread has a name, in the form of |name_prefix|/TID, for example | 75 // Every thread has a name, in the form of |name_prefix|/TID, for example |
| 76 // "my_thread/321". The thread will not be created until Start() is called. | 76 // "my_thread/321". The thread will not be created until Start() is called. |
| 77 explicit SimpleThread(const std::string& name_prefix) | 77 explicit SimpleThread(const std::string& name_prefix); |
| 78 : name_prefix_(name_prefix), name_(name_prefix), | 78 SimpleThread(const std::string& name_prefix, const Options& options); |
| 79 thread_(), event_(true, false), tid_(0), joined_(false) { } | |
| 80 SimpleThread(const std::string& name_prefix, const Options& options) | |
| 81 : name_prefix_(name_prefix), name_(name_prefix), options_(options), | |
| 82 thread_(), event_(true, false), tid_(0), joined_(false) { } | |
| 83 | 79 |
| 84 virtual ~SimpleThread(); | 80 virtual ~SimpleThread(); |
| 85 | 81 |
| 86 virtual void Start(); | 82 virtual void Start(); |
| 87 virtual void Join(); | 83 virtual void Join(); |
| 88 | 84 |
| 89 // We follow the PlatformThread Delegate interface. | 85 // We follow the PlatformThread Delegate interface. |
| 90 virtual void ThreadMain(); | 86 virtual void ThreadMain(); |
| 91 | 87 |
| 92 // Subclasses should override the Run method. | 88 // Subclasses should override the Run method. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 120 class DelegateSimpleThread : public SimpleThread { | 116 class DelegateSimpleThread : public SimpleThread { |
| 121 public: | 117 public: |
| 122 class Delegate { | 118 class Delegate { |
| 123 public: | 119 public: |
| 124 Delegate() { } | 120 Delegate() { } |
| 125 virtual ~Delegate() { } | 121 virtual ~Delegate() { } |
| 126 virtual void Run() = 0; | 122 virtual void Run() = 0; |
| 127 }; | 123 }; |
| 128 | 124 |
| 129 DelegateSimpleThread(Delegate* delegate, | 125 DelegateSimpleThread(Delegate* delegate, |
| 130 const std::string& name_prefix) | 126 const std::string& name_prefix); |
| 131 : SimpleThread(name_prefix), delegate_(delegate) { } | |
| 132 DelegateSimpleThread(Delegate* delegate, | 127 DelegateSimpleThread(Delegate* delegate, |
| 133 const std::string& name_prefix, | 128 const std::string& name_prefix, |
| 134 const Options& options) | 129 const Options& options); |
| 135 : SimpleThread(name_prefix, options), delegate_(delegate) { } | |
| 136 | 130 |
| 137 virtual ~DelegateSimpleThread() { } | 131 virtual ~DelegateSimpleThread(); |
| 138 virtual void Run(); | 132 virtual void Run(); |
| 139 private: | 133 private: |
| 140 Delegate* delegate_; | 134 Delegate* delegate_; |
| 141 }; | 135 }; |
| 142 | 136 |
| 143 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, | 137 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, |
| 144 // and then add jobs which will be dispatched to the threads. This is | 138 // and then add jobs which will be dispatched to the threads. This is |
| 145 // convenient when you have a lot of small work that you want done | 139 // convenient when you have a lot of small work that you want done |
| 146 // multi-threaded, but don't want to spawn a thread for each small bit of work. | 140 // multi-threaded, but don't want to spawn a thread for each small bit of work. |
| 147 // | 141 // |
| 148 // You just call AddWork() to add a delegate to the list of work to be done. | 142 // You just call AddWork() to add a delegate to the list of work to be done. |
| 149 // JoinAll() will make sure that all outstanding work is processed, and wait | 143 // JoinAll() will make sure that all outstanding work is processed, and wait |
| 150 // for everything to finish. You can reuse a pool, so you can call Start() | 144 // for everything to finish. You can reuse a pool, so you can call Start() |
| 151 // again after you've called JoinAll(). | 145 // again after you've called JoinAll(). |
| 152 class DelegateSimpleThreadPool : public DelegateSimpleThread::Delegate { | 146 class DelegateSimpleThreadPool : public DelegateSimpleThread::Delegate { |
| 153 public: | 147 public: |
| 154 typedef DelegateSimpleThread::Delegate Delegate; | 148 typedef DelegateSimpleThread::Delegate Delegate; |
| 155 | 149 |
| 156 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads) | 150 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); |
| 157 : name_prefix_(name_prefix), num_threads_(num_threads), | 151 virtual ~DelegateSimpleThreadPool(); |
| 158 dry_(true, false) { } | |
| 159 ~DelegateSimpleThreadPool(); | |
| 160 | 152 |
| 161 // Start up all of the underlying threads, and start processing work if we | 153 // Start up all of the underlying threads, and start processing work if we |
| 162 // have any. | 154 // have any. |
| 163 void Start(); | 155 void Start(); |
| 164 | 156 |
| 165 // Make sure all outstanding work is finished, and wait for and destroy all | 157 // Make sure all outstanding work is finished, and wait for and destroy all |
| 166 // of the underlying threads in the pool. | 158 // of the underlying threads in the pool. |
| 167 void JoinAll(); | 159 void JoinAll(); |
| 168 | 160 |
| 169 // It is safe to AddWork() any time, before or after Start(). | 161 // It is safe to AddWork() any time, before or after Start(). |
| (...skipping 11 matching lines...) Expand all Loading... |
| 181 int num_threads_; | 173 int num_threads_; |
| 182 std::vector<DelegateSimpleThread*> threads_; | 174 std::vector<DelegateSimpleThread*> threads_; |
| 183 std::queue<Delegate*> delegates_; | 175 std::queue<Delegate*> delegates_; |
| 184 Lock lock_; // Locks delegates_ | 176 Lock lock_; // Locks delegates_ |
| 185 WaitableEvent dry_; // Not signaled when there is no work to do. | 177 WaitableEvent dry_; // Not signaled when there is no work to do. |
| 186 }; | 178 }; |
| 187 | 179 |
| 188 } // namespace base | 180 } // namespace base |
| 189 | 181 |
| 190 #endif // BASE_SIMPLE_THREAD_H_ | 182 #endif // BASE_SIMPLE_THREAD_H_ |
| OLD | NEW |