hthread.h 3.1 KB

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