hthread.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef H_THREAD_H
  2. #define H_THREAD_H
  3. #include "hdef.h"
  4. #include "hplatform.h"
  5. #include <thread>
  6. #include <atomic>
  7. #include <chrono>
  8. #ifdef _MSC_VER
  9. inline uint32 getpid(){
  10. return GetCurrentProcessId();
  11. }
  12. #endif
  13. inline uint32 gettid(){
  14. #ifdef _MSC_VER
  15. return GetCurrentThreadId();
  16. #else
  17. return pthread_self();
  18. #endif
  19. }
  20. /************************************************
  21. * HThread
  22. * Status: STOP,RUNNING,PAUSE
  23. * Control: start,stop,pause,resume
  24. * first-level virtual: doTask
  25. * second-level virtual: run
  26. ************************************************/
  27. class HThread{
  28. public:
  29. HThread() {
  30. status = STOP;
  31. status_switch = false;
  32. sleep_policy = YIELD;
  33. sleep_ms = 0;
  34. }
  35. virtual ~HThread() {
  36. }
  37. virtual int start() {
  38. if (status == STOP) {
  39. switchStatus(RUNNING);
  40. dotask_cnt = 0;
  41. thread = std::thread([this]{
  42. doPrepare();
  43. run();
  44. doFinish();
  45. });
  46. }
  47. return 0;
  48. }
  49. virtual int stop() {
  50. if (status != STOP) {
  51. switchStatus(STOP);
  52. thread.join(); // wait thread exit
  53. }
  54. return 0;
  55. }
  56. virtual int pause() {
  57. if (status == RUNNING) {
  58. switchStatus(PAUSE);
  59. }
  60. return 0;
  61. }
  62. virtual int resume() {
  63. if (status == PAUSE) {
  64. switchStatus(RUNNING);
  65. }
  66. return 0;
  67. }
  68. virtual void run() {
  69. while (status != STOP) {
  70. while (status == PAUSE) {
  71. std::this_thread::yield();
  72. }
  73. doTask();
  74. dotask_cnt++;
  75. sleep();
  76. }
  77. }
  78. virtual void doPrepare() {}
  79. virtual void doTask() {}
  80. virtual void doFinish() {}
  81. std::thread thread;
  82. enum Status {
  83. STOP,
  84. RUNNING,
  85. PAUSE,
  86. };
  87. std::atomic<Status> status;
  88. uint32 dotask_cnt;
  89. enum SleepPolicy {
  90. YIELD,
  91. SLEEP_FOR,
  92. SLEEP_UNTIL,
  93. NO_SLEEP,
  94. };
  95. void setSleepPolicy(SleepPolicy policy, long long ms = 0) {
  96. status_switch = true;
  97. sleep_policy = policy;
  98. sleep_ms = ms;
  99. }
  100. protected:
  101. void switchStatus(Status stat) {
  102. status_switch = true;
  103. status = stat;
  104. }
  105. void sleep() {
  106. switch (sleep_policy) {
  107. case YIELD:
  108. std::this_thread::yield();
  109. break;
  110. case SLEEP_FOR:
  111. std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
  112. break;
  113. case SLEEP_UNTIL: {
  114. if (status_switch) {
  115. status_switch = false;
  116. base_tp = std::chrono::system_clock::now();
  117. }
  118. base_tp += std::chrono::milliseconds(sleep_ms);
  119. std::this_thread::sleep_until(base_tp);
  120. }
  121. break;
  122. default: // donothing, go all out.
  123. break;
  124. }
  125. }
  126. std::atomic<bool> status_switch;
  127. SleepPolicy sleep_policy;
  128. std::chrono::system_clock::time_point base_tp; // for SLEEP_UNTIL
  129. long long sleep_ms;
  130. };
  131. #endif // H_THREAD_H