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 | |
|
|
25 | #include <cassert> |
|
|
26 | |
24 | #include <sys/poll.h> |
27 | #include "poll.h" |
25 | |
28 | |
26 | #include "callback.h" |
29 | #include "callback.h" |
27 | #include "slog.h" |
30 | #include "slog.h" |
28 | |
31 | |
29 | typedef double tstamp; |
32 | typedef double tstamp; |
… | |
… | |
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 | struct io_watcher : callback<void, short> { |
63 | struct io_watcher : callback2<void, io_watcher &, short> { |
|
|
64 | bool registered; // already registered? |
|
|
65 | int fd; |
|
|
66 | short events; |
|
|
67 | |
61 | template<class O1, class O2> |
68 | template<class O1, class O2> |
62 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
69 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
63 | : callback<void, short>(object,method) |
70 | : callback2<void, io_watcher &, short>(object,method) |
|
|
71 | , registered(false) |
64 | { } |
72 | { } |
65 | |
73 | |
66 | ~io_watcher () |
74 | ~io_watcher (); |
|
|
75 | |
|
|
76 | void set(int fd_, short events_); |
|
|
77 | |
|
|
78 | void set(short events_) |
67 | { |
79 | { |
68 | iom.unreg (this); |
80 | set (fd, events_); |
69 | } |
81 | } |
70 | |
82 | |
71 | void start (int fd, short events) |
83 | void start (int fd_, short events_) |
72 | { |
84 | { |
|
|
85 | set (fd_, events_); |
73 | iom.reg (fd, events, this); |
86 | iom.reg (this); |
74 | } |
87 | } |
75 | |
88 | |
76 | void stop () |
89 | void stop () |
77 | { |
90 | { |
78 | iom.unreg (this); |
91 | iom.unreg (this); |
79 | } |
92 | } |
80 | }; |
93 | }; |
81 | |
94 | |
82 | #define TSTAMP_CANCEL -1. |
95 | #define TSTAMP_CANCEL -1. |
83 | |
96 | |
84 | struct time_watcher : callback<void, tstamp &> { |
97 | struct time_watcher : callback1<void, time_watcher &> { |
85 | bool registered; // already registered? |
98 | bool registered; // already registered? |
86 | tstamp at; |
99 | tstamp at; |
87 | |
100 | |
88 | template<class O1, class O2> |
101 | template<class O1, class O2> |
89 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
102 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
90 | : callback<void, tstamp &>(object,method) |
103 | : callback1<void, time_watcher &>(object,method) |
91 | , registered(false) |
104 | , registered(false) |
92 | { } |
105 | { } |
93 | |
106 | |
94 | ~time_watcher () |
107 | ~time_watcher (); |
95 | { |
|
|
96 | iom.unreg (this); |
|
|
97 | } |
|
|
98 | |
108 | |
99 | void set (tstamp when); |
109 | void set (tstamp when); |
100 | void trigger (); |
110 | void trigger (); |
101 | |
111 | |
102 | void operator ()() |
112 | void operator ()() |
103 | { |
113 | { |
104 | trigger (); |
114 | trigger (); |
105 | } |
115 | } |
106 | |
116 | |
107 | void start (); |
117 | void start () |
|
|
118 | { |
|
|
119 | iom.reg (this); |
|
|
120 | } |
|
|
121 | |
108 | void start (tstamp when) |
122 | void start (tstamp when) |
109 | { |
123 | { |
110 | set (when); |
124 | set (when); |
|
|
125 | iom.reg (this); |
111 | } |
126 | } |
112 | |
127 | |
113 | void stop () |
128 | void stop () |
114 | { |
129 | { |
115 | iom.unreg (this); |
130 | iom.unreg (this); |