Thread.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // Thread.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Thread.cpp#2 $
  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.h"
  16. #include "Poco/Mutex.h"
  17. #include "Poco/Exception.h"
  18. #include "Poco/ThreadLocal.h"
  19. #include "Poco/AtomicCounter.h"
  20. #include <sstream>
  21. #if defined(POCO_OS_FAMILY_WINDOWS)
  22. #if defined(_WIN32_WCE)
  23. #include "Thread_WINCE.cpp"
  24. #else
  25. #include "Thread_WIN32.cpp"
  26. #endif
  27. #elif defined(POCO_VXWORKS)
  28. #include "Thread_VX.cpp"
  29. #else
  30. #include "Thread_POSIX.cpp"
  31. #endif
  32. namespace Poco {
  33. namespace {
  34. class RunnableHolder: public Runnable
  35. {
  36. public:
  37. RunnableHolder(Runnable& target):
  38. _target(target)
  39. {
  40. }
  41. ~RunnableHolder()
  42. {
  43. }
  44. void run()
  45. {
  46. _target.run();
  47. }
  48. private:
  49. Runnable& _target;
  50. };
  51. class CallableHolder: public Runnable
  52. {
  53. public:
  54. CallableHolder(Thread::Callable callable, void* pData):
  55. _callable(callable),
  56. _pData(pData)
  57. {
  58. }
  59. ~CallableHolder()
  60. {
  61. }
  62. void run()
  63. {
  64. _callable(_pData);
  65. }
  66. private:
  67. Thread::Callable _callable;
  68. void* _pData;
  69. };
  70. } // namespace
  71. Thread::Thread():
  72. _id(uniqueId()),
  73. _name(makeName()),
  74. _pTLS(0),
  75. _event(true)
  76. {
  77. }
  78. Thread::Thread(const std::string& name):
  79. _id(uniqueId()),
  80. _name(name),
  81. _pTLS(0),
  82. _event(true)
  83. {
  84. }
  85. Thread::~Thread()
  86. {
  87. delete _pTLS;
  88. }
  89. void Thread::setPriority(Priority prio)
  90. {
  91. setPriorityImpl(prio);
  92. }
  93. Thread::Priority Thread::getPriority() const
  94. {
  95. return Priority(getPriorityImpl());
  96. }
  97. void Thread::start(Runnable& target)
  98. {
  99. startImpl(new RunnableHolder(target));
  100. }
  101. void Thread::start(Callable target, void* pData)
  102. {
  103. startImpl(new CallableHolder(target, pData));
  104. }
  105. void Thread::join()
  106. {
  107. joinImpl();
  108. }
  109. void Thread::join(long milliseconds)
  110. {
  111. if (!joinImpl(milliseconds))
  112. throw TimeoutException();
  113. }
  114. bool Thread::tryJoin(long milliseconds)
  115. {
  116. return joinImpl(milliseconds);
  117. }
  118. bool Thread::trySleep(long milliseconds)
  119. {
  120. Thread* pT = Thread::current();
  121. poco_check_ptr(pT);
  122. return !(pT->_event.tryWait(milliseconds));
  123. }
  124. void Thread::wakeUp()
  125. {
  126. _event.set();
  127. }
  128. ThreadLocalStorage& Thread::tls()
  129. {
  130. if (!_pTLS)
  131. _pTLS = new ThreadLocalStorage;
  132. return *_pTLS;
  133. }
  134. void Thread::clearTLS()
  135. {
  136. if (_pTLS)
  137. {
  138. delete _pTLS;
  139. _pTLS = 0;
  140. }
  141. }
  142. std::string Thread::makeName()
  143. {
  144. std::ostringstream name;
  145. name << '#' << _id;
  146. return name.str();
  147. }
  148. int Thread::uniqueId()
  149. {
  150. static Poco::AtomicCounter counter;
  151. return ++counter;
  152. }
  153. void Thread::setName(const std::string& name)
  154. {
  155. FastMutex::ScopedLock lock(_mutex);
  156. _name = name;
  157. }
  158. } // namespace Poco