1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
30 | */ |
29 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
30 | # include "config.h" |
32 | # include "config.h" |
31 | #endif |
33 | #endif |
32 | |
34 | |
33 | #include <math.h> |
35 | #include <math.h> |
34 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
40 | #include <stdio.h> |
42 | #include <stdio.h> |
41 | |
43 | |
42 | #include <assert.h> |
44 | #include <assert.h> |
43 | #include <errno.h> |
45 | #include <errno.h> |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
45 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
46 | #include <sys/time.h> |
50 | #include <sys/time.h> |
47 | #include <time.h> |
51 | #include <time.h> |
48 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
49 | #ifndef EV_USE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
56 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
57 | #endif |
54 | |
58 | |
55 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
57 | #endif |
61 | #endif |
58 | |
62 | |
|
|
63 | #ifndef EV_USEV_POLL |
|
|
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
65 | #endif |
|
|
66 | |
59 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
61 | #endif |
69 | #endif |
62 | |
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #ifndef EV_USE_REALTIME |
|
|
76 | # define EV_USE_REALTIME 1 |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | /**/ |
|
|
80 | |
|
|
81 | #ifndef CLOCK_MONOTONIC |
|
|
82 | # undef EV_USE_MONOTONIC |
|
|
83 | # define EV_USE_MONOTONIC 0 |
|
|
84 | #endif |
|
|
85 | |
63 | #ifndef CLOCK_REALTIME |
86 | #ifndef CLOCK_REALTIME |
|
|
87 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
88 | # define EV_USE_REALTIME 0 |
65 | #endif |
89 | #endif |
66 | #ifndef EV_USE_REALTIME |
90 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
91 | /**/ |
68 | #endif |
|
|
69 | |
92 | |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
74 | |
97 | |
75 | #include "ev.h" |
98 | #include "ev.h" |
|
|
99 | |
|
|
100 | #if __GNUC__ >= 3 |
|
|
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
102 | # define inline inline |
|
|
103 | #else |
|
|
104 | # define expect(expr,value) (expr) |
|
|
105 | # define inline static |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
76 | |
113 | |
77 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
80 | |
117 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
82 | ev_tstamp ev_now; |
|
|
83 | int ev_method; |
|
|
84 | |
|
|
85 | static int have_monotonic; /* runtime */ |
|
|
86 | |
|
|
87 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
|
|
88 | static void (*method_modify)(int fd, int oev, int nev); |
|
|
89 | static void (*method_poll)(ev_tstamp timeout); |
|
|
90 | |
119 | |
91 | /*****************************************************************************/ |
120 | /*****************************************************************************/ |
92 | |
121 | |
93 | ev_tstamp |
122 | typedef struct |
|
|
123 | { |
|
|
124 | struct ev_watcher_list *head; |
|
|
125 | unsigned char events; |
|
|
126 | unsigned char reify; |
|
|
127 | } ANFD; |
|
|
128 | |
|
|
129 | typedef struct |
|
|
130 | { |
|
|
131 | W w; |
|
|
132 | int events; |
|
|
133 | } ANPENDING; |
|
|
134 | |
|
|
135 | #if EV_MULTIPLICITY |
|
|
136 | |
|
|
137 | struct ev_loop |
|
|
138 | { |
|
|
139 | # define VAR(name,decl) decl; |
|
|
140 | # include "ev_vars.h" |
|
|
141 | }; |
|
|
142 | # undef VAR |
|
|
143 | # include "ev_wrap.h" |
|
|
144 | |
|
|
145 | #else |
|
|
146 | |
|
|
147 | # define VAR(name,decl) static decl; |
|
|
148 | # include "ev_vars.h" |
|
|
149 | # undef VAR |
|
|
150 | |
|
|
151 | #endif |
|
|
152 | |
|
|
153 | /*****************************************************************************/ |
|
|
154 | |
|
|
155 | inline ev_tstamp |
94 | ev_time (void) |
156 | ev_time (void) |
95 | { |
157 | { |
96 | #if EV_USE_REALTIME |
158 | #if EV_USE_REALTIME |
97 | struct timespec ts; |
159 | struct timespec ts; |
98 | clock_gettime (CLOCK_REALTIME, &ts); |
160 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
102 | gettimeofday (&tv, 0); |
164 | gettimeofday (&tv, 0); |
103 | return tv.tv_sec + tv.tv_usec * 1e-6; |
165 | return tv.tv_sec + tv.tv_usec * 1e-6; |
104 | #endif |
166 | #endif |
105 | } |
167 | } |
106 | |
168 | |
107 | static ev_tstamp |
169 | inline ev_tstamp |
108 | get_clock (void) |
170 | get_clock (void) |
109 | { |
171 | { |
110 | #if EV_USE_MONOTONIC |
172 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
173 | if (expect_true (have_monotonic)) |
112 | { |
174 | { |
113 | struct timespec ts; |
175 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
176 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
177 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
178 | } |
117 | #endif |
179 | #endif |
118 | |
180 | |
119 | return ev_time (); |
181 | return ev_time (); |
120 | } |
182 | } |
121 | |
183 | |
|
|
184 | ev_tstamp |
|
|
185 | ev_now (EV_P) |
|
|
186 | { |
|
|
187 | return rt_now; |
|
|
188 | } |
|
|
189 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
190 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
191 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
192 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
193 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
194 | { \ |
127 | int newcnt = cur; \ |
195 | int newcnt = cur; \ |
128 | do \ |
196 | do \ |
129 | { \ |
197 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
198 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
136 | cur = newcnt; \ |
204 | cur = newcnt; \ |
137 | } |
205 | } |
138 | |
206 | |
139 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
140 | |
208 | |
141 | typedef struct |
|
|
142 | { |
|
|
143 | struct ev_io *head; |
|
|
144 | unsigned char events; |
|
|
145 | unsigned char reify; |
|
|
146 | } ANFD; |
|
|
147 | |
|
|
148 | static ANFD *anfds; |
|
|
149 | static int anfdmax; |
|
|
150 | |
|
|
151 | static void |
209 | static void |
152 | anfds_init (ANFD *base, int count) |
210 | anfds_init (ANFD *base, int count) |
153 | { |
211 | { |
154 | while (count--) |
212 | while (count--) |
155 | { |
213 | { |
… | |
… | |
159 | |
217 | |
160 | ++base; |
218 | ++base; |
161 | } |
219 | } |
162 | } |
220 | } |
163 | |
221 | |
164 | typedef struct |
|
|
165 | { |
|
|
166 | W w; |
|
|
167 | int events; |
|
|
168 | } ANPENDING; |
|
|
169 | |
|
|
170 | static ANPENDING *pendings; |
|
|
171 | static int pendingmax, pendingcnt; |
|
|
172 | |
|
|
173 | static void |
222 | static void |
174 | event (W w, int events) |
223 | event (EV_P_ W w, int events) |
175 | { |
224 | { |
176 | if (w->pending) |
225 | if (w->pending) |
177 | { |
226 | { |
178 | pendings [w->pending - 1].events |= events; |
227 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
179 | return; |
228 | return; |
180 | } |
229 | } |
181 | |
230 | |
182 | w->pending = ++pendingcnt; |
231 | w->pending = ++pendingcnt [ABSPRI (w)]; |
183 | array_needsize (pendings, pendingmax, pendingcnt, ); |
232 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
184 | pendings [pendingcnt - 1].w = w; |
233 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
185 | pendings [pendingcnt - 1].events = events; |
234 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
186 | } |
235 | } |
187 | |
236 | |
188 | static void |
237 | static void |
189 | queue_events (W *events, int eventcnt, int type) |
238 | queue_events (EV_P_ W *events, int eventcnt, int type) |
190 | { |
239 | { |
191 | int i; |
240 | int i; |
192 | |
241 | |
193 | for (i = 0; i < eventcnt; ++i) |
242 | for (i = 0; i < eventcnt; ++i) |
194 | event (events [i], type); |
243 | event (EV_A_ events [i], type); |
195 | } |
244 | } |
196 | |
245 | |
197 | static void |
246 | static void |
198 | fd_event (int fd, int events) |
247 | fd_event (EV_P_ int fd, int events) |
199 | { |
248 | { |
200 | ANFD *anfd = anfds + fd; |
249 | ANFD *anfd = anfds + fd; |
201 | struct ev_io *w; |
250 | struct ev_io *w; |
202 | |
251 | |
203 | for (w = anfd->head; w; w = w->next) |
252 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
204 | { |
253 | { |
205 | int ev = w->events & events; |
254 | int ev = w->events & events; |
206 | |
255 | |
207 | if (ev) |
256 | if (ev) |
208 | event ((W)w, ev); |
257 | event (EV_A_ (W)w, ev); |
209 | } |
258 | } |
210 | } |
259 | } |
211 | |
260 | |
212 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
213 | |
262 | |
214 | static int *fdchanges; |
|
|
215 | static int fdchangemax, fdchangecnt; |
|
|
216 | |
|
|
217 | static void |
263 | static void |
218 | fd_reify (void) |
264 | fd_reify (EV_P) |
219 | { |
265 | { |
220 | int i; |
266 | int i; |
221 | |
267 | |
222 | for (i = 0; i < fdchangecnt; ++i) |
268 | for (i = 0; i < fdchangecnt; ++i) |
223 | { |
269 | { |
… | |
… | |
225 | ANFD *anfd = anfds + fd; |
271 | ANFD *anfd = anfds + fd; |
226 | struct ev_io *w; |
272 | struct ev_io *w; |
227 | |
273 | |
228 | int events = 0; |
274 | int events = 0; |
229 | |
275 | |
230 | for (w = anfd->head; w; w = w->next) |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
231 | events |= w->events; |
277 | events |= w->events; |
232 | |
278 | |
233 | anfd->reify = 0; |
279 | anfd->reify = 0; |
234 | |
280 | |
235 | if (anfd->events != events) |
281 | if (anfd->events != events) |
236 | { |
282 | { |
237 | method_modify (fd, anfd->events, events); |
283 | method_modify (EV_A_ fd, anfd->events, events); |
238 | anfd->events = events; |
284 | anfd->events = events; |
239 | } |
285 | } |
240 | } |
286 | } |
241 | |
287 | |
242 | fdchangecnt = 0; |
288 | fdchangecnt = 0; |
243 | } |
289 | } |
244 | |
290 | |
245 | static void |
291 | static void |
246 | fd_change (int fd) |
292 | fd_change (EV_P_ int fd) |
247 | { |
293 | { |
248 | if (anfds [fd].reify || fdchangecnt < 0) |
294 | if (anfds [fd].reify || fdchangecnt < 0) |
249 | return; |
295 | return; |
250 | |
296 | |
251 | anfds [fd].reify = 1; |
297 | anfds [fd].reify = 1; |
… | |
… | |
253 | ++fdchangecnt; |
299 | ++fdchangecnt; |
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
300 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
255 | fdchanges [fdchangecnt - 1] = fd; |
301 | fdchanges [fdchangecnt - 1] = fd; |
256 | } |
302 | } |
257 | |
303 | |
|
|
304 | static void |
|
|
305 | fd_kill (EV_P_ int fd) |
|
|
306 | { |
|
|
307 | struct ev_io *w; |
|
|
308 | |
|
|
309 | while ((w = (struct ev_io *)anfds [fd].head)) |
|
|
310 | { |
|
|
311 | ev_io_stop (EV_A_ w); |
|
|
312 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
313 | } |
|
|
314 | } |
|
|
315 | |
258 | /* called on EBADF to verify fds */ |
316 | /* called on EBADF to verify fds */ |
259 | static void |
317 | static void |
260 | fd_recheck (void) |
318 | fd_ebadf (EV_P) |
261 | { |
319 | { |
262 | int fd; |
320 | int fd; |
263 | |
321 | |
264 | for (fd = 0; fd < anfdmax; ++fd) |
322 | for (fd = 0; fd < anfdmax; ++fd) |
265 | if (anfds [fd].events) |
323 | if (anfds [fd].events) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
324 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
267 | while (anfds [fd].head) |
325 | fd_kill (EV_A_ fd); |
|
|
326 | } |
|
|
327 | |
|
|
328 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
329 | static void |
|
|
330 | fd_enomem (EV_P) |
|
|
331 | { |
|
|
332 | int fd = anfdmax; |
|
|
333 | |
|
|
334 | while (fd--) |
|
|
335 | if (anfds [fd].events) |
268 | { |
336 | { |
269 | ev_io_stop (anfds [fd].head); |
337 | close (fd); |
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
338 | fd_kill (EV_A_ fd); |
|
|
339 | return; |
271 | } |
340 | } |
|
|
341 | } |
|
|
342 | |
|
|
343 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
344 | static void |
|
|
345 | fd_rearm_all (EV_P) |
|
|
346 | { |
|
|
347 | int fd; |
|
|
348 | |
|
|
349 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
350 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
351 | if (anfds [fd].events) |
|
|
352 | { |
|
|
353 | anfds [fd].events = 0; |
|
|
354 | fd_change (fd); |
|
|
355 | } |
272 | } |
356 | } |
273 | |
357 | |
274 | /*****************************************************************************/ |
358 | /*****************************************************************************/ |
275 | |
359 | |
276 | static struct ev_timer **timers; |
|
|
277 | static int timermax, timercnt; |
|
|
278 | |
|
|
279 | static struct ev_periodic **periodics; |
|
|
280 | static int periodicmax, periodiccnt; |
|
|
281 | |
|
|
282 | static void |
360 | static void |
283 | upheap (WT *timers, int k) |
361 | upheap (WT *heap, int k) |
284 | { |
362 | { |
285 | WT w = timers [k]; |
363 | WT w = heap [k]; |
286 | |
364 | |
287 | while (k && timers [k >> 1]->at > w->at) |
365 | while (k && heap [k >> 1]->at > w->at) |
288 | { |
366 | { |
289 | timers [k] = timers [k >> 1]; |
367 | heap [k] = heap [k >> 1]; |
290 | timers [k]->active = k + 1; |
368 | heap [k]->active = k + 1; |
291 | k >>= 1; |
369 | k >>= 1; |
292 | } |
370 | } |
293 | |
371 | |
294 | timers [k] = w; |
372 | heap [k] = w; |
295 | timers [k]->active = k + 1; |
373 | heap [k]->active = k + 1; |
296 | |
374 | |
297 | } |
375 | } |
298 | |
376 | |
299 | static void |
377 | static void |
300 | downheap (WT *timers, int N, int k) |
378 | downheap (WT *heap, int N, int k) |
301 | { |
379 | { |
302 | WT w = timers [k]; |
380 | WT w = heap [k]; |
303 | |
381 | |
304 | while (k < (N >> 1)) |
382 | while (k < (N >> 1)) |
305 | { |
383 | { |
306 | int j = k << 1; |
384 | int j = k << 1; |
307 | |
385 | |
308 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
386 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
309 | ++j; |
387 | ++j; |
310 | |
388 | |
311 | if (w->at <= timers [j]->at) |
389 | if (w->at <= heap [j]->at) |
312 | break; |
390 | break; |
313 | |
391 | |
314 | timers [k] = timers [j]; |
392 | heap [k] = heap [j]; |
315 | timers [k]->active = k + 1; |
393 | heap [k]->active = k + 1; |
316 | k = j; |
394 | k = j; |
317 | } |
395 | } |
318 | |
396 | |
319 | timers [k] = w; |
397 | heap [k] = w; |
320 | timers [k]->active = k + 1; |
398 | heap [k]->active = k + 1; |
321 | } |
399 | } |
322 | |
400 | |
323 | /*****************************************************************************/ |
401 | /*****************************************************************************/ |
324 | |
402 | |
325 | typedef struct |
403 | typedef struct |
326 | { |
404 | { |
327 | struct ev_signal *head; |
405 | struct ev_watcher_list *head; |
328 | sig_atomic_t volatile gotsig; |
406 | sig_atomic_t volatile gotsig; |
329 | } ANSIG; |
407 | } ANSIG; |
330 | |
408 | |
331 | static ANSIG *signals; |
409 | static ANSIG *signals; |
332 | static int signalmax; |
410 | static int signalmax; |
333 | |
411 | |
334 | static int sigpipe [2]; |
412 | static int sigpipe [2]; |
335 | static sig_atomic_t volatile gotsig; |
413 | static sig_atomic_t volatile gotsig; |
336 | static struct ev_io sigev; |
|
|
337 | |
414 | |
338 | static void |
415 | static void |
339 | signals_init (ANSIG *base, int count) |
416 | signals_init (ANSIG *base, int count) |
340 | { |
417 | { |
341 | while (count--) |
418 | while (count--) |
… | |
… | |
352 | { |
429 | { |
353 | signals [signum - 1].gotsig = 1; |
430 | signals [signum - 1].gotsig = 1; |
354 | |
431 | |
355 | if (!gotsig) |
432 | if (!gotsig) |
356 | { |
433 | { |
|
|
434 | int old_errno = errno; |
357 | gotsig = 1; |
435 | gotsig = 1; |
358 | write (sigpipe [1], &signum, 1); |
436 | write (sigpipe [1], &signum, 1); |
|
|
437 | errno = old_errno; |
359 | } |
438 | } |
360 | } |
439 | } |
361 | |
440 | |
362 | static void |
441 | static void |
363 | sigcb (struct ev_io *iow, int revents) |
442 | sigcb (EV_P_ struct ev_io *iow, int revents) |
364 | { |
443 | { |
365 | struct ev_signal *w; |
444 | struct ev_watcher_list *w; |
366 | int sig; |
445 | int signum; |
367 | |
446 | |
368 | read (sigpipe [0], &revents, 1); |
447 | read (sigpipe [0], &revents, 1); |
369 | gotsig = 0; |
448 | gotsig = 0; |
370 | |
449 | |
371 | for (sig = signalmax; sig--; ) |
450 | for (signum = signalmax; signum--; ) |
372 | if (signals [sig].gotsig) |
451 | if (signals [signum].gotsig) |
373 | { |
452 | { |
374 | signals [sig].gotsig = 0; |
453 | signals [signum].gotsig = 0; |
375 | |
454 | |
376 | for (w = signals [sig].head; w; w = w->next) |
455 | for (w = signals [signum].head; w; w = w->next) |
377 | event ((W)w, EV_SIGNAL); |
456 | event (EV_A_ (W)w, EV_SIGNAL); |
378 | } |
457 | } |
379 | } |
458 | } |
380 | |
459 | |
381 | static void |
460 | static void |
382 | siginit (void) |
461 | siginit (EV_P) |
383 | { |
462 | { |
|
|
463 | #ifndef WIN32 |
384 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
464 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
385 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
465 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
386 | |
466 | |
387 | /* rather than sort out wether we really need nb, set it */ |
467 | /* rather than sort out wether we really need nb, set it */ |
388 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
468 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
389 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
469 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
470 | #endif |
390 | |
471 | |
391 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
472 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
392 | ev_io_start (&sigev); |
473 | ev_io_start (EV_A_ &sigev); |
|
|
474 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
393 | } |
475 | } |
394 | |
476 | |
395 | /*****************************************************************************/ |
477 | /*****************************************************************************/ |
396 | |
478 | |
397 | static struct ev_idle **idles; |
479 | #ifndef WIN32 |
398 | static int idlemax, idlecnt; |
|
|
399 | |
|
|
400 | static struct ev_prepare **prepares; |
|
|
401 | static int preparemax, preparecnt; |
|
|
402 | |
|
|
403 | static struct ev_check **checks; |
|
|
404 | static int checkmax, checkcnt; |
|
|
405 | |
|
|
406 | /*****************************************************************************/ |
|
|
407 | |
|
|
408 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
409 | static struct ev_signal childev; |
|
|
410 | |
480 | |
411 | #ifndef WCONTINUED |
481 | #ifndef WCONTINUED |
412 | # define WCONTINUED 0 |
482 | # define WCONTINUED 0 |
413 | #endif |
483 | #endif |
414 | |
484 | |
415 | static void |
485 | static void |
416 | childcb (struct ev_signal *sw, int revents) |
486 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
417 | { |
487 | { |
418 | struct ev_child *w; |
488 | struct ev_child *w; |
|
|
489 | |
|
|
490 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
|
|
491 | if (w->pid == pid || !w->pid) |
|
|
492 | { |
|
|
493 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
494 | w->rpid = pid; |
|
|
495 | w->rstatus = status; |
|
|
496 | event (EV_A_ (W)w, EV_CHILD); |
|
|
497 | } |
|
|
498 | } |
|
|
499 | |
|
|
500 | static void |
|
|
501 | childcb (EV_P_ struct ev_signal *sw, int revents) |
|
|
502 | { |
419 | int pid, status; |
503 | int pid, status; |
420 | |
504 | |
421 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
505 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
422 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
506 | { |
423 | if (w->pid == pid || w->pid == -1) |
507 | /* make sure we are called again until all childs have been reaped */ |
424 | { |
508 | event (EV_A_ (W)sw, EV_SIGNAL); |
425 | w->status = status; |
509 | |
426 | event ((W)w, EV_CHILD); |
510 | child_reap (EV_A_ sw, pid, pid, status); |
427 | } |
511 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
512 | } |
428 | } |
513 | } |
|
|
514 | |
|
|
515 | #endif |
429 | |
516 | |
430 | /*****************************************************************************/ |
517 | /*****************************************************************************/ |
431 | |
518 | |
|
|
519 | #if EV_USE_KQUEUE |
|
|
520 | # include "ev_kqueue.c" |
|
|
521 | #endif |
432 | #if EV_USE_EPOLL |
522 | #if EV_USE_EPOLL |
433 | # include "ev_epoll.c" |
523 | # include "ev_epoll.c" |
434 | #endif |
524 | #endif |
|
|
525 | #if EV_USEV_POLL |
|
|
526 | # include "ev_poll.c" |
|
|
527 | #endif |
435 | #if EV_USE_SELECT |
528 | #if EV_USE_SELECT |
436 | # include "ev_select.c" |
529 | # include "ev_select.c" |
437 | #endif |
530 | #endif |
438 | |
531 | |
439 | int |
532 | int |
… | |
… | |
446 | ev_version_minor (void) |
539 | ev_version_minor (void) |
447 | { |
540 | { |
448 | return EV_VERSION_MINOR; |
541 | return EV_VERSION_MINOR; |
449 | } |
542 | } |
450 | |
543 | |
451 | int ev_init (int flags) |
544 | /* return true if we are running with elevated privileges and should ignore env variables */ |
|
|
545 | static int |
|
|
546 | enable_secure (void) |
452 | { |
547 | { |
|
|
548 | #ifdef WIN32 |
|
|
549 | return 0; |
|
|
550 | #else |
|
|
551 | return getuid () != geteuid () |
|
|
552 | || getgid () != getegid (); |
|
|
553 | #endif |
|
|
554 | } |
|
|
555 | |
|
|
556 | int |
|
|
557 | ev_method (EV_P) |
|
|
558 | { |
|
|
559 | return method; |
|
|
560 | } |
|
|
561 | |
|
|
562 | static void |
|
|
563 | loop_init (EV_P_ int methods) |
|
|
564 | { |
453 | if (!ev_method) |
565 | if (!method) |
454 | { |
566 | { |
455 | #if EV_USE_MONOTONIC |
567 | #if EV_USE_MONOTONIC |
456 | { |
568 | { |
457 | struct timespec ts; |
569 | struct timespec ts; |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
570 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
459 | have_monotonic = 1; |
571 | have_monotonic = 1; |
460 | } |
572 | } |
461 | #endif |
573 | #endif |
462 | |
574 | |
463 | ev_now = ev_time (); |
575 | rt_now = ev_time (); |
464 | now = get_clock (); |
576 | mn_now = get_clock (); |
|
|
577 | now_floor = mn_now; |
465 | diff = ev_now - now; |
578 | rtmn_diff = rt_now - mn_now; |
466 | |
579 | |
467 | if (pipe (sigpipe)) |
580 | if (methods == EVMETHOD_AUTO) |
468 | return 0; |
581 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
469 | |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
583 | else |
470 | ev_method = EVMETHOD_NONE; |
584 | methods = EVMETHOD_ANY; |
|
|
585 | |
|
|
586 | method = 0; |
|
|
587 | #if EV_USE_KQUEUE |
|
|
588 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
|
|
589 | #endif |
471 | #if EV_USE_EPOLL |
590 | #if EV_USE_EPOLL |
472 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
591 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
|
|
592 | #endif |
|
|
593 | #if EV_USEV_POLL |
|
|
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
473 | #endif |
595 | #endif |
474 | #if EV_USE_SELECT |
596 | #if EV_USE_SELECT |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
476 | #endif |
598 | #endif |
|
|
599 | } |
|
|
600 | } |
477 | |
601 | |
|
|
602 | void |
|
|
603 | loop_destroy (EV_P) |
|
|
604 | { |
|
|
605 | #if EV_USE_KQUEUE |
|
|
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
607 | #endif |
|
|
608 | #if EV_USE_EPOLL |
|
|
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
610 | #endif |
|
|
611 | #if EV_USEV_POLL |
|
|
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
613 | #endif |
|
|
614 | #if EV_USE_SELECT |
|
|
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
616 | #endif |
|
|
617 | |
|
|
618 | method = 0; |
|
|
619 | /*TODO*/ |
|
|
620 | } |
|
|
621 | |
|
|
622 | void |
|
|
623 | loop_fork (EV_P) |
|
|
624 | { |
|
|
625 | /*TODO*/ |
|
|
626 | #if EV_USE_EPOLL |
|
|
627 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
628 | #endif |
|
|
629 | #if EV_USE_KQUEUE |
|
|
630 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
631 | #endif |
|
|
632 | } |
|
|
633 | |
|
|
634 | #if EV_MULTIPLICITY |
|
|
635 | struct ev_loop * |
|
|
636 | ev_loop_new (int methods) |
|
|
637 | { |
|
|
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
639 | |
|
|
640 | loop_init (EV_A_ methods); |
|
|
641 | |
|
|
642 | if (ev_methods (EV_A)) |
|
|
643 | return loop; |
|
|
644 | |
|
|
645 | return 0; |
|
|
646 | } |
|
|
647 | |
|
|
648 | void |
|
|
649 | ev_loop_destroy (EV_P) |
|
|
650 | { |
|
|
651 | loop_destroy (EV_A); |
|
|
652 | free (loop); |
|
|
653 | } |
|
|
654 | |
|
|
655 | void |
|
|
656 | ev_loop_fork (EV_P) |
|
|
657 | { |
|
|
658 | loop_fork (EV_A); |
|
|
659 | } |
|
|
660 | |
|
|
661 | #endif |
|
|
662 | |
|
|
663 | #if EV_MULTIPLICITY |
|
|
664 | struct ev_loop default_loop_struct; |
|
|
665 | static struct ev_loop *default_loop; |
|
|
666 | |
|
|
667 | struct ev_loop * |
|
|
668 | #else |
|
|
669 | static int default_loop; |
|
|
670 | |
|
|
671 | int |
|
|
672 | #endif |
|
|
673 | ev_default_loop (int methods) |
|
|
674 | { |
|
|
675 | if (sigpipe [0] == sigpipe [1]) |
|
|
676 | if (pipe (sigpipe)) |
|
|
677 | return 0; |
|
|
678 | |
|
|
679 | if (!default_loop) |
|
|
680 | { |
|
|
681 | #if EV_MULTIPLICITY |
|
|
682 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
683 | #else |
|
|
684 | default_loop = 1; |
|
|
685 | #endif |
|
|
686 | |
|
|
687 | loop_init (EV_A_ methods); |
|
|
688 | |
478 | if (ev_method) |
689 | if (ev_method (EV_A)) |
479 | { |
690 | { |
480 | ev_watcher_init (&sigev, sigcb); |
691 | ev_watcher_init (&sigev, sigcb); |
|
|
692 | ev_set_priority (&sigev, EV_MAXPRI); |
481 | siginit (); |
693 | siginit (EV_A); |
482 | |
694 | |
|
|
695 | #ifndef WIN32 |
483 | ev_signal_init (&childev, childcb, SIGCHLD); |
696 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
697 | ev_set_priority (&childev, EV_MAXPRI); |
484 | ev_signal_start (&childev); |
698 | ev_signal_start (EV_A_ &childev); |
|
|
699 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
|
|
700 | #endif |
485 | } |
701 | } |
|
|
702 | else |
|
|
703 | default_loop = 0; |
486 | } |
704 | } |
487 | |
705 | |
488 | return ev_method; |
706 | return default_loop; |
489 | } |
707 | } |
490 | |
708 | |
491 | /*****************************************************************************/ |
|
|
492 | |
|
|
493 | void |
709 | void |
494 | ev_fork_prepare (void) |
710 | ev_default_destroy (void) |
495 | { |
711 | { |
496 | /* nop */ |
712 | struct ev_loop *loop = default_loop; |
497 | } |
|
|
498 | |
713 | |
499 | void |
714 | ev_ref (EV_A); /* child watcher */ |
500 | ev_fork_parent (void) |
715 | ev_signal_stop (EV_A_ &childev); |
501 | { |
|
|
502 | /* nop */ |
|
|
503 | } |
|
|
504 | |
716 | |
505 | void |
717 | ev_ref (EV_A); /* signal watcher */ |
506 | ev_fork_child (void) |
|
|
507 | { |
|
|
508 | #if EV_USE_EPOLL |
|
|
509 | if (ev_method == EVMETHOD_EPOLL) |
|
|
510 | epoll_postfork_child (); |
|
|
511 | #endif |
|
|
512 | |
|
|
513 | ev_io_stop (&sigev); |
718 | ev_io_stop (EV_A_ &sigev); |
|
|
719 | |
|
|
720 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
721 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
722 | |
|
|
723 | loop_destroy (EV_A); |
|
|
724 | } |
|
|
725 | |
|
|
726 | void |
|
|
727 | ev_default_fork (EV_P) |
|
|
728 | { |
|
|
729 | loop_fork (EV_A); |
|
|
730 | |
|
|
731 | ev_io_stop (EV_A_ &sigev); |
514 | close (sigpipe [0]); |
732 | close (sigpipe [0]); |
515 | close (sigpipe [1]); |
733 | close (sigpipe [1]); |
516 | pipe (sigpipe); |
734 | pipe (sigpipe); |
|
|
735 | |
|
|
736 | ev_ref (EV_A); /* signal watcher */ |
517 | siginit (); |
737 | siginit (EV_A); |
518 | } |
738 | } |
519 | |
739 | |
520 | /*****************************************************************************/ |
740 | /*****************************************************************************/ |
521 | |
741 | |
522 | static void |
742 | static void |
523 | call_pending (void) |
743 | call_pending (EV_P) |
524 | { |
744 | { |
|
|
745 | int pri; |
|
|
746 | |
|
|
747 | for (pri = NUMPRI; pri--; ) |
525 | while (pendingcnt) |
748 | while (pendingcnt [pri]) |
526 | { |
749 | { |
527 | ANPENDING *p = pendings + --pendingcnt; |
750 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
528 | |
751 | |
529 | if (p->w) |
752 | if (p->w) |
530 | { |
753 | { |
531 | p->w->pending = 0; |
754 | p->w->pending = 0; |
532 | p->w->cb (p->w, p->events); |
755 | p->w->cb (EV_A_ p->w, p->events); |
533 | } |
756 | } |
534 | } |
757 | } |
535 | } |
758 | } |
536 | |
759 | |
537 | static void |
760 | static void |
538 | timers_reify (void) |
761 | timers_reify (EV_P) |
539 | { |
762 | { |
540 | while (timercnt && timers [0]->at <= now) |
763 | while (timercnt && timers [0]->at <= mn_now) |
541 | { |
764 | { |
542 | struct ev_timer *w = timers [0]; |
765 | struct ev_timer *w = timers [0]; |
543 | |
766 | |
544 | /* first reschedule or stop timer */ |
767 | /* first reschedule or stop timer */ |
545 | if (w->repeat) |
768 | if (w->repeat) |
546 | { |
769 | { |
547 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
770 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
548 | w->at = now + w->repeat; |
771 | w->at = mn_now + w->repeat; |
549 | downheap ((WT *)timers, timercnt, 0); |
772 | downheap ((WT *)timers, timercnt, 0); |
550 | } |
773 | } |
551 | else |
774 | else |
552 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
775 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
553 | |
776 | |
554 | event ((W)w, EV_TIMEOUT); |
777 | event (EV_A_ (W)w, EV_TIMEOUT); |
555 | } |
778 | } |
556 | } |
779 | } |
557 | |
780 | |
558 | static void |
781 | static void |
559 | periodics_reify (void) |
782 | periodics_reify (EV_P) |
560 | { |
783 | { |
561 | while (periodiccnt && periodics [0]->at <= ev_now) |
784 | while (periodiccnt && periodics [0]->at <= rt_now) |
562 | { |
785 | { |
563 | struct ev_periodic *w = periodics [0]; |
786 | struct ev_periodic *w = periodics [0]; |
564 | |
787 | |
565 | /* first reschedule or stop timer */ |
788 | /* first reschedule or stop timer */ |
566 | if (w->interval) |
789 | if (w->interval) |
567 | { |
790 | { |
568 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
791 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
569 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
792 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
570 | downheap ((WT *)periodics, periodiccnt, 0); |
793 | downheap ((WT *)periodics, periodiccnt, 0); |
571 | } |
794 | } |
572 | else |
795 | else |
573 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
796 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
574 | |
797 | |
575 | event ((W)w, EV_PERIODIC); |
798 | event (EV_A_ (W)w, EV_PERIODIC); |
576 | } |
799 | } |
577 | } |
800 | } |
578 | |
801 | |
579 | static void |
802 | static void |
580 | periodics_reschedule (ev_tstamp diff) |
803 | periodics_reschedule (EV_P) |
581 | { |
804 | { |
582 | int i; |
805 | int i; |
583 | |
806 | |
584 | /* adjust periodics after time jump */ |
807 | /* adjust periodics after time jump */ |
585 | for (i = 0; i < periodiccnt; ++i) |
808 | for (i = 0; i < periodiccnt; ++i) |
586 | { |
809 | { |
587 | struct ev_periodic *w = periodics [i]; |
810 | struct ev_periodic *w = periodics [i]; |
588 | |
811 | |
589 | if (w->interval) |
812 | if (w->interval) |
590 | { |
813 | { |
591 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
814 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
592 | |
815 | |
593 | if (fabs (diff) >= 1e-4) |
816 | if (fabs (diff) >= 1e-4) |
594 | { |
817 | { |
595 | ev_periodic_stop (w); |
818 | ev_periodic_stop (EV_A_ w); |
596 | ev_periodic_start (w); |
819 | ev_periodic_start (EV_A_ w); |
597 | |
820 | |
598 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
821 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
599 | } |
822 | } |
600 | } |
823 | } |
601 | } |
824 | } |
602 | } |
825 | } |
603 | |
826 | |
|
|
827 | inline int |
|
|
828 | time_update_monotonic (EV_P) |
|
|
829 | { |
|
|
830 | mn_now = get_clock (); |
|
|
831 | |
|
|
832 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
833 | { |
|
|
834 | rt_now = rtmn_diff + mn_now; |
|
|
835 | return 0; |
|
|
836 | } |
|
|
837 | else |
|
|
838 | { |
|
|
839 | now_floor = mn_now; |
|
|
840 | rt_now = ev_time (); |
|
|
841 | return 1; |
|
|
842 | } |
|
|
843 | } |
|
|
844 | |
604 | static void |
845 | static void |
605 | time_update (void) |
846 | time_update (EV_P) |
606 | { |
847 | { |
607 | int i; |
848 | int i; |
608 | |
849 | |
609 | ev_now = ev_time (); |
850 | #if EV_USE_MONOTONIC |
610 | |
|
|
611 | if (have_monotonic) |
851 | if (expect_true (have_monotonic)) |
612 | { |
852 | { |
613 | ev_tstamp odiff = diff; |
853 | if (time_update_monotonic (EV_A)) |
614 | |
|
|
615 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
616 | { |
854 | { |
617 | now = get_clock (); |
855 | ev_tstamp odiff = rtmn_diff; |
|
|
856 | |
|
|
857 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
858 | { |
618 | diff = ev_now - now; |
859 | rtmn_diff = rt_now - mn_now; |
619 | |
860 | |
620 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
861 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
621 | return; /* all is well */ |
862 | return; /* all is well */ |
622 | |
863 | |
623 | ev_now = ev_time (); |
864 | rt_now = ev_time (); |
|
|
865 | mn_now = get_clock (); |
|
|
866 | now_floor = mn_now; |
|
|
867 | } |
|
|
868 | |
|
|
869 | periodics_reschedule (EV_A); |
|
|
870 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
871 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
624 | } |
872 | } |
625 | |
|
|
626 | periodics_reschedule (diff - odiff); |
|
|
627 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
628 | } |
873 | } |
629 | else |
874 | else |
|
|
875 | #endif |
630 | { |
876 | { |
631 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
877 | rt_now = ev_time (); |
|
|
878 | |
|
|
879 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
632 | { |
880 | { |
633 | periodics_reschedule (ev_now - now); |
881 | periodics_reschedule (EV_A); |
634 | |
882 | |
635 | /* adjust timers. this is easy, as the offset is the same for all */ |
883 | /* adjust timers. this is easy, as the offset is the same for all */ |
636 | for (i = 0; i < timercnt; ++i) |
884 | for (i = 0; i < timercnt; ++i) |
637 | timers [i]->at += diff; |
885 | timers [i]->at += rt_now - mn_now; |
638 | } |
886 | } |
639 | |
887 | |
640 | now = ev_now; |
888 | mn_now = rt_now; |
641 | } |
889 | } |
642 | } |
890 | } |
643 | |
891 | |
644 | int ev_loop_done; |
892 | void |
|
|
893 | ev_ref (EV_P) |
|
|
894 | { |
|
|
895 | ++activecnt; |
|
|
896 | } |
645 | |
897 | |
|
|
898 | void |
|
|
899 | ev_unref (EV_P) |
|
|
900 | { |
|
|
901 | --activecnt; |
|
|
902 | } |
|
|
903 | |
|
|
904 | static int loop_done; |
|
|
905 | |
|
|
906 | void |
646 | void ev_loop (int flags) |
907 | ev_loop (EV_P_ int flags) |
647 | { |
908 | { |
648 | double block; |
909 | double block; |
649 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
910 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
650 | |
911 | |
651 | do |
912 | do |
652 | { |
913 | { |
653 | /* queue check watchers (and execute them) */ |
914 | /* queue check watchers (and execute them) */ |
654 | if (preparecnt) |
915 | if (expect_false (preparecnt)) |
655 | { |
916 | { |
656 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
917 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
657 | call_pending (); |
918 | call_pending (EV_A); |
658 | } |
919 | } |
659 | |
920 | |
660 | /* update fd-related kernel structures */ |
921 | /* update fd-related kernel structures */ |
661 | fd_reify (); |
922 | fd_reify (EV_A); |
662 | |
923 | |
663 | /* calculate blocking time */ |
924 | /* calculate blocking time */ |
664 | |
925 | |
665 | /* we only need this for !monotonic clockor timers, but as we basically |
926 | /* we only need this for !monotonic clockor timers, but as we basically |
666 | always have timers, we just calculate it always */ |
927 | always have timers, we just calculate it always */ |
|
|
928 | #if EV_USE_MONOTONIC |
|
|
929 | if (expect_true (have_monotonic)) |
|
|
930 | time_update_monotonic (EV_A); |
|
|
931 | else |
|
|
932 | #endif |
|
|
933 | { |
667 | ev_now = ev_time (); |
934 | rt_now = ev_time (); |
|
|
935 | mn_now = rt_now; |
|
|
936 | } |
668 | |
937 | |
669 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
938 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
670 | block = 0.; |
939 | block = 0.; |
671 | else |
940 | else |
672 | { |
941 | { |
673 | block = MAX_BLOCKTIME; |
942 | block = MAX_BLOCKTIME; |
674 | |
943 | |
675 | if (timercnt) |
944 | if (timercnt) |
676 | { |
945 | { |
677 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
946 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
678 | if (block > to) block = to; |
947 | if (block > to) block = to; |
679 | } |
948 | } |
680 | |
949 | |
681 | if (periodiccnt) |
950 | if (periodiccnt) |
682 | { |
951 | { |
683 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
952 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
684 | if (block > to) block = to; |
953 | if (block > to) block = to; |
685 | } |
954 | } |
686 | |
955 | |
687 | if (block < 0.) block = 0.; |
956 | if (block < 0.) block = 0.; |
688 | } |
957 | } |
689 | |
958 | |
690 | method_poll (block); |
959 | method_poll (EV_A_ block); |
691 | |
960 | |
692 | /* update ev_now, do magic */ |
961 | /* update rt_now, do magic */ |
693 | time_update (); |
962 | time_update (EV_A); |
694 | |
963 | |
695 | /* queue pending timers and reschedule them */ |
964 | /* queue pending timers and reschedule them */ |
696 | timers_reify (); /* relative timers called last */ |
965 | timers_reify (EV_A); /* relative timers called last */ |
697 | periodics_reify (); /* absolute timers called first */ |
966 | periodics_reify (EV_A); /* absolute timers called first */ |
698 | |
967 | |
699 | /* queue idle watchers unless io or timers are pending */ |
968 | /* queue idle watchers unless io or timers are pending */ |
700 | if (!pendingcnt) |
969 | if (!pendingcnt) |
701 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
970 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
702 | |
971 | |
703 | /* queue check watchers, to be executed first */ |
972 | /* queue check watchers, to be executed first */ |
704 | if (checkcnt) |
973 | if (checkcnt) |
705 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
974 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
706 | |
975 | |
707 | call_pending (); |
976 | call_pending (EV_A); |
708 | } |
977 | } |
709 | while (!ev_loop_done); |
978 | while (activecnt && !loop_done); |
710 | |
979 | |
711 | if (ev_loop_done != 2) |
980 | if (loop_done != 2) |
712 | ev_loop_done = 0; |
981 | loop_done = 0; |
|
|
982 | } |
|
|
983 | |
|
|
984 | void |
|
|
985 | ev_unloop (EV_P_ int how) |
|
|
986 | { |
|
|
987 | loop_done = how; |
713 | } |
988 | } |
714 | |
989 | |
715 | /*****************************************************************************/ |
990 | /*****************************************************************************/ |
716 | |
991 | |
717 | static void |
992 | inline void |
718 | wlist_add (WL *head, WL elem) |
993 | wlist_add (WL *head, WL elem) |
719 | { |
994 | { |
720 | elem->next = *head; |
995 | elem->next = *head; |
721 | *head = elem; |
996 | *head = elem; |
722 | } |
997 | } |
723 | |
998 | |
724 | static void |
999 | inline void |
725 | wlist_del (WL *head, WL elem) |
1000 | wlist_del (WL *head, WL elem) |
726 | { |
1001 | { |
727 | while (*head) |
1002 | while (*head) |
728 | { |
1003 | { |
729 | if (*head == elem) |
1004 | if (*head == elem) |
… | |
… | |
734 | |
1009 | |
735 | head = &(*head)->next; |
1010 | head = &(*head)->next; |
736 | } |
1011 | } |
737 | } |
1012 | } |
738 | |
1013 | |
739 | static void |
1014 | inline void |
740 | ev_clear_pending (W w) |
1015 | ev_clear_pending (EV_P_ W w) |
741 | { |
1016 | { |
742 | if (w->pending) |
1017 | if (w->pending) |
743 | { |
1018 | { |
744 | pendings [w->pending - 1].w = 0; |
1019 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
745 | w->pending = 0; |
1020 | w->pending = 0; |
746 | } |
1021 | } |
747 | } |
1022 | } |
748 | |
1023 | |
749 | static void |
1024 | inline void |
750 | ev_start (W w, int active) |
1025 | ev_start (EV_P_ W w, int active) |
751 | { |
1026 | { |
|
|
1027 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
1028 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
1029 | |
752 | w->active = active; |
1030 | w->active = active; |
|
|
1031 | ev_ref (EV_A); |
753 | } |
1032 | } |
754 | |
1033 | |
755 | static void |
1034 | inline void |
756 | ev_stop (W w) |
1035 | ev_stop (EV_P_ W w) |
757 | { |
1036 | { |
|
|
1037 | ev_unref (EV_A); |
758 | w->active = 0; |
1038 | w->active = 0; |
759 | } |
1039 | } |
760 | |
1040 | |
761 | /*****************************************************************************/ |
1041 | /*****************************************************************************/ |
762 | |
1042 | |
763 | void |
1043 | void |
764 | ev_io_start (struct ev_io *w) |
1044 | ev_io_start (EV_P_ struct ev_io *w) |
765 | { |
1045 | { |
|
|
1046 | int fd = w->fd; |
|
|
1047 | |
766 | if (ev_is_active (w)) |
1048 | if (ev_is_active (w)) |
767 | return; |
1049 | return; |
768 | |
1050 | |
769 | int fd = w->fd; |
|
|
770 | |
|
|
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1051 | assert (("ev_io_start called with negative fd", fd >= 0)); |
772 | |
1052 | |
773 | ev_start ((W)w, 1); |
1053 | ev_start (EV_A_ (W)w, 1); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1054 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
775 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1055 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
776 | |
1056 | |
777 | fd_change (fd); |
1057 | fd_change (EV_A_ fd); |
778 | } |
1058 | } |
779 | |
1059 | |
780 | void |
1060 | void |
781 | ev_io_stop (struct ev_io *w) |
1061 | ev_io_stop (EV_P_ struct ev_io *w) |
782 | { |
1062 | { |
783 | ev_clear_pending ((W)w); |
1063 | ev_clear_pending (EV_A_ (W)w); |
784 | if (!ev_is_active (w)) |
1064 | if (!ev_is_active (w)) |
785 | return; |
1065 | return; |
786 | |
1066 | |
787 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1067 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
788 | ev_stop ((W)w); |
1068 | ev_stop (EV_A_ (W)w); |
789 | |
1069 | |
790 | fd_change (w->fd); |
1070 | fd_change (EV_A_ w->fd); |
791 | } |
1071 | } |
792 | |
1072 | |
793 | void |
1073 | void |
794 | ev_timer_start (struct ev_timer *w) |
1074 | ev_timer_start (EV_P_ struct ev_timer *w) |
795 | { |
1075 | { |
796 | if (ev_is_active (w)) |
1076 | if (ev_is_active (w)) |
797 | return; |
1077 | return; |
798 | |
1078 | |
799 | w->at += now; |
1079 | w->at += mn_now; |
800 | |
1080 | |
801 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1081 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
802 | |
1082 | |
803 | ev_start ((W)w, ++timercnt); |
1083 | ev_start (EV_A_ (W)w, ++timercnt); |
804 | array_needsize (timers, timermax, timercnt, ); |
1084 | array_needsize (timers, timermax, timercnt, ); |
805 | timers [timercnt - 1] = w; |
1085 | timers [timercnt - 1] = w; |
806 | upheap ((WT *)timers, timercnt - 1); |
1086 | upheap ((WT *)timers, timercnt - 1); |
807 | } |
1087 | } |
808 | |
1088 | |
809 | void |
1089 | void |
810 | ev_timer_stop (struct ev_timer *w) |
1090 | ev_timer_stop (EV_P_ struct ev_timer *w) |
811 | { |
1091 | { |
812 | ev_clear_pending ((W)w); |
1092 | ev_clear_pending (EV_A_ (W)w); |
813 | if (!ev_is_active (w)) |
1093 | if (!ev_is_active (w)) |
814 | return; |
1094 | return; |
815 | |
1095 | |
816 | if (w->active < timercnt--) |
1096 | if (w->active < timercnt--) |
817 | { |
1097 | { |
… | |
… | |
819 | downheap ((WT *)timers, timercnt, w->active - 1); |
1099 | downheap ((WT *)timers, timercnt, w->active - 1); |
820 | } |
1100 | } |
821 | |
1101 | |
822 | w->at = w->repeat; |
1102 | w->at = w->repeat; |
823 | |
1103 | |
824 | ev_stop ((W)w); |
1104 | ev_stop (EV_A_ (W)w); |
825 | } |
1105 | } |
826 | |
1106 | |
827 | void |
1107 | void |
828 | ev_timer_again (struct ev_timer *w) |
1108 | ev_timer_again (EV_P_ struct ev_timer *w) |
829 | { |
1109 | { |
830 | if (ev_is_active (w)) |
1110 | if (ev_is_active (w)) |
831 | { |
1111 | { |
832 | if (w->repeat) |
1112 | if (w->repeat) |
833 | { |
1113 | { |
834 | w->at = now + w->repeat; |
1114 | w->at = mn_now + w->repeat; |
835 | downheap ((WT *)timers, timercnt, w->active - 1); |
1115 | downheap ((WT *)timers, timercnt, w->active - 1); |
836 | } |
1116 | } |
837 | else |
1117 | else |
838 | ev_timer_stop (w); |
1118 | ev_timer_stop (EV_A_ w); |
839 | } |
1119 | } |
840 | else if (w->repeat) |
1120 | else if (w->repeat) |
841 | ev_timer_start (w); |
1121 | ev_timer_start (EV_A_ w); |
842 | } |
1122 | } |
843 | |
1123 | |
844 | void |
1124 | void |
845 | ev_periodic_start (struct ev_periodic *w) |
1125 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
846 | { |
1126 | { |
847 | if (ev_is_active (w)) |
1127 | if (ev_is_active (w)) |
848 | return; |
1128 | return; |
849 | |
1129 | |
850 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1130 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
851 | |
1131 | |
852 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1132 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
853 | if (w->interval) |
1133 | if (w->interval) |
854 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1134 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
855 | |
1135 | |
856 | ev_start ((W)w, ++periodiccnt); |
1136 | ev_start (EV_A_ (W)w, ++periodiccnt); |
857 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1137 | array_needsize (periodics, periodicmax, periodiccnt, ); |
858 | periodics [periodiccnt - 1] = w; |
1138 | periodics [periodiccnt - 1] = w; |
859 | upheap ((WT *)periodics, periodiccnt - 1); |
1139 | upheap ((WT *)periodics, periodiccnt - 1); |
860 | } |
1140 | } |
861 | |
1141 | |
862 | void |
1142 | void |
863 | ev_periodic_stop (struct ev_periodic *w) |
1143 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
864 | { |
1144 | { |
865 | ev_clear_pending ((W)w); |
1145 | ev_clear_pending (EV_A_ (W)w); |
866 | if (!ev_is_active (w)) |
1146 | if (!ev_is_active (w)) |
867 | return; |
1147 | return; |
868 | |
1148 | |
869 | if (w->active < periodiccnt--) |
1149 | if (w->active < periodiccnt--) |
870 | { |
1150 | { |
871 | periodics [w->active - 1] = periodics [periodiccnt]; |
1151 | periodics [w->active - 1] = periodics [periodiccnt]; |
872 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1152 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
873 | } |
1153 | } |
874 | |
1154 | |
875 | ev_stop ((W)w); |
1155 | ev_stop (EV_A_ (W)w); |
876 | } |
1156 | } |
877 | |
1157 | |
878 | void |
1158 | void |
879 | ev_signal_start (struct ev_signal *w) |
1159 | ev_idle_start (EV_P_ struct ev_idle *w) |
880 | { |
1160 | { |
881 | if (ev_is_active (w)) |
1161 | if (ev_is_active (w)) |
882 | return; |
1162 | return; |
883 | |
1163 | |
|
|
1164 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1165 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1166 | idles [idlecnt - 1] = w; |
|
|
1167 | } |
|
|
1168 | |
|
|
1169 | void |
|
|
1170 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1171 | { |
|
|
1172 | ev_clear_pending (EV_A_ (W)w); |
|
|
1173 | if (ev_is_active (w)) |
|
|
1174 | return; |
|
|
1175 | |
|
|
1176 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1177 | ev_stop (EV_A_ (W)w); |
|
|
1178 | } |
|
|
1179 | |
|
|
1180 | void |
|
|
1181 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1182 | { |
|
|
1183 | if (ev_is_active (w)) |
|
|
1184 | return; |
|
|
1185 | |
|
|
1186 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1187 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1188 | prepares [preparecnt - 1] = w; |
|
|
1189 | } |
|
|
1190 | |
|
|
1191 | void |
|
|
1192 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1193 | { |
|
|
1194 | ev_clear_pending (EV_A_ (W)w); |
|
|
1195 | if (ev_is_active (w)) |
|
|
1196 | return; |
|
|
1197 | |
|
|
1198 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1199 | ev_stop (EV_A_ (W)w); |
|
|
1200 | } |
|
|
1201 | |
|
|
1202 | void |
|
|
1203 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1204 | { |
|
|
1205 | if (ev_is_active (w)) |
|
|
1206 | return; |
|
|
1207 | |
|
|
1208 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1209 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1210 | checks [checkcnt - 1] = w; |
|
|
1211 | } |
|
|
1212 | |
|
|
1213 | void |
|
|
1214 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1215 | { |
|
|
1216 | ev_clear_pending (EV_A_ (W)w); |
|
|
1217 | if (ev_is_active (w)) |
|
|
1218 | return; |
|
|
1219 | |
|
|
1220 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1221 | ev_stop (EV_A_ (W)w); |
|
|
1222 | } |
|
|
1223 | |
|
|
1224 | #ifndef SA_RESTART |
|
|
1225 | # define SA_RESTART 0 |
|
|
1226 | #endif |
|
|
1227 | |
|
|
1228 | void |
|
|
1229 | ev_signal_start (EV_P_ struct ev_signal *w) |
|
|
1230 | { |
|
|
1231 | #if EV_MULTIPLICITY |
|
|
1232 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1233 | #endif |
|
|
1234 | if (ev_is_active (w)) |
|
|
1235 | return; |
|
|
1236 | |
884 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1237 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
885 | |
1238 | |
886 | ev_start ((W)w, 1); |
1239 | ev_start (EV_A_ (W)w, 1); |
887 | array_needsize (signals, signalmax, w->signum, signals_init); |
1240 | array_needsize (signals, signalmax, w->signum, signals_init); |
888 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1241 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
889 | |
1242 | |
890 | if (!w->next) |
1243 | if (!w->next) |
891 | { |
1244 | { |
892 | struct sigaction sa; |
1245 | struct sigaction sa; |
893 | sa.sa_handler = sighandler; |
1246 | sa.sa_handler = sighandler; |
894 | sigfillset (&sa.sa_mask); |
1247 | sigfillset (&sa.sa_mask); |
895 | sa.sa_flags = 0; |
1248 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
896 | sigaction (w->signum, &sa, 0); |
1249 | sigaction (w->signum, &sa, 0); |
897 | } |
1250 | } |
898 | } |
1251 | } |
899 | |
1252 | |
900 | void |
1253 | void |
901 | ev_signal_stop (struct ev_signal *w) |
1254 | ev_signal_stop (EV_P_ struct ev_signal *w) |
902 | { |
1255 | { |
903 | ev_clear_pending ((W)w); |
1256 | ev_clear_pending (EV_A_ (W)w); |
904 | if (!ev_is_active (w)) |
1257 | if (!ev_is_active (w)) |
905 | return; |
1258 | return; |
906 | |
1259 | |
907 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1260 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
908 | ev_stop ((W)w); |
1261 | ev_stop (EV_A_ (W)w); |
909 | |
1262 | |
910 | if (!signals [w->signum - 1].head) |
1263 | if (!signals [w->signum - 1].head) |
911 | signal (w->signum, SIG_DFL); |
1264 | signal (w->signum, SIG_DFL); |
912 | } |
1265 | } |
913 | |
1266 | |
914 | void |
1267 | void |
915 | ev_idle_start (struct ev_idle *w) |
1268 | ev_child_start (EV_P_ struct ev_child *w) |
916 | { |
1269 | { |
|
|
1270 | #if EV_MULTIPLICITY |
|
|
1271 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1272 | #endif |
917 | if (ev_is_active (w)) |
1273 | if (ev_is_active (w)) |
918 | return; |
1274 | return; |
919 | |
1275 | |
920 | ev_start ((W)w, ++idlecnt); |
1276 | ev_start (EV_A_ (W)w, 1); |
921 | array_needsize (idles, idlemax, idlecnt, ); |
1277 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
922 | idles [idlecnt - 1] = w; |
|
|
923 | } |
1278 | } |
924 | |
1279 | |
925 | void |
1280 | void |
926 | ev_idle_stop (struct ev_idle *w) |
1281 | ev_child_stop (EV_P_ struct ev_child *w) |
927 | { |
1282 | { |
928 | ev_clear_pending ((W)w); |
1283 | ev_clear_pending (EV_A_ (W)w); |
929 | if (ev_is_active (w)) |
1284 | if (ev_is_active (w)) |
930 | return; |
1285 | return; |
931 | |
1286 | |
932 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
933 | ev_stop ((W)w); |
|
|
934 | } |
|
|
935 | |
|
|
936 | void |
|
|
937 | ev_prepare_start (struct ev_prepare *w) |
|
|
938 | { |
|
|
939 | if (ev_is_active (w)) |
|
|
940 | return; |
|
|
941 | |
|
|
942 | ev_start ((W)w, ++preparecnt); |
|
|
943 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
944 | prepares [preparecnt - 1] = w; |
|
|
945 | } |
|
|
946 | |
|
|
947 | void |
|
|
948 | ev_prepare_stop (struct ev_prepare *w) |
|
|
949 | { |
|
|
950 | ev_clear_pending ((W)w); |
|
|
951 | if (ev_is_active (w)) |
|
|
952 | return; |
|
|
953 | |
|
|
954 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
955 | ev_stop ((W)w); |
|
|
956 | } |
|
|
957 | |
|
|
958 | void |
|
|
959 | ev_check_start (struct ev_check *w) |
|
|
960 | { |
|
|
961 | if (ev_is_active (w)) |
|
|
962 | return; |
|
|
963 | |
|
|
964 | ev_start ((W)w, ++checkcnt); |
|
|
965 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
966 | checks [checkcnt - 1] = w; |
|
|
967 | } |
|
|
968 | |
|
|
969 | void |
|
|
970 | ev_check_stop (struct ev_check *w) |
|
|
971 | { |
|
|
972 | ev_clear_pending ((W)w); |
|
|
973 | if (ev_is_active (w)) |
|
|
974 | return; |
|
|
975 | |
|
|
976 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
977 | ev_stop ((W)w); |
|
|
978 | } |
|
|
979 | |
|
|
980 | void |
|
|
981 | ev_child_start (struct ev_child *w) |
|
|
982 | { |
|
|
983 | if (ev_is_active (w)) |
|
|
984 | return; |
|
|
985 | |
|
|
986 | ev_start ((W)w, 1); |
|
|
987 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
988 | } |
|
|
989 | |
|
|
990 | void |
|
|
991 | ev_child_stop (struct ev_child *w) |
|
|
992 | { |
|
|
993 | ev_clear_pending ((W)w); |
|
|
994 | if (ev_is_active (w)) |
|
|
995 | return; |
|
|
996 | |
|
|
997 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1287 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
998 | ev_stop ((W)w); |
1288 | ev_stop (EV_A_ (W)w); |
999 | } |
1289 | } |
1000 | |
1290 | |
1001 | /*****************************************************************************/ |
1291 | /*****************************************************************************/ |
1002 | |
1292 | |
1003 | struct ev_once |
1293 | struct ev_once |
… | |
… | |
1007 | void (*cb)(int revents, void *arg); |
1297 | void (*cb)(int revents, void *arg); |
1008 | void *arg; |
1298 | void *arg; |
1009 | }; |
1299 | }; |
1010 | |
1300 | |
1011 | static void |
1301 | static void |
1012 | once_cb (struct ev_once *once, int revents) |
1302 | once_cb (EV_P_ struct ev_once *once, int revents) |
1013 | { |
1303 | { |
1014 | void (*cb)(int revents, void *arg) = once->cb; |
1304 | void (*cb)(int revents, void *arg) = once->cb; |
1015 | void *arg = once->arg; |
1305 | void *arg = once->arg; |
1016 | |
1306 | |
1017 | ev_io_stop (&once->io); |
1307 | ev_io_stop (EV_A_ &once->io); |
1018 | ev_timer_stop (&once->to); |
1308 | ev_timer_stop (EV_A_ &once->to); |
1019 | free (once); |
1309 | free (once); |
1020 | |
1310 | |
1021 | cb (revents, arg); |
1311 | cb (revents, arg); |
1022 | } |
1312 | } |
1023 | |
1313 | |
1024 | static void |
1314 | static void |
1025 | once_cb_io (struct ev_io *w, int revents) |
1315 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1026 | { |
1316 | { |
1027 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1317 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1028 | } |
1318 | } |
1029 | |
1319 | |
1030 | static void |
1320 | static void |
1031 | once_cb_to (struct ev_timer *w, int revents) |
1321 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1032 | { |
1322 | { |
1033 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1323 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1034 | } |
1324 | } |
1035 | |
1325 | |
1036 | void |
1326 | void |
1037 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1327 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1038 | { |
1328 | { |
1039 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1329 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1040 | |
1330 | |
1041 | if (!once) |
1331 | if (!once) |
1042 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1332 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1047 | |
1337 | |
1048 | ev_watcher_init (&once->io, once_cb_io); |
1338 | ev_watcher_init (&once->io, once_cb_io); |
1049 | if (fd >= 0) |
1339 | if (fd >= 0) |
1050 | { |
1340 | { |
1051 | ev_io_set (&once->io, fd, events); |
1341 | ev_io_set (&once->io, fd, events); |
1052 | ev_io_start (&once->io); |
1342 | ev_io_start (EV_A_ &once->io); |
1053 | } |
1343 | } |
1054 | |
1344 | |
1055 | ev_watcher_init (&once->to, once_cb_to); |
1345 | ev_watcher_init (&once->to, once_cb_to); |
1056 | if (timeout >= 0.) |
1346 | if (timeout >= 0.) |
1057 | { |
1347 | { |
1058 | ev_timer_set (&once->to, timeout, 0.); |
1348 | ev_timer_set (&once->to, timeout, 0.); |
1059 | ev_timer_start (&once->to); |
1349 | ev_timer_start (EV_A_ &once->to); |
1060 | } |
1350 | } |
1061 | } |
1351 | } |
1062 | } |
1352 | } |
1063 | |
1353 | |
1064 | /*****************************************************************************/ |
|
|
1065 | |
|
|
1066 | #if 0 |
|
|
1067 | |
|
|
1068 | struct ev_io wio; |
|
|
1069 | |
|
|
1070 | static void |
|
|
1071 | sin_cb (struct ev_io *w, int revents) |
|
|
1072 | { |
|
|
1073 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1074 | } |
|
|
1075 | |
|
|
1076 | static void |
|
|
1077 | ocb (struct ev_timer *w, int revents) |
|
|
1078 | { |
|
|
1079 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1080 | ev_timer_stop (w); |
|
|
1081 | ev_timer_start (w); |
|
|
1082 | } |
|
|
1083 | |
|
|
1084 | static void |
|
|
1085 | scb (struct ev_signal *w, int revents) |
|
|
1086 | { |
|
|
1087 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1088 | ev_io_stop (&wio); |
|
|
1089 | ev_io_start (&wio); |
|
|
1090 | } |
|
|
1091 | |
|
|
1092 | static void |
|
|
1093 | gcb (struct ev_signal *w, int revents) |
|
|
1094 | { |
|
|
1095 | fprintf (stderr, "generic %x\n", revents); |
|
|
1096 | |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | int main (void) |
|
|
1100 | { |
|
|
1101 | ev_init (0); |
|
|
1102 | |
|
|
1103 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1104 | ev_io_start (&wio); |
|
|
1105 | |
|
|
1106 | struct ev_timer t[10000]; |
|
|
1107 | |
|
|
1108 | #if 0 |
|
|
1109 | int i; |
|
|
1110 | for (i = 0; i < 10000; ++i) |
|
|
1111 | { |
|
|
1112 | struct ev_timer *w = t + i; |
|
|
1113 | ev_watcher_init (w, ocb, i); |
|
|
1114 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1115 | ev_timer_start (w); |
|
|
1116 | if (drand48 () < 0.5) |
|
|
1117 | ev_timer_stop (w); |
|
|
1118 | } |
|
|
1119 | #endif |
|
|
1120 | |
|
|
1121 | struct ev_timer t1; |
|
|
1122 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1123 | ev_timer_start (&t1); |
|
|
1124 | |
|
|
1125 | struct ev_signal sig; |
|
|
1126 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1127 | ev_signal_start (&sig); |
|
|
1128 | |
|
|
1129 | struct ev_check cw; |
|
|
1130 | ev_check_init (&cw, gcb); |
|
|
1131 | ev_check_start (&cw); |
|
|
1132 | |
|
|
1133 | struct ev_idle iw; |
|
|
1134 | ev_idle_init (&iw, gcb); |
|
|
1135 | ev_idle_start (&iw); |
|
|
1136 | |
|
|
1137 | ev_loop (0); |
|
|
1138 | |
|
|
1139 | return 0; |
|
|
1140 | } |
|
|
1141 | |
|
|
1142 | #endif |
|
|
1143 | |
|
|
1144 | |
|
|
1145 | |
|
|
1146 | |
|
|