1 | // |
---|
2 | // Thread_WIN32.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Thread_WIN32.cpp#9 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: Thread |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/Thread_WIN32.h> |
---|
38 | #include <Poco/Exception.h> |
---|
39 | #include <Poco/ErrorHandler.h> |
---|
40 | #include <process.h> |
---|
41 | |
---|
42 | |
---|
43 | namespace Poco { |
---|
44 | |
---|
45 | |
---|
46 | ThreadImpl::CurrentThreadHolder ThreadImpl::_currentThreadHolder; |
---|
47 | |
---|
48 | |
---|
49 | ThreadImpl::ThreadImpl(): |
---|
50 | _pRunnableTarget(0), |
---|
51 | _thread(0), |
---|
52 | _threadId(0), |
---|
53 | _prio(PRIO_NORMAL_IMPL), |
---|
54 | _stackSize(POCO_THREAD_STACK_SIZE) |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | ThreadImpl::~ThreadImpl() |
---|
60 | { |
---|
61 | if (_thread) CloseHandle(_thread); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | void ThreadImpl::setPriorityImpl(int prio) |
---|
66 | { |
---|
67 | if (prio != _prio) |
---|
68 | { |
---|
69 | _prio = prio; |
---|
70 | if (_thread) |
---|
71 | { |
---|
72 | if (SetThreadPriority(_thread, _prio) == 0) |
---|
73 | throw SystemException("cannot set thread priority"); |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void ThreadImpl::setOSPriorityImpl(int prio) |
---|
80 | { |
---|
81 | setPriorityImpl(prio); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void ThreadImpl::startImpl(Runnable& target) |
---|
86 | { |
---|
87 | if (isRunningImpl()) |
---|
88 | throw SystemException("thread already running"); |
---|
89 | |
---|
90 | _pRunnableTarget = ⌖ |
---|
91 | |
---|
92 | createImpl(runnableEntry, this); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | void ThreadImpl::startImpl(Callable target, void* pData) |
---|
97 | { |
---|
98 | if (isRunningImpl()) |
---|
99 | throw SystemException("thread already running"); |
---|
100 | |
---|
101 | _callbackTarget.callback = target; |
---|
102 | _callbackTarget.pData = pData; |
---|
103 | |
---|
104 | createImpl(callableEntry, this); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | void ThreadImpl::createImpl(Entry ent, void* pData) |
---|
109 | { |
---|
110 | #if defined(_DLL) |
---|
111 | _thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId); |
---|
112 | #else |
---|
113 | unsigned threadId; |
---|
114 | _thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId); |
---|
115 | _threadId = static_cast<DWORD>(threadId); |
---|
116 | #endif |
---|
117 | if (!_thread) |
---|
118 | throw SystemException("cannot create thread"); |
---|
119 | if (_prio != PRIO_NORMAL_IMPL && !SetThreadPriority(_thread, _prio)) |
---|
120 | throw SystemException("cannot set thread priority"); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void ThreadImpl::joinImpl() |
---|
125 | { |
---|
126 | if (!_thread) return; |
---|
127 | |
---|
128 | switch (WaitForSingleObject(_thread, INFINITE)) |
---|
129 | { |
---|
130 | case WAIT_OBJECT_0: |
---|
131 | threadCleanup(); |
---|
132 | return; |
---|
133 | default: |
---|
134 | throw SystemException("cannot join thread"); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | bool ThreadImpl::joinImpl(long milliseconds) |
---|
140 | { |
---|
141 | if (!_thread) return true; |
---|
142 | |
---|
143 | switch (WaitForSingleObject(_thread, milliseconds + 1)) |
---|
144 | { |
---|
145 | case WAIT_TIMEOUT: |
---|
146 | return false; |
---|
147 | case WAIT_OBJECT_0: |
---|
148 | threadCleanup(); |
---|
149 | return true; |
---|
150 | default: |
---|
151 | throw SystemException("cannot join thread"); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | bool ThreadImpl::isRunningImpl() const |
---|
157 | { |
---|
158 | if (_thread) |
---|
159 | { |
---|
160 | DWORD ec = 0; |
---|
161 | return GetExitCodeThread(_thread, &ec) && ec == STILL_ACTIVE; |
---|
162 | } |
---|
163 | return false; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void ThreadImpl::threadCleanup() |
---|
168 | { |
---|
169 | if (!_thread) return; |
---|
170 | if (CloseHandle(_thread)) _thread = 0; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | ThreadImpl* ThreadImpl::currentImpl() |
---|
175 | { |
---|
176 | return _currentThreadHolder.get(); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | ThreadImpl::TIDImpl ThreadImpl::currentTidImpl() |
---|
181 | { |
---|
182 | return GetCurrentThreadId(); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | #if defined(_DLL) |
---|
187 | DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread) |
---|
188 | #else |
---|
189 | unsigned __stdcall ThreadImpl::runnableEntry(void* pThread) |
---|
190 | #endif |
---|
191 | { |
---|
192 | _currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread)); |
---|
193 | try |
---|
194 | { |
---|
195 | reinterpret_cast<ThreadImpl*>(pThread)->_pRunnableTarget->run(); |
---|
196 | } |
---|
197 | catch (Exception& exc) |
---|
198 | { |
---|
199 | ErrorHandler::handle(exc); |
---|
200 | } |
---|
201 | catch (std::exception& exc) |
---|
202 | { |
---|
203 | ErrorHandler::handle(exc); |
---|
204 | } |
---|
205 | catch (...) |
---|
206 | { |
---|
207 | ErrorHandler::handle(); |
---|
208 | } |
---|
209 | return 0; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | #if defined(_DLL) |
---|
214 | DWORD WINAPI ThreadImpl::callableEntry(LPVOID pThread) |
---|
215 | #else |
---|
216 | unsigned __stdcall ThreadImpl::callableEntry(void* pThread) |
---|
217 | #endif |
---|
218 | { |
---|
219 | _currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread)); |
---|
220 | try |
---|
221 | { |
---|
222 | ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread); |
---|
223 | pTI->_callbackTarget.callback(pTI->_callbackTarget.pData); |
---|
224 | } |
---|
225 | catch (Exception& exc) |
---|
226 | { |
---|
227 | ErrorHandler::handle(exc); |
---|
228 | } |
---|
229 | catch (std::exception& exc) |
---|
230 | { |
---|
231 | ErrorHandler::handle(exc); |
---|
232 | } |
---|
233 | catch (...) |
---|
234 | { |
---|
235 | ErrorHandler::handle(); |
---|
236 | } |
---|
237 | return 0; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | } // namespace Poco |
---|