1 | // |
---|
2 | // Thread.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Thread.h#6 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: Thread |
---|
9 | // |
---|
10 | // Definition of the Thread class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Thread_INCLUDED |
---|
40 | #define Foundation_Thread_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Mutex.h" |
---|
45 | |
---|
46 | |
---|
47 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
48 | #include "Poco/Thread_WIN32.h" |
---|
49 | #else |
---|
50 | #include "Poco/Thread_POSIX.h" |
---|
51 | #endif |
---|
52 | |
---|
53 | |
---|
54 | namespace Poco { |
---|
55 | |
---|
56 | |
---|
57 | class Runnable; |
---|
58 | class ThreadLocalStorage; |
---|
59 | |
---|
60 | |
---|
61 | class Foundation_API Thread: private ThreadImpl |
---|
62 | /// This class implements a platform-independent |
---|
63 | /// wrapper to an operating system thread. |
---|
64 | /// |
---|
65 | /// Every Thread object gets a unique (within |
---|
66 | /// its process) numeric thread ID. |
---|
67 | /// Furthermore, a thread can be assigned a name. |
---|
68 | /// The name of a thread can be changed at any time. |
---|
69 | { |
---|
70 | public: |
---|
71 | typedef ThreadImpl::TIDImpl TID; |
---|
72 | |
---|
73 | using ThreadImpl::Callable; |
---|
74 | |
---|
75 | enum Priority |
---|
76 | /// Thread priorities. |
---|
77 | { |
---|
78 | PRIO_LOWEST = PRIO_LOWEST_IMPL, /// The lowest thread priority. |
---|
79 | PRIO_LOW = PRIO_LOW_IMPL, /// A lower than normal thread priority. |
---|
80 | PRIO_NORMAL = PRIO_NORMAL_IMPL, /// The normal thread priority. |
---|
81 | PRIO_HIGH = PRIO_HIGH_IMPL, /// A higher than normal thread priority. |
---|
82 | PRIO_HIGHEST = PRIO_HIGHEST_IMPL /// The highest thread priority. |
---|
83 | }; |
---|
84 | |
---|
85 | Thread(); |
---|
86 | /// Creates a thread. Call start() to start it. |
---|
87 | |
---|
88 | Thread(const std::string& name); |
---|
89 | /// Creates a named thread. Call start() to start it. |
---|
90 | |
---|
91 | ~Thread(); |
---|
92 | /// Destroys the thread. |
---|
93 | |
---|
94 | int id() const; |
---|
95 | /// Returns the unique thread ID of the thread. |
---|
96 | |
---|
97 | TID tid() const; |
---|
98 | /// Returns the native thread ID of the thread. |
---|
99 | |
---|
100 | std::string name() const; |
---|
101 | /// Returns the name of the thread. |
---|
102 | |
---|
103 | std::string getName() const; |
---|
104 | /// Returns teh name of the thread. |
---|
105 | |
---|
106 | void setName(const std::string& name); |
---|
107 | /// Sets the name of the thread. |
---|
108 | |
---|
109 | void setPriority(Priority prio); |
---|
110 | /// Sets the thread's priority. |
---|
111 | /// |
---|
112 | /// Some platform only allow changing a thread's priority |
---|
113 | /// if the process has certain privileges. |
---|
114 | |
---|
115 | Priority getPriority() const; |
---|
116 | /// Returns the thread's priority. |
---|
117 | |
---|
118 | void setOSPriority(int prio); |
---|
119 | /// Sets the thread's priority, using an operating system specific |
---|
120 | /// priority value. Use getMinOSPriority() and getMaxOSPriority() to |
---|
121 | /// obtain mininum and maximum priority values. |
---|
122 | |
---|
123 | int getOSPriority() const; |
---|
124 | /// Returns the thread's priority, expressed as an operating system |
---|
125 | /// specific priority value. |
---|
126 | |
---|
127 | static int getMinOSPriority(); |
---|
128 | /// Returns the mininum operating system-specific priority value, |
---|
129 | /// which can be passed to setOSPriority(). |
---|
130 | |
---|
131 | static int getMaxOSPriority(); |
---|
132 | /// Returns the maximum operating system-specific priority value, |
---|
133 | /// which can be passed to setOSPriority(). |
---|
134 | |
---|
135 | void setStackSize(int size); |
---|
136 | /// Sets the thread's stack size in bytes. |
---|
137 | /// Setting the stack size to 0 will use the default stack size. |
---|
138 | /// Typically, the real stack size is rounded up to the nearest |
---|
139 | /// page size multiple. |
---|
140 | |
---|
141 | int getStackSize() const; |
---|
142 | /// Returns the thread's stack size in bytes. |
---|
143 | /// If the default stack size is used, 0 is returned. |
---|
144 | |
---|
145 | void start(Runnable& target); |
---|
146 | /// Starts the thread with the given target. |
---|
147 | |
---|
148 | void start(Callable target, void* pData = 0); |
---|
149 | /// Starts the thread with the given target and parameter. |
---|
150 | |
---|
151 | void join(); |
---|
152 | /// Waits until the thread completes execution. |
---|
153 | /// If multiple threads try to join the same |
---|
154 | /// thread, the result is undefined. |
---|
155 | |
---|
156 | void join(long milliseconds); |
---|
157 | /// Waits for at most the given interval for the thread |
---|
158 | /// to complete. Throws a TimeoutException if the thread |
---|
159 | /// does not complete within the specified time interval. |
---|
160 | |
---|
161 | bool tryJoin(long milliseconds); |
---|
162 | /// Waits for at most the given interval for the thread |
---|
163 | /// to complete. Returns true if the thread has finished, |
---|
164 | /// false otherwise. |
---|
165 | |
---|
166 | bool isRunning() const; |
---|
167 | /// Returns true if the thread is running. |
---|
168 | |
---|
169 | static void sleep(long milliseconds); |
---|
170 | /// Suspends the current thread for the specified |
---|
171 | /// amount of time. |
---|
172 | |
---|
173 | static void yield(); |
---|
174 | /// Yields cpu to other threads. |
---|
175 | |
---|
176 | static Thread* current(); |
---|
177 | /// Returns the Thread object for the currently active thread. |
---|
178 | /// If the current thread is the main thread, 0 is returned. |
---|
179 | |
---|
180 | static TID currentTid(); |
---|
181 | /// Returns the native thread ID for the current thread. |
---|
182 | |
---|
183 | protected: |
---|
184 | ThreadLocalStorage& tls(); |
---|
185 | /// Returns a reference to the thread's local storage. |
---|
186 | |
---|
187 | void clearTLS(); |
---|
188 | /// Clears the thread's local storage. |
---|
189 | |
---|
190 | std::string makeName(); |
---|
191 | /// Creates a unique name for a thread. |
---|
192 | |
---|
193 | static int uniqueId(); |
---|
194 | /// Creates and returns a unique id for a thread. |
---|
195 | |
---|
196 | private: |
---|
197 | Thread(const Thread&); |
---|
198 | Thread& operator = (const Thread&); |
---|
199 | |
---|
200 | int _id; |
---|
201 | std::string _name; |
---|
202 | ThreadLocalStorage* _pTLS; |
---|
203 | mutable FastMutex _mutex; |
---|
204 | |
---|
205 | friend class ThreadLocalStorage; |
---|
206 | friend class PooledThread; |
---|
207 | }; |
---|
208 | |
---|
209 | |
---|
210 | // |
---|
211 | // inlines |
---|
212 | // |
---|
213 | inline Thread::TID Thread::tid() const |
---|
214 | { |
---|
215 | return tidImpl(); |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | inline int Thread::id() const |
---|
220 | { |
---|
221 | return _id; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | inline std::string Thread::name() const |
---|
226 | { |
---|
227 | FastMutex::ScopedLock lock(_mutex); |
---|
228 | |
---|
229 | return _name; |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | inline std::string Thread::getName() const |
---|
234 | { |
---|
235 | FastMutex::ScopedLock lock(_mutex); |
---|
236 | |
---|
237 | return _name; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | inline bool Thread::isRunning() const |
---|
242 | { |
---|
243 | return isRunningImpl(); |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | inline void Thread::sleep(long milliseconds) |
---|
248 | { |
---|
249 | sleepImpl(milliseconds); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | inline void Thread::yield() |
---|
254 | { |
---|
255 | yieldImpl(); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | inline Thread* Thread::current() |
---|
260 | { |
---|
261 | return static_cast<Thread*>(currentImpl()); |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | inline void Thread::setOSPriority(int prio) |
---|
266 | { |
---|
267 | setOSPriorityImpl(prio); |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | inline int Thread::getOSPriority() const |
---|
272 | { |
---|
273 | return getOSPriorityImpl(); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | inline int Thread::getMinOSPriority() |
---|
278 | { |
---|
279 | return ThreadImpl::getMinOSPriorityImpl(); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | inline int Thread::getMaxOSPriority() |
---|
284 | { |
---|
285 | return ThreadImpl::getMaxOSPriorityImpl(); |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | inline void Thread::setStackSize(int size) |
---|
290 | { |
---|
291 | setStackSizeImpl(size); |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | inline int Thread::getStackSize() const |
---|
296 | { |
---|
297 | return getStackSizeImpl(); |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | inline Thread::TID Thread::currentTid() |
---|
302 | { |
---|
303 | return currentTidImpl(); |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | } // namespace Poco |
---|
308 | |
---|
309 | |
---|
310 | #endif // Foundation_Thread_INCLUDED |
---|