Thread_WINCE.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Thread_WINCE.h
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Thread_WINCE.cpp#1 $
  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_WINCE.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/ErrorHandler.h"
  18. namespace Poco {
  19. ThreadImpl::CurrentThreadHolder ThreadImpl::_currentThreadHolder;
  20. ThreadImpl::ThreadImpl():
  21. _pRunnableTarget(0),
  22. _thread(0),
  23. _threadId(0),
  24. _prio(PRIO_NORMAL_IMPL),
  25. _stackSize(POCO_THREAD_STACK_SIZE)
  26. {
  27. }
  28. ThreadImpl::~ThreadImpl()
  29. {
  30. if (_thread) CloseHandle(_thread);
  31. }
  32. void ThreadImpl::setPriorityImpl(int prio)
  33. {
  34. if (prio != _prio)
  35. {
  36. _prio = prio;
  37. if (_thread)
  38. {
  39. if (SetThreadPriority(_thread, _prio) == 0)
  40. throw SystemException("cannot set thread priority");
  41. }
  42. }
  43. }
  44. void ThreadImpl::setOSPriorityImpl(int prio, int /* policy */)
  45. {
  46. setPriorityImpl(prio);
  47. }
  48. void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
  49. {
  50. if (isRunningImpl())
  51. throw SystemException("thread already running");
  52. _pRunnableTarget = pTarget;
  53. createImpl(runnableEntry, this);
  54. }
  55. void ThreadImpl::createImpl(Entry ent, void* pData)
  56. {
  57. _thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId);
  58. if (!_thread)
  59. throw SystemException("cannot create thread");
  60. if (_prio != PRIO_NORMAL_IMPL && !SetThreadPriority(_thread, _prio))
  61. throw SystemException("cannot set thread priority");
  62. }
  63. void ThreadImpl::joinImpl()
  64. {
  65. if (!_thread) return;
  66. switch (WaitForSingleObject(_thread, INFINITE))
  67. {
  68. case WAIT_OBJECT_0:
  69. threadCleanup();
  70. return;
  71. default:
  72. throw SystemException("cannot join thread");
  73. }
  74. }
  75. bool ThreadImpl::joinImpl(long milliseconds)
  76. {
  77. if (!_thread) return true;
  78. switch (WaitForSingleObject(_thread, milliseconds + 1))
  79. {
  80. case WAIT_TIMEOUT:
  81. return false;
  82. case WAIT_OBJECT_0:
  83. threadCleanup();
  84. return true;
  85. default:
  86. throw SystemException("cannot join thread");
  87. }
  88. }
  89. bool ThreadImpl::isRunningImpl() const
  90. {
  91. if (_thread)
  92. {
  93. DWORD ec = 0;
  94. return GetExitCodeThread(_thread, &ec) && ec == STILL_ACTIVE;
  95. }
  96. return false;
  97. }
  98. void ThreadImpl::threadCleanup()
  99. {
  100. if (!_thread) return;
  101. if (CloseHandle(_thread)) _thread = 0;
  102. }
  103. ThreadImpl* ThreadImpl::currentImpl()
  104. {
  105. return _currentThreadHolder.get();
  106. }
  107. ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
  108. {
  109. return GetCurrentThreadId();
  110. }
  111. DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread)
  112. {
  113. _currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread));
  114. try
  115. {
  116. reinterpret_cast<ThreadImpl*>(pThread)->_pRunnableTarget->run();
  117. }
  118. catch (Exception& exc)
  119. {
  120. ErrorHandler::handle(exc);
  121. }
  122. catch (std::exception& exc)
  123. {
  124. ErrorHandler::handle(exc);
  125. }
  126. catch (...)
  127. {
  128. ErrorHandler::handle();
  129. }
  130. return 0;
  131. }
  132. } // namespace Poco