| 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 27 matching lines...) Expand all Loading... |
| 38 // // or Start again. | 38 // // or Start again. |
| 39 | 39 |
| 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_api.h" | 48 #include "base/base_export.h" |
| 49 #include "base/basictypes.h" | 49 #include "base/basictypes.h" |
| 50 #include "base/threading/platform_thread.h" | 50 #include "base/threading/platform_thread.h" |
| 51 #include "base/synchronization/lock.h" | 51 #include "base/synchronization/lock.h" |
| 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_API SimpleThread : public PlatformThread::Delegate { | 58 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
| 59 public: | 59 public: |
| 60 class BASE_API Options { | 60 class BASE_EXPORT Options { |
| 61 public: | 61 public: |
| 62 Options() : stack_size_(0) { } | 62 Options() : stack_size_(0) { } |
| 63 ~Options() { } | 63 ~Options() { } |
| 64 | 64 |
| 65 // We use the standard compiler-supplied copy constructor. | 65 // We use the standard compiler-supplied copy constructor. |
| 66 | 66 |
| 67 // A custom stack size, or 0 for the system default. | 67 // A custom stack size, or 0 for the system default. |
| 68 void set_stack_size(size_t size) { stack_size_ = size; } | 68 void set_stack_size(size_t size) { stack_size_ = size; } |
| 69 size_t stack_size() const { return stack_size_; } | 69 size_t stack_size() const { return stack_size_; } |
| 70 private: | 70 private: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 private: | 113 private: |
| 114 const std::string name_prefix_; | 114 const std::string name_prefix_; |
| 115 std::string name_; | 115 std::string name_; |
| 116 const Options options_; | 116 const Options options_; |
| 117 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! | 117 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! |
| 118 WaitableEvent event_; // Signaled if Start() was ever called. | 118 WaitableEvent event_; // Signaled if Start() was ever called. |
| 119 PlatformThreadId tid_; // The backing thread's id. | 119 PlatformThreadId tid_; // The backing thread's id. |
| 120 bool joined_; // True if Join has been called. | 120 bool joined_; // True if Join has been called. |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 class BASE_API DelegateSimpleThread : public SimpleThread { | 123 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { |
| 124 public: | 124 public: |
| 125 class BASE_API Delegate { | 125 class BASE_EXPORT Delegate { |
| 126 public: | 126 public: |
| 127 Delegate() { } | 127 Delegate() { } |
| 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 virtual ~DelegateSimpleThread(); |
| 139 virtual void Run(); | 139 virtual void Run(); |
| 140 private: | 140 private: |
| 141 Delegate* delegate_; | 141 Delegate* delegate_; |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, | 144 // 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 | 145 // 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 | 146 // 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. | 147 // multi-threaded, but don't want to spawn a thread for each small bit of work. |
| 148 // | 148 // |
| 149 // You just call AddWork() to add a delegate to the list of work to be done. | 149 // 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 | 150 // 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() | 151 // for everything to finish. You can reuse a pool, so you can call Start() |
| 152 // again after you've called JoinAll(). | 152 // again after you've called JoinAll(). |
| 153 class BASE_API DelegateSimpleThreadPool | 153 class BASE_EXPORT DelegateSimpleThreadPool |
| 154 : public DelegateSimpleThread::Delegate { | 154 : public DelegateSimpleThread::Delegate { |
| 155 public: | 155 public: |
| 156 typedef DelegateSimpleThread::Delegate Delegate; | 156 typedef DelegateSimpleThread::Delegate Delegate; |
| 157 | 157 |
| 158 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); | 158 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); |
| 159 virtual ~DelegateSimpleThreadPool(); | 159 virtual ~DelegateSimpleThreadPool(); |
| 160 | 160 |
| 161 // Start up all of the underlying threads, and start processing work if we | 161 // Start up all of the underlying threads, and start processing work if we |
| 162 // have any. | 162 // have any. |
| 163 void Start(); | 163 void Start(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 181 int num_threads_; | 181 int num_threads_; |
| 182 std::vector<DelegateSimpleThread*> threads_; | 182 std::vector<DelegateSimpleThread*> threads_; |
| 183 std::queue<Delegate*> delegates_; | 183 std::queue<Delegate*> delegates_; |
| 184 base::Lock lock_; // Locks delegates_ | 184 base::Lock lock_; // Locks delegates_ |
| 185 WaitableEvent dry_; // Not signaled when there is no work to do. | 185 WaitableEvent dry_; // Not signaled when there is no work to do. |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 } // namespace base | 188 } // namespace base |
| 189 | 189 |
| 190 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 190 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
| OLD | NEW |