UniSet 2.32.1
CommonEventLoop.h
1// -------------------------------------------------------------------------
2#ifndef CommonEventLoop_H_
3#define CommonEventLoop_H_
4// -------------------------------------------------------------------------
5#include <ev++.h>
6#include <atomic>
7#include <thread>
8#include <mutex>
9#include <condition_variable>
10#include <vector>
11#include <queue>
12#include <future>
13// -------------------------------------------------------------------------
14namespace uniset
15{
16
17
19 {
20 public:
21 EvWatcher() {}
22 virtual ~EvWatcher() {}
23
24 // подготовка перед запуском loop:
25 // запуск своих ev::xxx.start()
26 virtual void evprepare( const ev::loop_ref& ) {}
27
28 // действия при завершении:
29 // вызов своих ev::xxx.stop()
30 virtual void evfinish( const ev::loop_ref& ) {}
31
32 virtual std::string wname() const noexcept
33 {
34 return "";
35 }
36 };
37 // -------------------------------------------------------------------------
55 {
56 public:
57
58 CommonEventLoop() noexcept;
60
61 bool evIsActive() const noexcept;
62
67 bool evrun( EvWatcher* w, size_t prepareTimeout_msec = 60000);
68
73 bool async_evrun( EvWatcher* w, size_t prepareTimeout_msec = 60000 );
74
76 bool evstop( EvWatcher* w );
77
78 inline const ev::loop_ref evloop() noexcept
79 {
80 return loop;
81 }
82
83 // количество зарегистрированных wather-ов
84 size_t size() const;
85
86 protected:
87
88 private:
89
90 void onStop( ev::async& w, int revents ) noexcept;
91 void onPrepare( ev::async& w, int revents ) noexcept;
92 void defaultLoop() noexcept;
93 bool runDefaultLoop( size_t waitTimeout_msec );
94 bool activateWatcher( EvWatcher* w, size_t waitTimeout_msec );
95 void onLoopOK( ev::timer& t, int revents ) noexcept;
96
97 std::atomic_bool cancelled = { false };
98 std::atomic_bool isrunning = { false };
99
100 ev::dynamic_loop loop;
101 ev::async evterm;
102 std::unique_ptr<std::thread> thr;
103 std::mutex thr_mutex;
104
105 std::mutex term_mutex;
106 std::condition_variable term_event;
107 std::atomic_bool term_notify = { false };
108
109 std::mutex wlist_mutex;
110 std::vector<EvWatcher*> wlist;
111
112 // готовящийся Watcher. Он может быть только один в единицу времени
113 // это гарантирует prep_mutex
114 EvWatcher* wprep = { nullptr };
115 ev::async evprep;
116 std::condition_variable prep_event;
117 std::mutex prep_mutex;
118 std::atomic_bool prep_notify = { false };
119
120
121 std::mutex looprunOK_mutex;
122 std::condition_variable looprunOK_event;
123 ev::timer evruntimer;
124 };
125 // -------------------------------------------------------------------------
126} // end of uniset namespace
127// -------------------------------------------------------------------------
128#endif // CommonEventLoop_H_
129// -------------------------------------------------------------------------
The CommonEventLoop class Реализация механизма "один eventloop, много подписчиков" (libev)....
Definition CommonEventLoop.h:55
bool evstop(EvWatcher *w)
Definition CommonEventLoop.cc:160
bool async_evrun(EvWatcher *w, size_t prepareTimeout_msec=60000)
Definition CommonEventLoop.cc:134
bool evrun(EvWatcher *w, size_t prepareTimeout_msec=60000)
Definition CommonEventLoop.cc:98
Definition CommonEventLoop.h:19
Definition Calibration.h:27