Thread.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __THREAD_H
  6. #define __THREAD_H
  7. #include "Types.h"
  8. namespace tzc {
  9. class DECLDLL OSThread {
  10. public:
  11. #if defined(WIN32) || defined(WINDOWS)
  12. typedef DWORD STTid; // just a integer, represent for thread ID
  13. typedef DWORD STKey;
  14. typedef HANDLE STAttr; // use to handle the thread, is different from Linux
  15. #elif defined(LINUX)
  16. typedef pthread_t STTid;
  17. typedef pthread_key_t STKey;
  18. typedef pthread_attr_t STAttr;
  19. #endif
  20. public:
  21. OSThread();
  22. virtual ~OSThread();
  23. virtual void Entry() = 0;
  24. void Start();
  25. void Stop();
  26. void StopAndWait();
  27. void Join();
  28. TZ_BOOL IsRunning() const;
  29. TZ_BOOL IsStop() const;
  30. TZ_BOOL SetStackSize(const TZ_Int32 iSize); // in Bytes
  31. TZ_Int32 GetStackSize() const;
  32. TZ_BOOL SetCPUIndex(const TZ_Int32 iIndex);
  33. TZ_Int32 GetCPUIndex() const;
  34. TZ_BOOL SetPolicy(const TZ_Int32 iPolicy);
  35. TZ_Int32 GetPolicy() const;
  36. TZ_BOOL SetPriority(const TZ_Int32 iPriority);
  37. TZ_Int32 GetPriority() const;
  38. TZ_BOOL SetThreadName(const std::string & sName);
  39. const std::string & GetThreadName();
  40. STTid GetTid() const;
  41. static STTid CurrentTid();
  42. static OSThread * Current();
  43. static const std::string & CurrentName();
  44. protected:
  45. #if defined(WIN32) || defined(WINDOWS)
  46. static unsigned WINAPI __Running(void *arg);
  47. #elif defined(LINUX)
  48. static void * __Running(void *arg);
  49. #endif
  50. private:
  51. volatile TZ_BOOL m_bRunning;
  52. volatile TZ_BOOL m_bStop;
  53. TZ_Int32 m_iStackSize;
  54. TZ_Int32 m_iPolicy;
  55. TZ_Int32 m_iPriority;
  56. TZ_Int32 m_iCPUIndex;
  57. STTid m_threadID;
  58. STAttr m_threadAttr;
  59. std::string m_sName;
  60. class ThreadHolder {
  61. public:
  62. ThreadHolder()
  63. {
  64. #if defined(WIN32) || defined(WINDOWS)
  65. m_key = TlsAlloc();
  66. if (TLS_OUT_OF_INDEXES == m_key)
  67. throw std::runtime_error("Create thread key failed");
  68. #elif defined(LINUX)
  69. if (pthread_key_create(&m_key, NULL))
  70. throw std::runtime_error("Create thread key failed");
  71. #endif
  72. }
  73. ~ThreadHolder()
  74. {
  75. #if defined(WIN32) || defined(WINDOWS)
  76. TlsFree(m_key);
  77. #elif defined(LINUX)
  78. pthread_key_delete(m_key);
  79. #endif
  80. }
  81. void * Get() const
  82. {
  83. #if defined(WIN32) || defined(WINDOWS)
  84. return TlsGetValue(m_key);
  85. #elif defined(LINUX)
  86. return pthread_getspecific(m_key);
  87. #endif
  88. }
  89. void Set(void *pData)
  90. {
  91. #if defined(WIN32) || defined(WINDOWS)
  92. TlsSetValue(m_key, pData);
  93. #elif defined(LINUX)
  94. pthread_setspecific(m_key, pData);
  95. #endif
  96. }
  97. private:
  98. STKey m_key;
  99. };
  100. static ThreadHolder m_holder;
  101. };
  102. //
  103. // inlines
  104. //
  105. inline TZ_BOOL OSThread::IsRunning() const
  106. {
  107. return !m_bStop;
  108. }
  109. inline TZ_BOOL OSThread::IsStop() const
  110. {
  111. return m_bStop;
  112. }
  113. inline TZ_Int32 OSThread::GetStackSize() const
  114. {
  115. if (m_bRunning)
  116. return m_iStackSize;
  117. return -1;
  118. }
  119. inline TZ_Int32 OSThread::GetCPUIndex() const
  120. {
  121. if (m_bRunning)
  122. return m_iCPUIndex;
  123. return -1;
  124. }
  125. inline TZ_Int32 OSThread::GetPolicy() const
  126. {
  127. if (m_bRunning)
  128. return m_iPolicy;
  129. return -1;
  130. }
  131. inline TZ_Int32 OSThread::GetPriority() const
  132. {
  133. if (m_bRunning)
  134. return m_iPriority;
  135. return -1;
  136. }
  137. inline OSThread::STTid OSThread::GetTid() const
  138. {
  139. if (m_bRunning)
  140. return m_threadID;
  141. return static_cast<STTid>(-1);
  142. }
  143. } // namespace tzc
  144. #endif /* ----- #ifndef __THREAD_H ----- */