Thread_WIN32.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Thread_WIN32.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Thread_WIN32.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Thread
  9. //
  10. // Definition of the ThreadImpl class for WIN32.
  11. //
  12. // Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Thread_WIN32_INCLUDED
  18. #define Foundation_Thread_WIN32_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Runnable.h"
  21. #include "Poco/SharedPtr.h"
  22. #include "Poco/UnWindows.h"
  23. namespace Poco {
  24. class Foundation_API ThreadImpl
  25. {
  26. public:
  27. typedef DWORD TIDImpl;
  28. typedef void (*Callable)(void*);
  29. #if defined(_DLL)
  30. typedef DWORD (WINAPI *Entry)(LPVOID);
  31. #else
  32. typedef unsigned (__stdcall *Entry)(void*);
  33. #endif
  34. enum Priority
  35. {
  36. PRIO_LOWEST_IMPL = THREAD_PRIORITY_LOWEST,
  37. PRIO_LOW_IMPL = THREAD_PRIORITY_BELOW_NORMAL,
  38. PRIO_NORMAL_IMPL = THREAD_PRIORITY_NORMAL,
  39. PRIO_HIGH_IMPL = THREAD_PRIORITY_ABOVE_NORMAL,
  40. PRIO_HIGHEST_IMPL = THREAD_PRIORITY_HIGHEST
  41. };
  42. enum Policy
  43. {
  44. POLICY_DEFAULT_IMPL = 0
  45. };
  46. ThreadImpl();
  47. ~ThreadImpl();
  48. TIDImpl tidImpl() const;
  49. void setPriorityImpl(int prio);
  50. int getPriorityImpl() const;
  51. void setOSPriorityImpl(int prio, int policy = 0);
  52. int getOSPriorityImpl() const;
  53. static int getMinOSPriorityImpl(int policy);
  54. static int getMaxOSPriorityImpl(int policy);
  55. void setStackSizeImpl(int size);
  56. int getStackSizeImpl() const;
  57. void startImpl(SharedPtr<Runnable> pTarget);
  58. void joinImpl();
  59. bool joinImpl(long milliseconds);
  60. bool isRunningImpl() const;
  61. static void sleepImpl(long milliseconds);
  62. static void yieldImpl();
  63. static ThreadImpl* currentImpl();
  64. static TIDImpl currentTidImpl();
  65. protected:
  66. #if defined(_DLL)
  67. static DWORD WINAPI runnableEntry(LPVOID pThread);
  68. #else
  69. static unsigned __stdcall runnableEntry(void* pThread);
  70. #endif
  71. void createImpl(Entry ent, void* pData);
  72. void threadCleanup();
  73. private:
  74. class CurrentThreadHolder
  75. {
  76. public:
  77. CurrentThreadHolder(): _slot(TlsAlloc())
  78. {
  79. if (_slot == TLS_OUT_OF_INDEXES)
  80. throw SystemException("cannot allocate thread context key");
  81. }
  82. ~CurrentThreadHolder()
  83. {
  84. TlsFree(_slot);
  85. }
  86. ThreadImpl* get() const
  87. {
  88. return reinterpret_cast<ThreadImpl*>(TlsGetValue(_slot));
  89. }
  90. void set(ThreadImpl* pThread)
  91. {
  92. TlsSetValue(_slot, pThread);
  93. }
  94. private:
  95. DWORD _slot;
  96. };
  97. SharedPtr<Runnable> _pRunnableTarget;
  98. HANDLE _thread;
  99. DWORD _threadId;
  100. int _prio;
  101. int _stackSize;
  102. static CurrentThreadHolder _currentThreadHolder;
  103. };
  104. //
  105. // inlines
  106. //
  107. inline int ThreadImpl::getPriorityImpl() const
  108. {
  109. return _prio;
  110. }
  111. inline int ThreadImpl::getOSPriorityImpl() const
  112. {
  113. return _prio;
  114. }
  115. inline int ThreadImpl::getMinOSPriorityImpl(int /* policy */)
  116. {
  117. return PRIO_LOWEST_IMPL;
  118. }
  119. inline int ThreadImpl::getMaxOSPriorityImpl(int /* policy */)
  120. {
  121. return PRIO_HIGHEST_IMPL;
  122. }
  123. inline void ThreadImpl::sleepImpl(long milliseconds)
  124. {
  125. Sleep(DWORD(milliseconds));
  126. }
  127. inline void ThreadImpl::yieldImpl()
  128. {
  129. Sleep(0);
  130. }
  131. inline void ThreadImpl::setStackSizeImpl(int size)
  132. {
  133. _stackSize = size;
  134. }
  135. inline int ThreadImpl::getStackSizeImpl() const
  136. {
  137. return _stackSize;
  138. }
  139. inline ThreadImpl::TIDImpl ThreadImpl::tidImpl() const
  140. {
  141. return _threadId;
  142. }
  143. } // namespace Poco
  144. #endif // Foundation_Thread_WIN32_INCLUDED