Thread_WIN32.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Thread_WIN32.h
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Thread_WIN32.cpp#4 $
  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. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Thread_WIN32.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/ErrorHandler.h"
  18. #include <process.h>
  19. #if defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
  20. namespace
  21. {
  22. /// See <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>
  23. /// and <http://blogs.msdn.com/b/stevejs/archive/2005/12/19/505815.aspx> for
  24. /// more information on the code below.
  25. const DWORD MS_VC_EXCEPTION = 0x406D1388;
  26. #pragma pack(push,8)
  27. typedef struct tagTHREADNAME_INFO
  28. {
  29. DWORD dwType; // Must be 0x1000.
  30. LPCSTR szName; // Pointer to name (in user addr space).
  31. DWORD dwThreadID; // Thread ID (-1=caller thread).
  32. DWORD dwFlags; // Reserved for future use, must be zero.
  33. } THREADNAME_INFO;
  34. #pragma pack(pop)
  35. void setThreadName(DWORD dwThreadID, const char* threadName)
  36. {
  37. THREADNAME_INFO info;
  38. info.dwType = 0x1000;
  39. info.szName = threadName;
  40. info.dwThreadID = dwThreadID;
  41. info.dwFlags = 0;
  42. __try
  43. {
  44. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  45. }
  46. __except (EXCEPTION_CONTINUE_EXECUTION)
  47. {
  48. }
  49. }
  50. }
  51. #endif
  52. namespace Poco {
  53. ThreadImpl::CurrentThreadHolder ThreadImpl::_currentThreadHolder;
  54. ThreadImpl::ThreadImpl():
  55. _thread(0),
  56. _threadId(0),
  57. _prio(PRIO_NORMAL_IMPL),
  58. _stackSize(POCO_THREAD_STACK_SIZE)
  59. {
  60. }
  61. ThreadImpl::~ThreadImpl()
  62. {
  63. if (_thread) CloseHandle(_thread);
  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. void ThreadImpl::setOSPriorityImpl(int prio, int /* policy */)
  78. {
  79. setPriorityImpl(prio);
  80. }
  81. void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
  82. {
  83. if (isRunningImpl())
  84. throw SystemException("thread already running");
  85. _pRunnableTarget = pTarget;
  86. createImpl(runnableEntry, this);
  87. }
  88. void ThreadImpl::createImpl(Entry ent, void* pData)
  89. {
  90. #if defined(_DLL)
  91. _thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId);
  92. #else
  93. unsigned threadId;
  94. _thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId);
  95. _threadId = static_cast<DWORD>(threadId);
  96. #endif
  97. if (!_thread)
  98. throw SystemException("cannot create thread");
  99. if (_prio != PRIO_NORMAL_IMPL && !SetThreadPriority(_thread, _prio))
  100. throw SystemException("cannot set thread priority");
  101. }
  102. void ThreadImpl::joinImpl()
  103. {
  104. if (!_thread) return;
  105. switch (WaitForSingleObject(_thread, INFINITE))
  106. {
  107. case WAIT_OBJECT_0:
  108. threadCleanup();
  109. return;
  110. default:
  111. throw SystemException("cannot join thread");
  112. }
  113. }
  114. bool ThreadImpl::joinImpl(long milliseconds)
  115. {
  116. if (!_thread) return true;
  117. switch (WaitForSingleObject(_thread, milliseconds + 1))
  118. {
  119. case WAIT_TIMEOUT:
  120. return false;
  121. case WAIT_OBJECT_0:
  122. threadCleanup();
  123. return true;
  124. default:
  125. throw SystemException("cannot join thread");
  126. }
  127. }
  128. bool ThreadImpl::isRunningImpl() const
  129. {
  130. if (_thread)
  131. {
  132. DWORD ec = 0;
  133. return GetExitCodeThread(_thread, &ec) && ec == STILL_ACTIVE;
  134. }
  135. return false;
  136. }
  137. void ThreadImpl::threadCleanup()
  138. {
  139. if (!_thread) return;
  140. if (CloseHandle(_thread)) _thread = 0;
  141. }
  142. ThreadImpl* ThreadImpl::currentImpl()
  143. {
  144. return _currentThreadHolder.get();
  145. }
  146. ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
  147. {
  148. return GetCurrentThreadId();
  149. }
  150. #if defined(_DLL)
  151. DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread)
  152. #else
  153. unsigned __stdcall ThreadImpl::runnableEntry(void* pThread)
  154. #endif
  155. {
  156. _currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread));
  157. #if defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
  158. setThreadName(-1, reinterpret_cast<Thread*>(pThread)->getName().c_str());
  159. #endif
  160. try
  161. {
  162. reinterpret_cast<ThreadImpl*>(pThread)->_pRunnableTarget->run();
  163. }
  164. catch (Exception& exc)
  165. {
  166. ErrorHandler::handle(exc);
  167. }
  168. catch (std::exception& exc)
  169. {
  170. ErrorHandler::handle(exc);
  171. }
  172. catch (...)
  173. {
  174. ErrorHandler::handle();
  175. }
  176. return 0;
  177. }
  178. } // namespace Poco