Thread_WIN32.h 3.2 KB

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