|
UniSet
2.6.0
|
00001 // ------------------------------------------------------------------------- 00002 #ifndef CommonEventLoop_H_ 00003 #define CommonEventLoop_H_ 00004 // ------------------------------------------------------------------------- 00005 #include <ev++.h> 00006 #include <atomic> 00007 #include <thread> 00008 #include <mutex> 00009 #include <condition_variable> 00010 #include <vector> 00011 // ------------------------------------------------------------------------- 00012 namespace uniset 00013 { 00014 00015 00016 class EvWatcher 00017 { 00018 public: 00019 EvWatcher() {} 00020 virtual ~EvWatcher() {} 00021 00022 // подготовка перед запуском loop: 00023 // запуск своих ev::xxx.start() 00024 virtual void evprepare( const ev::loop_ref& ) {} 00025 00026 // действия при завершении: 00027 // вызов своих ev::xxx.stop() 00028 virtual void evfinish( const ev::loop_ref& ) {} 00029 00030 virtual std::string wname() const noexcept 00031 { 00032 return ""; 00033 } 00034 }; 00035 // ------------------------------------------------------------------------- 00052 class CommonEventLoop 00053 { 00054 public: 00055 00056 CommonEventLoop() noexcept; 00057 ~CommonEventLoop(); 00058 00059 bool evIsActive() const noexcept; 00060 00068 bool evrun( EvWatcher* w, bool thread = true, size_t waitPrepareTimeout_msec = 8000); 00069 00071 bool evstop( EvWatcher* w ); 00072 00073 inline const ev::loop_ref evloop() noexcept 00074 { 00075 return loop; 00076 } 00077 00078 // количество зарегистрированных wather-ов 00079 size_t size() const; 00080 00081 protected: 00082 00083 private: 00084 00085 void onStop() noexcept; 00086 void onPrepare() noexcept; 00087 void defaultLoop() noexcept; 00088 00089 std::atomic_bool cancelled = { false }; 00090 std::atomic_bool isrunning = { false }; 00091 00092 ev::dynamic_loop loop; 00093 ev::async evterm; 00094 std::shared_ptr<std::thread> thr; 00095 std::mutex thr_mutex; 00096 00097 std::mutex term_mutex; 00098 std::condition_variable term_event; 00099 std::atomic_bool term_notify = { false }; 00100 00101 std::mutex wlist_mutex; 00102 std::vector<EvWatcher*> wlist; 00103 00104 // готовящийся Watcher..он может быть только один в единицу времени 00105 // это гарантирует prep_mutex 00106 EvWatcher* wprep = { nullptr }; 00107 ev::async evprep; 00108 std::condition_variable prep_event; 00109 std::mutex prep_mutex; 00110 std::atomic_bool prep_notify = { false }; 00111 }; 00112 // ------------------------------------------------------------------------- 00113 } // end of uniset namespace 00114 // ------------------------------------------------------------------------- 00115 #endif // CommonEventLoop_H_ 00116 // -------------------------------------------------------------------------
1.7.6.1