Thread_VX.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // Thread_VX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Thread_VX.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Thread
  9. //
  10. // Copyright (c) 2004-2011, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Thread_VX.h"
  16. #include "Poco/ErrorHandler.h"
  17. #include "Poco/Exception.h"
  18. #include "Poco/Timestamp.h"
  19. #include "Poco/Timespan.h"
  20. #include <timers.h>
  21. namespace Poco {
  22. ThreadImpl* ThreadImpl::_pCurrent(0);
  23. ThreadImpl::ThreadImpl():
  24. _pData(new ThreadData)
  25. {
  26. }
  27. ThreadImpl::~ThreadImpl()
  28. {
  29. }
  30. void ThreadImpl::setPriorityImpl(int prio)
  31. {
  32. if (prio != _pData->prio)
  33. {
  34. _pData->prio = prio;
  35. _pData->osPrio = mapPrio(_pData->prio);
  36. if (isRunningImpl())
  37. {
  38. if (taskPrioritySet(_pData->task, _pData->osPrio) != OK)
  39. throw SystemException("cannot set task priority");
  40. }
  41. }
  42. }
  43. void ThreadImpl::setOSPriorityImpl(int prio, int /* policy */)
  44. {
  45. if (prio != _pData->osPrio)
  46. {
  47. _pData->prio = reverseMapPrio(prio);
  48. _pData->osPrio = prio;
  49. if (_pData->pRunnableTarget || _pData->pCallbackTarget)
  50. {
  51. if (taskPrioritySet(_pData->task, prio) != OK)
  52. throw SystemException("cannot set task priority");
  53. }
  54. }
  55. }
  56. int ThreadImpl::getMinOSPriorityImpl(int /* policy */)
  57. {
  58. return 255;
  59. }
  60. int ThreadImpl::getMaxOSPriorityImpl(int /* policy */)
  61. {
  62. return 0;
  63. }
  64. void ThreadImpl::setStackSizeImpl(int size)
  65. {
  66. _pData->stackSize = size;
  67. }
  68. void ThreadImpl::startImpl(Runnable& target)
  69. {
  70. if (_pData->pRunnableTarget)
  71. throw SystemException("thread already running");
  72. _pData->pRunnableTarget = &target;
  73. int stackSize = _pData->stackSize == 0 ? DEFAULT_THREAD_STACK_SIZE : _pData->stackSize;
  74. int id = taskSpawn(NULL, _pData->osPrio, VX_FP_TASK, stackSize, reinterpret_cast<FUNCPTR>(runnableEntry), reinterpret_cast<int>(this), 0, 0, 0, 0, 0, 0, 0, 0, 0);
  75. if (id == ERROR)
  76. throw SystemException("cannot spawn task");
  77. _pData->task = id;
  78. }
  79. void ThreadImpl::startImpl(Callable target, void* pData)
  80. {
  81. if (_pData->pCallbackTarget && _pData->pCallbackTarget->callback)
  82. throw SystemException("thread already running");
  83. if (0 == _pData->pCallbackTarget.get())
  84. _pData->pCallbackTarget = new CallbackData;
  85. _pData->pCallbackTarget->callback = target;
  86. _pData->pCallbackTarget->pData = pData;
  87. int stackSize = _pData->stackSize == 0 ? DEFAULT_THREAD_STACK_SIZE : _pData->stackSize;
  88. int id = taskSpawn(NULL, _pData->osPrio, VX_FP_TASK, stackSize, reinterpret_cast<FUNCPTR>(callableEntry), reinterpret_cast<int>(this), 0, 0, 0, 0, 0, 0, 0, 0, 0);
  89. if (id == ERROR)
  90. throw SystemException("cannot spawn task");
  91. _pData->task = id;
  92. }
  93. void ThreadImpl::joinImpl()
  94. {
  95. _pData->done.wait();
  96. }
  97. bool ThreadImpl::joinImpl(long milliseconds)
  98. {
  99. return _pData->done.tryWait(milliseconds);
  100. }
  101. ThreadImpl* ThreadImpl::currentImpl()
  102. {
  103. return _pCurrent;
  104. }
  105. ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
  106. {
  107. return taskIdSelf();
  108. }
  109. void ThreadImpl::sleepImpl(long milliseconds)
  110. {
  111. Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds));
  112. int rc;
  113. do
  114. {
  115. struct timespec ts;
  116. ts.tv_sec = (long) remainingTime.totalSeconds();
  117. ts.tv_nsec = (long) remainingTime.useconds()*1000;
  118. Poco::Timestamp start;
  119. rc = ::nanosleep(&ts, 0);
  120. if (rc < 0 && errno == EINTR)
  121. {
  122. Poco::Timestamp end;
  123. Poco::Timespan waited = start.elapsed();
  124. if (waited < remainingTime)
  125. remainingTime -= waited;
  126. else
  127. remainingTime = 0;
  128. }
  129. }
  130. while (remainingTime > 0 && rc < 0 && errno == EINTR);
  131. if (rc < 0 && remainingTime > 0) throw Poco::SystemException("Thread::sleep(): nanosleep() failed");
  132. }
  133. void ThreadImpl::runnableEntry(void* pThread, int, int, int, int, int, int, int, int, int)
  134. {
  135. taskVarAdd(0, reinterpret_cast<int*>(&_pCurrent));
  136. _pCurrent = reinterpret_cast<ThreadImpl*>(pThread);
  137. AutoPtr<ThreadData> pData = _pCurrent->_pData;
  138. try
  139. {
  140. pData->pRunnableTarget->run();
  141. }
  142. catch (Exception& exc)
  143. {
  144. ErrorHandler::handle(exc);
  145. }
  146. catch (std::exception& exc)
  147. {
  148. ErrorHandler::handle(exc);
  149. }
  150. catch (...)
  151. {
  152. ErrorHandler::handle();
  153. }
  154. pData->pRunnableTarget = 0;
  155. pData->done.set();
  156. }
  157. void ThreadImpl::callableEntry(void* pThread, int, int, int, int, int, int, int, int, int)
  158. {
  159. taskVarAdd(0, reinterpret_cast<int*>(&_pCurrent));
  160. _pCurrent = reinterpret_cast<ThreadImpl*>(pThread);
  161. AutoPtr<ThreadData> pData = _pCurrent->_pData;
  162. try
  163. {
  164. pData->pCallbackTarget->callback(pData->pCallbackTarget->pData);
  165. }
  166. catch (Exception& exc)
  167. {
  168. ErrorHandler::handle(exc);
  169. }
  170. catch (std::exception& exc)
  171. {
  172. ErrorHandler::handle(exc);
  173. }
  174. catch (...)
  175. {
  176. ErrorHandler::handle();
  177. }
  178. pData->pCallbackTarget->callback = 0;
  179. pData->pCallbackTarget->pData = 0;
  180. pData->done.set();
  181. }
  182. int ThreadImpl::mapPrio(int prio)
  183. {
  184. int pmin = getMinOSPriorityImpl();
  185. int pmax = getMaxOSPriorityImpl();
  186. switch (prio)
  187. {
  188. case PRIO_LOWEST_IMPL:
  189. return pmin;
  190. case PRIO_LOW_IMPL:
  191. return pmin + (pmax - pmin)/4;
  192. case PRIO_NORMAL_IMPL:
  193. return pmin + (pmax - pmin)/2;
  194. case PRIO_HIGH_IMPL:
  195. return pmin + 3*(pmax - pmin)/4;
  196. case PRIO_HIGHEST_IMPL:
  197. return pmax;
  198. default:
  199. poco_bugcheck_msg("invalid thread priority");
  200. }
  201. return -1; // just to satisfy compiler - we'll never get here anyway
  202. }
  203. int ThreadImpl::reverseMapPrio(int prio)
  204. {
  205. int pmin = getMinOSPriorityImpl();
  206. int pmax = getMaxOSPriorityImpl();
  207. int normal = pmin + (pmax - pmin)/2;
  208. if (prio == pmax)
  209. return PRIO_HIGHEST_IMPL;
  210. if (prio > normal)
  211. return PRIO_HIGH_IMPL;
  212. else if (prio == normal)
  213. return PRIO_NORMAL_IMPL;
  214. else if (prio > pmin)
  215. return PRIO_LOW_IMPL;
  216. else
  217. return PRIO_LOWEST_IMPL;
  218. }
  219. } // namespace Poco