1 | /* |
1 | /* |
2 | iom.C -- I/O multiplexor |
2 | iom.C -- 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. |
… | |
… | |
21 | #include <sys/time.h> |
22 | #include <sys/time.h> |
22 | |
23 | |
23 | #include <algorithm> |
24 | #include <algorithm> |
24 | #include <functional> |
25 | #include <functional> |
25 | |
26 | |
|
|
27 | #include "gettext.h" |
|
|
28 | |
26 | #include "slog.h" |
29 | #include "slog.h" |
27 | #include "iom.h" |
30 | #include "iom.h" |
28 | |
31 | |
29 | tstamp NOW; |
32 | tstamp NOW; |
30 | bool iom_valid; |
33 | bool iom_valid; |
… | |
… | |
36 | } |
39 | } |
37 | |
40 | |
38 | void time_watcher::set (tstamp when) |
41 | void time_watcher::set (tstamp when) |
39 | { |
42 | { |
40 | at = when; |
43 | at = when; |
|
|
44 | |
|
|
45 | if (registered) |
|
|
46 | iom.reschedule_time_watchers (); |
|
|
47 | } |
|
|
48 | |
|
|
49 | void time_watcher::trigger () |
|
|
50 | { |
|
|
51 | call (*this); |
41 | |
52 | |
42 | if (registered) |
53 | if (registered) |
43 | iom.reschedule_time_watchers (); |
54 | iom.reschedule_time_watchers (); |
44 | else |
55 | else |
45 | iom.reg (this); |
56 | iom.reg (this); |
46 | } |
57 | } |
47 | |
58 | |
48 | void time_watcher::trigger () |
|
|
49 | { |
|
|
50 | call (*this); |
|
|
51 | |
|
|
52 | if (registered) |
|
|
53 | iom.reschedule_time_watchers (); |
|
|
54 | else |
|
|
55 | iom.reg (this); |
|
|
56 | } |
|
|
57 | |
|
|
58 | void time_watcher::start () |
|
|
59 | { |
|
|
60 | if (!registered) |
|
|
61 | iom.reg (this); |
|
|
62 | } |
|
|
63 | |
|
|
64 | time_watcher::~time_watcher () |
59 | time_watcher::~time_watcher () |
65 | { |
60 | { |
66 | if (iom_valid) |
61 | if (iom_valid) |
67 | iom.unreg (this); |
62 | iom.unreg (this); |
68 | } |
63 | } |
69 | |
64 | |
|
|
65 | void io_watcher::set(int fd_, short events_) |
|
|
66 | { |
|
|
67 | fd = fd_; |
|
|
68 | events = events_; |
|
|
69 | |
|
|
70 | if (registered) |
|
|
71 | { |
|
|
72 | iom.unreg (this); |
|
|
73 | iom.reg (this); |
|
|
74 | } |
|
|
75 | } |
|
|
76 | |
70 | io_watcher::~io_watcher () |
77 | io_watcher::~io_watcher () |
71 | { |
78 | { |
72 | if (iom_valid) |
79 | if (iom_valid) |
73 | iom.unreg (this); |
80 | iom.unreg (this); |
74 | } |
81 | } |
75 | |
82 | |
76 | void io_manager::reg (io_watcher *w) |
83 | void io_manager::reg (io_watcher *w) |
77 | { |
84 | { |
|
|
85 | if (!w->registered) |
|
|
86 | { |
|
|
87 | w->registered = true; |
|
|
88 | |
78 | pollfd pfd; |
89 | pollfd pfd; |
79 | |
90 | |
|
|
91 | pfd.fd = w->fd; |
|
|
92 | pfd.events = w->events; |
|
|
93 | |
80 | pfs.push_back (pfd); |
94 | pfs.push_back (pfd); |
81 | iow.push_back (w); |
95 | iow.push_back (w); |
82 | |
96 | } |
83 | w->p = &(*(pfs.end () - 1)); |
|
|
84 | } |
97 | } |
85 | |
98 | |
86 | void io_manager::unreg (io_watcher *w) |
99 | void io_manager::unreg (io_watcher *w) |
87 | { |
100 | { |
88 | if (w->p) |
101 | if (w->registered) |
89 | { |
102 | { |
|
|
103 | w->registered = false; |
|
|
104 | |
90 | unsigned int sz = iow.size (); |
105 | unsigned int sz = iow.size (); |
91 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
106 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
92 | |
107 | |
93 | assert (i != sz); |
108 | assert (i != sz); |
94 | |
109 | |
… | |
… | |
105 | else |
120 | else |
106 | { |
121 | { |
107 | iow[i] = iow[sz - 1]; iow.pop_back (); |
122 | iow[i] = iow[sz - 1]; iow.pop_back (); |
108 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
123 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
109 | } |
124 | } |
110 | |
|
|
111 | w->p = 0; |
|
|
112 | } |
125 | } |
113 | } |
126 | } |
114 | |
127 | |
115 | void io_manager::reschedule_time_watchers () |
128 | void io_manager::reschedule_time_watchers () |
116 | { |
129 | { |
117 | make_heap (tw.begin (), tw.end (), earliest_first); |
130 | make_heap (tw.begin (), tw.end (), earliest_first); |
118 | } |
131 | } |
119 | |
132 | |
120 | void io_manager::reg (time_watcher *w) |
133 | void io_manager::reg (time_watcher *w) |
121 | { |
134 | { |
|
|
135 | if (!w->registered) |
|
|
136 | { |
122 | w->registered = true; |
137 | w->registered = true; |
123 | |
138 | |
124 | tw.push_back (w); |
139 | tw.push_back (w); |
125 | push_heap (tw.begin (), tw.end (), earliest_first); |
140 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
141 | } |
126 | } |
142 | } |
127 | |
143 | |
128 | void io_manager::unreg (time_watcher *w) |
144 | void io_manager::unreg (time_watcher *w) |
129 | { |
145 | { |
130 | if (w->registered) |
146 | if (w->registered) |
131 | { |
147 | { |
|
|
148 | w->registered = false; |
|
|
149 | |
132 | unsigned int sz = tw.size (); |
150 | unsigned int sz = tw.size (); |
133 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
151 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
134 | |
152 | |
135 | assert (i != sz); |
153 | assert (i != sz); |
136 | |
154 | |
137 | if (i != sz - 1) |
155 | if (i != sz - 1) |
138 | tw[i] = tw[sz - 1]; |
156 | tw[i] = tw[sz - 1]; |
139 | |
157 | |
140 | tw.pop_back (); |
158 | tw.pop_back (); |
141 | reschedule_time_watchers (); |
159 | reschedule_time_watchers (); |
142 | |
|
|
143 | w->registered = false; |
|
|
144 | } |
160 | } |
145 | } |
161 | } |
146 | |
162 | |
147 | inline void set_now (void) |
163 | inline void set_now (void) |
148 | { |
164 | { |
… | |
… | |
190 | fds > 0 && w < iow.end (); |
206 | fds > 0 && w < iow.end (); |
191 | ++w, ++p) |
207 | ++w, ++p) |
192 | if (p->revents) |
208 | if (p->revents) |
193 | { |
209 | { |
194 | --fds; |
210 | --fds; |
|
|
211 | |
|
|
212 | if (p->revents & POLLNVAL) |
|
|
213 | { |
|
|
214 | slog (L_ERR, _("io_watcher started on illegal file descriptor, disabling.")); |
|
|
215 | (*w)->stop (); |
|
|
216 | } |
|
|
217 | else |
195 | (*w)->call (**w, p->revents); |
218 | (*w)->call (**w, p->revents); |
196 | } |
219 | } |
197 | } |
220 | } |
198 | } |
221 | } |
199 | |
222 | |
200 | void io_manager::idle_cb (time_watcher &w) |
223 | void io_manager::idle_cb (time_watcher &w) |