Thread_WINCE.h 3.2 KB

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