1
0

EventLoopThread.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. if (loop) {
  14. loop_ = loop;
  15. } else {
  16. loop_.reset(new EventLoop);
  17. }
  18. setStatus(kInitialized);
  19. }
  20. ~EventLoopThread() {
  21. stop();
  22. join();
  23. }
  24. const EventLoopPtr& loop() {
  25. return loop_;
  26. }
  27. hloop_t* hloop() {
  28. return loop_->loop();
  29. }
  30. bool isRunning() {
  31. return loop_->isRunning();
  32. }
  33. // @param wait_thread_started: if ture this method will block until loop_thread started.
  34. // @param pre: This functor will be executed when loop_thread started.
  35. // @param post:This Functor will be executed when loop_thread stopped.
  36. void start(bool wait_thread_started = true,
  37. Functor pre = Functor(),
  38. Functor post = Functor()) {
  39. setStatus(kStarting);
  40. assert(thread_.get() == NULL);
  41. thread_.reset(new std::thread(&EventLoopThread::loop_thread, this, pre, post));
  42. if (wait_thread_started) {
  43. while (loop_->status() < kRunning) {
  44. hv_delay(1);
  45. }
  46. }
  47. }
  48. // @param wait_thread_started: if ture this method will block until loop_thread stopped.
  49. void stop(bool wait_thread_stopped = false) {
  50. if (status() >= kStopping) return;
  51. setStatus(kStopping);
  52. loop_->stop();
  53. if (wait_thread_stopped) {
  54. while (!isStopped()) {
  55. hv_delay(1);
  56. }
  57. }
  58. }
  59. // @brief join loop_thread
  60. // @note destructor will join loop_thread if you forget to call this method.
  61. void join() {
  62. if (thread_ && thread_->joinable()) {
  63. thread_->join();
  64. thread_ = NULL;
  65. }
  66. }
  67. private:
  68. void loop_thread(const Functor& pre, const Functor& post) {
  69. hlogi("EventLoopThread started, tid=%ld", hv_gettid());
  70. setStatus(kStarted);
  71. if (pre) {
  72. loop_->queueInLoop([this, pre]{
  73. if (pre() != 0) {
  74. loop_->stop();
  75. }
  76. });
  77. }
  78. loop_->run();
  79. assert(loop_->isStopped());
  80. if (post) {
  81. post();
  82. }
  83. setStatus(kStopped);
  84. hlogi("EventLoopThread stopped, tid=%ld", hv_gettid());
  85. }
  86. private:
  87. EventLoopPtr loop_;
  88. std::shared_ptr<std::thread> thread_;
  89. };
  90. typedef std::shared_ptr<EventLoopThread> EventLoopThreadPtr;
  91. }
  92. #endif // HV_EVENT_LOOP_THREAD_HPP_