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/basictypes.h" | 49 #include "base/basictypes.h" |
49 #include "base/threading/platform_thread.h" | 50 #include "base/threading/platform_thread.h" |
50 #include "base/synchronization/lock.h" | 51 #include "base/synchronization/lock.h" |
51 #include "base/synchronization/waitable_event.h" | 52 #include "base/synchronization/waitable_event.h" |
52 | 53 |
53 namespace base { | 54 namespace base { |
54 | 55 |
55 // 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 |
56 // virtual Run method, or you can use the DelegateSimpleThread interface. | 57 // virtual Run method, or you can use the DelegateSimpleThread interface. |
57 class SimpleThread : public PlatformThread::Delegate { | 58 class BASE_API SimpleThread : public PlatformThread::Delegate { |
58 public: | 59 public: |
59 class Options { | 60 class BASE_API Options { |
60 public: | 61 public: |
61 Options() : stack_size_(0) { } | 62 Options() : stack_size_(0) { } |
62 ~Options() { } | 63 ~Options() { } |
63 | 64 |
64 // We use the standard compiler-supplied copy constructor. | 65 // We use the standard compiler-supplied copy constructor. |
65 | 66 |
66 // A custom stack size, or 0 for the system default. | 67 // A custom stack size, or 0 for the system default. |
67 void set_stack_size(size_t size) { stack_size_ = size; } | 68 void set_stack_size(size_t size) { stack_size_ = size; } |
68 size_t stack_size() const { return stack_size_; } | 69 size_t stack_size() const { return stack_size_; } |
69 private: | 70 private: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 private: | 107 private: |
107 const std::string name_prefix_; | 108 const std::string name_prefix_; |
108 std::string name_; | 109 std::string name_; |
109 const Options options_; | 110 const Options options_; |
110 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! | 111 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! |
111 WaitableEvent event_; // Signaled if Start() was ever called. | 112 WaitableEvent event_; // Signaled if Start() was ever called. |
112 PlatformThreadId tid_; // The backing thread's id. | 113 PlatformThreadId tid_; // The backing thread's id. |
113 bool joined_; // True if Join has been called. | 114 bool joined_; // True if Join has been called. |
114 }; | 115 }; |
115 | 116 |
116 class DelegateSimpleThread : public SimpleThread { | 117 class BASE_API DelegateSimpleThread : public SimpleThread { |
117 public: | 118 public: |
118 class Delegate { | 119 class BASE_API Delegate { |
119 public: | 120 public: |
120 Delegate() { } | 121 Delegate() { } |
121 virtual ~Delegate() { } | 122 virtual ~Delegate() { } |
122 virtual void Run() = 0; | 123 virtual void Run() = 0; |
123 }; | 124 }; |
124 | 125 |
125 DelegateSimpleThread(Delegate* delegate, | 126 DelegateSimpleThread(Delegate* delegate, |
126 const std::string& name_prefix); | 127 const std::string& name_prefix); |
127 DelegateSimpleThread(Delegate* delegate, | 128 DelegateSimpleThread(Delegate* delegate, |
128 const std::string& name_prefix, | 129 const std::string& name_prefix, |
129 const Options& options); | 130 const Options& options); |
130 | 131 |
131 virtual ~DelegateSimpleThread(); | 132 virtual ~DelegateSimpleThread(); |
132 virtual void Run(); | 133 virtual void Run(); |
133 private: | 134 private: |
134 Delegate* delegate_; | 135 Delegate* delegate_; |
135 }; | 136 }; |
136 | 137 |
137 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, | 138 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, |
138 // and then add jobs which will be dispatched to the threads. This is | 139 // and then add jobs which will be dispatched to the threads. This is |
139 // convenient when you have a lot of small work that you want done | 140 // convenient when you have a lot of small work that you want done |
140 // multi-threaded, but don't want to spawn a thread for each small bit of work. | 141 // multi-threaded, but don't want to spawn a thread for each small bit of work. |
141 // | 142 // |
142 // You just call AddWork() to add a delegate to the list of work to be done. | 143 // You just call AddWork() to add a delegate to the list of work to be done. |
143 // JoinAll() will make sure that all outstanding work is processed, and wait | 144 // JoinAll() will make sure that all outstanding work is processed, and wait |
144 // for everything to finish. You can reuse a pool, so you can call Start() | 145 // for everything to finish. You can reuse a pool, so you can call Start() |
145 // again after you've called JoinAll(). | 146 // again after you've called JoinAll(). |
146 class DelegateSimpleThreadPool : public DelegateSimpleThread::Delegate { | 147 class BASE_API DelegateSimpleThreadPool |
| 148 : public DelegateSimpleThread::Delegate { |
147 public: | 149 public: |
148 typedef DelegateSimpleThread::Delegate Delegate; | 150 typedef DelegateSimpleThread::Delegate Delegate; |
149 | 151 |
150 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); | 152 DelegateSimpleThreadPool(const std::string& name_prefix, int num_threads); |
151 virtual ~DelegateSimpleThreadPool(); | 153 virtual ~DelegateSimpleThreadPool(); |
152 | 154 |
153 // Start up all of the underlying threads, and start processing work if we | 155 // Start up all of the underlying threads, and start processing work if we |
154 // have any. | 156 // have any. |
155 void Start(); | 157 void Start(); |
156 | 158 |
(...skipping 16 matching lines...) Expand all Loading... |
173 int num_threads_; | 175 int num_threads_; |
174 std::vector<DelegateSimpleThread*> threads_; | 176 std::vector<DelegateSimpleThread*> threads_; |
175 std::queue<Delegate*> delegates_; | 177 std::queue<Delegate*> delegates_; |
176 base::Lock lock_; // Locks delegates_ | 178 base::Lock lock_; // Locks delegates_ |
177 WaitableEvent dry_; // Not signaled when there is no work to do. | 179 WaitableEvent dry_; // Not signaled when there is no work to do. |
178 }; | 180 }; |
179 | 181 |
180 } // namespace base | 182 } // namespace base |
181 | 183 |
182 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 184 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
OLD | NEW |