1 | #include <unistd.h> |
|
|
2 | /* |
1 | /* |
3 | iom.C -- I/O multiplexor |
2 | iom.C -- I/O multiplexor |
4 | |
3 | |
5 | This program is free software; you can redistribute it and/or modify |
4 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
5 | it under the terms of the GNU General Public License as published by |
… | |
… | |
23 | |
22 | |
24 | #include <algorithm> |
23 | #include <algorithm> |
25 | #include <functional> |
24 | #include <functional> |
26 | |
25 | |
27 | #include "slog.h" |
26 | #include "slog.h" |
28 | |
|
|
29 | #include "iom.h" |
27 | #include "iom.h" |
30 | |
28 | |
31 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
29 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
32 | { |
30 | { |
33 | return a->at > b->at; |
31 | return a->at > b->at; |
… | |
… | |
42 | iom.unreg (this); |
40 | iom.unreg (this); |
43 | at = when; |
41 | at = when; |
44 | iom.reg (this); |
42 | iom.reg (this); |
45 | } |
43 | } |
46 | |
44 | |
|
|
45 | void time_watcher::trigger () |
|
|
46 | { |
|
|
47 | iom.unreg (this); |
|
|
48 | call (at); |
|
|
49 | iom.reg (this); |
|
|
50 | } |
|
|
51 | |
|
|
52 | void time_watcher::start () |
|
|
53 | { |
|
|
54 | iom.unreg (this); |
|
|
55 | iom.reg (this); |
|
|
56 | } |
|
|
57 | |
47 | void io_manager::reg (int fd, short events, io_watcher *w) |
58 | void io_manager::reg (int fd, short events, io_watcher *w) |
48 | { |
59 | { |
49 | pollfd pfd; |
60 | pollfd pfd; |
50 | |
61 | |
51 | pfd.fd = fd; |
62 | pfd.fd = fd; |
… | |
… | |
53 | |
64 | |
54 | pfs.push_back (pfd); |
65 | pfs.push_back (pfd); |
55 | iow.push_back (w); |
66 | iow.push_back (w); |
56 | } |
67 | } |
57 | |
68 | |
58 | void io_manager::unreg (io_watcher *w) |
69 | void io_manager::unreg (const io_watcher *w) |
59 | { |
70 | { |
60 | unsigned int sz = iow.size (); |
71 | unsigned int sz = iow.size (); |
61 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
72 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
62 | |
73 | |
63 | if (i != sz) |
74 | if (i != sz) |
… | |
… | |
84 | { |
95 | { |
85 | tw.push_back (w); |
96 | tw.push_back (w); |
86 | push_heap (tw.begin (), tw.end (), lowest_first); |
97 | push_heap (tw.begin (), tw.end (), lowest_first); |
87 | } |
98 | } |
88 | |
99 | |
89 | void io_manager::unreg (time_watcher *w) |
100 | void io_manager::unreg (const time_watcher *w) |
90 | { |
101 | { |
91 | unsigned int sz = tw.size (); |
102 | unsigned int sz = tw.size (); |
92 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
103 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
93 | |
104 | |
94 | if (i != sz) |
105 | if (i != sz) |
95 | { |
106 | { |
96 | if (sz == 1) |
|
|
97 | tw.clear (); |
|
|
98 | else |
|
|
99 | { |
|
|
100 | if (i != sz - 1) |
107 | if (i != sz - 1) |
101 | tw[i] = tw[sz - 1]; |
108 | tw[i] = tw[sz - 1]; |
102 | |
109 | |
103 | tw.pop_back (); |
110 | tw.pop_back (); |
104 | make_heap (tw.begin (), tw.end (), lowest_first); |
111 | make_heap (tw.begin (), tw.end (), lowest_first); |
105 | } |
|
|
106 | } |
112 | } |
107 | } |
113 | } |
108 | |
114 | |
109 | inline void set_now (void) |
115 | inline void set_now (void) |
110 | { |
116 | { |
… | |
… | |
117 | |
123 | |
118 | void io_manager::loop () |
124 | void io_manager::loop () |
119 | { |
125 | { |
120 | set_now (); |
126 | set_now (); |
121 | |
127 | |
122 | while (!(iow.empty () && tw.empty ())) |
128 | for (;;) |
123 | { |
129 | { |
124 | int timeout = tw.empty () |
130 | while (tw[0]->at <= NOW) |
125 | ? 3600 * 1000 // wake up at least every hour |
131 | { |
126 | : (int) ((tw[0]->at - NOW) * 1000); |
132 | pop_heap (tw.begin (), tw.end (), lowest_first); |
|
|
133 | time_watcher *w = *(tw.end () - 1); |
127 | |
134 | |
128 | printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ()); |
135 | if (w->at >= 0) |
129 | |
|
|
130 | if (timeout >= 0) |
|
|
131 | { |
|
|
132 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
133 | |
|
|
134 | set_now (); |
|
|
135 | |
|
|
136 | for (unsigned int i = iow.size (); fds && i--; ) |
|
|
137 | if (pfs[i].revents) |
|
|
138 | { |
136 | { |
139 | --fds; |
137 | w->call (w->at); |
140 | iow[i]->call (pfs[i].revents); |
138 | push_heap (tw.begin (), tw.end (), lowest_first); |
141 | } |
139 | } |
|
|
140 | else |
|
|
141 | tw.pop_back (); |
142 | } |
142 | } |
143 | |
143 | |
144 | while (!tw.empty () && tw[0]->at <= NOW) |
144 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
145 | |
|
|
146 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
147 | |
|
|
148 | set_now (); |
|
|
149 | |
|
|
150 | for (unsigned int i = iow.size (); fds > 0 && i--; ) |
|
|
151 | if (pfs[i].revents) |
145 | { |
152 | { |
146 | pop_heap (tw.begin (), tw.end (), lowest_first); |
153 | --fds; |
147 | (*(tw.end () - 1))->trigger (); |
154 | iow[i]->call (pfs[i].revents); |
148 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
149 | } |
155 | } |
150 | } |
156 | } |
|
|
157 | } |
|
|
158 | |
|
|
159 | void io_manager::idle_cb (tstamp &ts) |
|
|
160 | { |
|
|
161 | ts = NOW + 86400; // wake up every day, for no good reason |
151 | } |
162 | } |
152 | |
163 | |
153 | io_manager::io_manager () |
164 | io_manager::io_manager () |
154 | { |
165 | { |
155 | set_now (); |
166 | set_now (); |
|
|
167 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
168 | idle->start (0); |
156 | } |
169 | } |
157 | |
170 | |
158 | io_manager::~io_manager () |
171 | io_manager::~io_manager () |
159 | { |
172 | { |
160 | // |
173 | // |