1
0

EventLoopThread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef HV_EVENT_LOOP_THREAD_HPP_
  2. #define HV_EVENT_LOOP_THREAD_HPP_
  3. #include <thread>
  4. #include "hlog.h"
  5. #include "EventLoop.h"
  6. namespace hv {
  7. class EventLoopThread : public Status {
  8. public:
  9. // Return 0 means OK, other failed.
  10. typedef std::function<int()> Functor;
  11. EventLoopThread(EventLoopPtr loop = NULL) {
  12. setStatus(kInitializing);
  13. loop_ = loop ? loop : std::make_shared<EventLoop>();
  14. setStatus(kInitialized);
  15. }
  16. ~EventLoopThread() {
  17. stop();
  18. join();
  19. }
  20. const EventLoopPtr& loop() {
  21. return loop_;
  22. }
  23. hloop_t* hloop() {
  24. return loop_->loop();
  25. }
  26. bool isRunning() {
  27. return loop_->isRunning();
  28. }
  29. // @param wait_thread_started: if ture this method will block until loop_thread started.
  30. // @param pre: This functor will be executed when loop_thread started.
  31. // @param post:This Functor will be executed when loop_thread stopped.
  32. void start(bool wait_thread_started = true,
  33. Functor pre = Functor(),
  34. Functor post = Functor()) {
  35. if (status() >= kStarting && status() < kStopped) return;
  36. if (isRunning()) return;
  37. setStatus(kStarting);
  38. thread_ = std::make_shared<std::thread>(&EventLoopThread::loop_thread, this, pre, post);
  39. if (wait_thread_started) {
  40. while (loop_->status() < kRunning) {
  41. hv_delay(1);
  42. }
  43. }
  44. }
  45. // @param wait_thread_started: if ture this method will block until loop_thread stopped.
  46. // stop thread-safe
  47. void stop(bool wait_thread_stopped = false) {
  48. if (status() < kStarting || status() >= kStopping) return;
  49. setStatus(kStopping);
  50. long loop_tid = loop_->tid();
  51. loop_->stop();
  52. if (wait_thread_stopped) {
  53. if (hv_gettid() == loop_tid) return;
  54. join();
  55. }
  56. }
  57. // @brief join loop_thread
  58. // @note destructor will join loop_thread if you forget to call this method.
  59. void join() {
  60. if (thread_ && thread_->joinable()) {
  61. thread_->join();
  62. thread_ = NULL;
  63. }
  64. }
  65. private:
  66. void loop_thread(const Functor& pre, const Functor& post) {
  67. hlogi("EventLoopThread started, tid=%ld", hv_gettid());
  68. setStatus(kStarted);
  69. if (pre) {
  70. loop_->queueInLoop([this, pre]{
  71. if (pre() != 0) {
  72. loop_->stop();
  73. }
  74. });
  75. }
  76. loop_->run();
  77. assert(loop_->isStopped());
  78. if (post) {
  79. post();
  80. }
  81. setStatus(kStopped);
  82. hlogi("EventLoopThread stopped, tid=%ld", hv_gettid());
  83. }
  84. private:
  85. EventLoopPtr loop_;
  86. std::shared_ptr<std::thread> thread_;
  87. };
  88. typedef std::shared_ptr<EventLoopThread> EventLoopThreadPtr;
  89. }
  90. #endif // HV_EVENT_LOOP_THREAD_HPP_