… | |
… | |
19 | #ifndef VPE_IOM_H__ |
19 | #ifndef VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
21 | |
21 | |
22 | #include <vector> |
22 | #include <vector> |
23 | |
23 | |
|
|
24 | #include <cassert> |
|
|
25 | |
24 | #include <sys/poll.h> |
26 | #include <sys/poll.h> |
25 | |
27 | |
26 | #include "callback.h" |
28 | #include "callback.h" |
27 | #include "slog.h" |
29 | #include "slog.h" |
28 | |
30 | |
… | |
… | |
33 | struct io_watcher; |
35 | struct io_watcher; |
34 | struct time_watcher; |
36 | struct time_watcher; |
35 | |
37 | |
36 | class io_manager { |
38 | class io_manager { |
37 | vector<pollfd> pfs; |
39 | vector<pollfd> pfs; |
38 | vector<const io_watcher *> iow; |
40 | vector<io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
41 | vector<time_watcher *> tw; // actually a heap |
40 | |
42 | |
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
43 | void idle_cb (time_watcher &w); time_watcher *idle; |
42 | public: |
44 | public: |
43 | |
45 | |
44 | void reschedule_time_watchers (); |
46 | void reschedule_time_watchers (); |
45 | |
47 | |
46 | // register a watcher |
48 | // register a watcher |
47 | void reg (int fd, short events, io_watcher *w); |
49 | void reg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
50 | void unreg (io_watcher *w); |
49 | void reg (time_watcher *w); |
51 | void reg (time_watcher *w); |
50 | void unreg (time_watcher *w); |
52 | void unreg (time_watcher *w); |
51 | |
53 | |
52 | void loop (); |
54 | void loop (); |
53 | |
55 | |
54 | io_manager (); |
56 | io_manager (); |
55 | ~io_manager (); |
57 | ~io_manager (); |
56 | }; |
58 | }; |
57 | |
59 | |
58 | extern io_manager iom; |
60 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
59 | |
61 | |
60 | struct io_watcher : callback<void, short> { |
62 | struct io_watcher : callback2<void, io_watcher &, short> { |
|
|
63 | pollfd *p; |
|
|
64 | |
61 | template<class O1, class O2> |
65 | template<class O1, class O2> |
62 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
66 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
63 | : callback<void, short>(object,method) |
67 | : callback2<void, io_watcher &, short>(object,method) |
64 | { } |
68 | { } |
65 | |
69 | |
66 | ~io_watcher () |
70 | ~io_watcher (); |
|
|
71 | |
|
|
72 | void set(int fd, short events) |
67 | { |
73 | { |
68 | iom.unreg (this); |
74 | assert (p); |
|
|
75 | p->fd = fd; |
|
|
76 | p->events = events; |
|
|
77 | } |
|
|
78 | |
|
|
79 | void set(short events) |
|
|
80 | { |
|
|
81 | assert (p); |
|
|
82 | p->events = events; |
69 | } |
83 | } |
70 | |
84 | |
71 | void start (int fd, short events) |
85 | void start (int fd, short events) |
72 | { |
86 | { |
73 | iom.reg (fd, events, this); |
87 | iom.reg (this); // make sure pfd is set |
|
|
88 | |
|
|
89 | p->fd = fd; |
|
|
90 | p->events = events; |
74 | } |
91 | } |
75 | |
92 | |
76 | void stop () |
93 | void stop () |
77 | { |
94 | { |
78 | iom.unreg (this); |
95 | iom.unreg (this); |
79 | } |
96 | } |
80 | }; |
97 | }; |
81 | |
98 | |
82 | #define TSTAMP_CANCEL -1. |
99 | #define TSTAMP_CANCEL -1. |
83 | |
100 | |
84 | struct time_watcher : callback<void, tstamp &> { |
101 | struct time_watcher : callback1<void, time_watcher &> { |
85 | bool registered; // already registered? |
102 | bool registered; // already registered? |
86 | tstamp at; |
103 | tstamp at; |
87 | |
104 | |
88 | template<class O1, class O2> |
105 | template<class O1, class O2> |
89 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
106 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
90 | : callback<void, tstamp &>(object,method) |
107 | : callback1<void, time_watcher &>(object,method) |
91 | , registered(false) |
108 | , registered(false) |
92 | { } |
109 | { } |
93 | |
110 | |
94 | ~time_watcher () |
111 | ~time_watcher (); |
95 | { |
|
|
96 | iom.unreg (this); |
|
|
97 | } |
|
|
98 | |
112 | |
99 | void set (tstamp when); |
113 | void set (tstamp when); |
100 | void trigger (); |
114 | void trigger (); |
101 | |
115 | |
102 | void operator ()() |
116 | void operator ()() |
… | |
… | |
116 | } |
130 | } |
117 | |
131 | |
118 | void reset (tstamp when = TSTAMP_CANCEL) |
132 | void reset (tstamp when = TSTAMP_CANCEL) |
119 | { |
133 | { |
120 | stop (); |
134 | stop (); |
|
|
135 | |
121 | at = when; |
136 | at = when; |
122 | } |
137 | } |
123 | }; |
138 | }; |
124 | |
139 | |
125 | #endif |
140 | #endif |