1 | /* |
1 | /* |
2 | iom.h -- I/O multiplexor |
2 | iom.h -- generic I/O multiplexor |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
19 | #ifndef VPE_IOM_H__ |
20 | #ifndef VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
21 | #define VPE_IOM_H__ |
21 | |
22 | |
22 | #include <vector> |
23 | #include <vector> |
23 | |
24 | |
24 | #include <sys/poll.h> |
25 | #include <cassert> |
25 | |
26 | |
|
|
27 | #include "poll.h" |
|
|
28 | |
|
|
29 | #include "callback.h" |
26 | #include "slog.h" |
30 | #include "slog.h" |
27 | |
31 | |
28 | typedef double tstamp; |
32 | typedef double tstamp; |
29 | |
33 | |
30 | extern tstamp NOW; |
34 | extern tstamp NOW; |
31 | |
35 | |
32 | template<class R, class A> class callback; |
|
|
33 | struct io_watcher; |
36 | struct io_watcher; |
34 | struct time_watcher; |
37 | struct time_watcher; |
35 | |
38 | |
36 | class io_manager { |
39 | class io_manager { |
37 | vector<pollfd> pfs; |
40 | vector<pollfd> pfs; |
38 | vector<const io_watcher *> iow; |
41 | vector<io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
42 | vector<time_watcher *> tw; // actually a heap |
40 | |
43 | |
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
44 | void idle_cb (time_watcher &w); time_watcher *idle; |
42 | public: |
45 | public: |
43 | |
46 | |
44 | void reschedule_time_watchers (); |
47 | void reschedule_time_watchers (); |
45 | |
48 | |
46 | // register a watcher |
49 | // register a watcher |
47 | void reg (int fd, short events, io_watcher *w); |
50 | void reg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
51 | void unreg (io_watcher *w); |
49 | void reg (time_watcher *w); |
52 | void reg (time_watcher *w); |
50 | void unreg (time_watcher *w); |
53 | void unreg (time_watcher *w); |
51 | |
54 | |
52 | void loop (); |
55 | void loop (); |
53 | |
56 | |
54 | io_manager (); |
57 | io_manager (); |
55 | ~io_manager (); |
58 | ~io_manager (); |
56 | }; |
59 | }; |
57 | |
60 | |
58 | extern io_manager iom; |
61 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
59 | |
62 | |
60 | template<class R, class A> |
63 | struct io_watcher : callback2<void, io_watcher &, short> { |
61 | class callback { |
64 | bool registered; // already registered? |
62 | struct object { }; |
65 | int fd; |
|
|
66 | short events; |
63 | |
67 | |
64 | void *obj; |
68 | template<class O1, class O2> |
65 | R (object::*meth)(A arg); |
69 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
|
|
70 | : callback2<void, io_watcher &, short>(object,method) |
|
|
71 | , registered(false) |
|
|
72 | { } |
66 | |
73 | |
67 | // a proxy is a kind of recipe on how to call a specific class method |
74 | ~io_watcher (); |
68 | struct proxy_base { |
|
|
69 | virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
|
|
70 | }; |
|
|
71 | template<class O1, class O2> |
|
|
72 | struct proxy : proxy_base { |
|
|
73 | virtual R call (void *obj, R (object::*meth)(A), A arg) |
|
|
74 | { |
|
|
75 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
|
|
76 | (arg); |
|
|
77 | } |
|
|
78 | }; |
|
|
79 | |
75 | |
80 | proxy_base *prxy; |
76 | void set(int fd_, short events_); |
81 | |
77 | |
82 | public: |
78 | void set(short events_) |
83 | template<class O1, class O2> |
|
|
84 | callback (O1 *object, R (O2::*method)(A)) |
|
|
85 | { |
79 | { |
86 | static proxy<O1,O2> p; |
80 | set (fd, events_); |
87 | obj = reinterpret_cast<void *>(object); |
|
|
88 | meth = reinterpret_cast<R (object::*)(A)>(method); |
|
|
89 | prxy = &p; |
|
|
90 | } |
81 | } |
91 | |
82 | |
92 | R call(A arg) const |
83 | void start (int fd_, short events_) |
93 | { |
84 | { |
94 | return prxy->call (obj, meth, arg); |
85 | set (fd_, events_); |
95 | } |
|
|
96 | |
|
|
97 | R operator ()(A arg) const |
|
|
98 | { |
|
|
99 | return call (arg); |
|
|
100 | } |
|
|
101 | }; |
|
|
102 | |
|
|
103 | struct io_watcher : callback<void, short> { |
|
|
104 | template<class O1, class O2> |
|
|
105 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
|
|
106 | : callback<void, short>(object,method) |
|
|
107 | { } |
|
|
108 | |
|
|
109 | ~io_watcher () |
|
|
110 | { |
|
|
111 | iom.unreg (this); |
86 | iom.reg (this); |
112 | } |
|
|
113 | |
|
|
114 | void start (int fd, short events) |
|
|
115 | { |
|
|
116 | iom.reg (fd, events, this); |
|
|
117 | } |
87 | } |
118 | |
88 | |
119 | void stop () |
89 | void stop () |
120 | { |
90 | { |
121 | iom.unreg (this); |
91 | iom.unreg (this); |
122 | } |
92 | } |
123 | }; |
93 | }; |
124 | |
94 | |
125 | #define TSTAMP_CANCEL -1. |
95 | #define TSTAMP_CANCEL -1. |
126 | |
96 | |
127 | struct time_watcher : callback<void, tstamp &> { |
97 | struct time_watcher : callback1<void, time_watcher &> { |
128 | bool registered; // already registered? |
98 | bool registered; // already registered? |
129 | tstamp at; |
99 | tstamp at; |
130 | |
100 | |
131 | template<class O1, class O2> |
101 | template<class O1, class O2> |
132 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
102 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
133 | : callback<void, tstamp &>(object,method) |
103 | : callback1<void, time_watcher &>(object,method) |
134 | , registered(false) |
104 | , registered(false) |
135 | { } |
105 | { } |
136 | |
106 | |
137 | ~time_watcher () |
107 | ~time_watcher (); |
138 | { |
|
|
139 | iom.unreg (this); |
|
|
140 | } |
|
|
141 | |
108 | |
142 | void set (tstamp when); |
109 | void set (tstamp when); |
143 | void trigger (); |
110 | void trigger (); |
144 | |
111 | |
145 | void operator ()() |
112 | void operator ()() |
146 | { |
113 | { |
147 | trigger (); |
114 | trigger (); |
148 | } |
115 | } |
149 | |
116 | |
150 | void start (); |
117 | void start () |
|
|
118 | { |
|
|
119 | iom.reg (this); |
|
|
120 | } |
|
|
121 | |
151 | void start (tstamp when) |
122 | void start (tstamp when) |
152 | { |
123 | { |
153 | set (when); |
124 | set (when); |
|
|
125 | iom.reg (this); |
154 | } |
126 | } |
155 | |
127 | |
156 | void stop () |
128 | void stop () |
157 | { |
129 | { |
158 | iom.unreg (this); |
130 | iom.unreg (this); |