Thread_WINCE.h 3.2 KB

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