|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (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. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
|
|
32 | #include <unistd.h> |
|
|
33 | #include <fcntl.h> |
|
|
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
3 | |
36 | |
4 | #include <stdio.h> |
37 | #include <stdio.h> |
5 | |
38 | |
6 | #include <assert.h> |
39 | #include <assert.h> |
7 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
8 | #include <sys/time.h> |
43 | #include <sys/time.h> |
9 | #include <time.h> |
44 | #include <time.h> |
10 | |
45 | |
|
|
46 | #ifndef HAVE_MONOTONIC |
11 | #ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
12 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
13 | #endif |
49 | # endif |
|
|
50 | #endif |
14 | |
51 | |
15 | #define HAVE_EPOLL 1 |
52 | #ifndef HAVE_SELECT |
16 | #define HAVE_REALTIME 1 |
|
|
17 | #define HAVE_SELECT 1 |
53 | # define HAVE_SELECT 1 |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef HAVE_EPOLL |
|
|
57 | # define HAVE_EPOLL 0 |
|
|
58 | #endif |
|
|
59 | |
|
|
60 | #ifndef HAVE_REALTIME |
|
|
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
62 | #endif |
18 | |
63 | |
19 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
20 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
21 | |
67 | |
22 | #include "ev.h" |
68 | #include "ev.h" |
23 | |
69 | |
24 | struct ev_watcher { |
70 | typedef struct ev_watcher *W; |
25 | EV_WATCHER (ev_watcher); |
|
|
26 | }; |
|
|
27 | |
|
|
28 | struct ev_watcher_list { |
71 | typedef struct ev_watcher_list *WL; |
29 | EV_WATCHER_LIST (ev_watcher_list); |
72 | typedef struct ev_watcher_time *WT; |
30 | }; |
|
|
31 | |
73 | |
32 | static ev_tstamp now, diff; /* monotonic clock */ |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
33 | ev_tstamp ev_now; |
75 | ev_tstamp ev_now; |
34 | int ev_method; |
76 | int ev_method; |
35 | |
77 | |
36 | static int have_monotonic; /* runtime */ |
78 | static int have_monotonic; /* runtime */ |
37 | |
79 | |
38 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
80 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
39 | static void (*method_modify)(int fd, int oev, int nev); |
81 | static void (*method_modify)(int fd, int oev, int nev); |
40 | static void (*method_poll)(ev_tstamp timeout); |
82 | static void (*method_poll)(ev_tstamp timeout); |
|
|
83 | |
|
|
84 | /*****************************************************************************/ |
41 | |
85 | |
42 | ev_tstamp |
86 | ev_tstamp |
43 | ev_time (void) |
87 | ev_time (void) |
44 | { |
88 | { |
45 | #if HAVE_REALTIME |
89 | #if HAVE_REALTIME |
… | |
… | |
69 | } |
113 | } |
70 | |
114 | |
71 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
72 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
73 | { \ |
117 | { \ |
74 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
75 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
76 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
77 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
78 | cur = newcnt; \ |
127 | cur = newcnt; \ |
79 | } |
128 | } |
80 | |
129 | |
|
|
130 | /*****************************************************************************/ |
|
|
131 | |
81 | typedef struct |
132 | typedef struct |
82 | { |
133 | { |
83 | struct ev_io *head; |
134 | struct ev_io *head; |
84 | unsigned char wev, rev; /* want, received event set */ |
135 | unsigned char wev, rev; /* want, received event set */ |
85 | } ANFD; |
136 | } ANFD; |
… | |
… | |
101 | } |
152 | } |
102 | } |
153 | } |
103 | |
154 | |
104 | typedef struct |
155 | typedef struct |
105 | { |
156 | { |
106 | struct ev_watcher *w; |
157 | W w; |
107 | int events; |
158 | int events; |
108 | } ANPENDING; |
159 | } ANPENDING; |
109 | |
160 | |
110 | static ANPENDING *pendings; |
161 | static ANPENDING *pendings; |
111 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
112 | |
163 | |
113 | static void |
164 | static void |
114 | event (struct ev_watcher *w, int events) |
165 | event (W w, int events) |
115 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
116 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
117 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
118 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
119 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
120 | } |
174 | } |
121 | |
175 | |
122 | static void |
176 | static void |
123 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
124 | { |
178 | { |
… | |
… | |
128 | for (w = anfd->head; w; w = w->next) |
182 | for (w = anfd->head; w; w = w->next) |
129 | { |
183 | { |
130 | int ev = w->events & events; |
184 | int ev = w->events & events; |
131 | |
185 | |
132 | if (ev) |
186 | if (ev) |
133 | event ((struct ev_watcher *)w, ev); |
187 | event ((W)w, ev); |
134 | } |
188 | } |
135 | } |
189 | } |
136 | |
190 | |
|
|
191 | static void |
|
|
192 | queue_events (W *events, int eventcnt, int type) |
|
|
193 | { |
|
|
194 | int i; |
|
|
195 | |
|
|
196 | for (i = 0; i < eventcnt; ++i) |
|
|
197 | event (events [i], type); |
|
|
198 | } |
|
|
199 | |
|
|
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck (void) |
|
|
203 | { |
|
|
204 | int fd; |
|
|
205 | |
|
|
206 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
207 | if (anfds [fd].wev) |
|
|
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
209 | while (anfds [fd].head) |
|
|
210 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
|
|
214 | } |
|
|
215 | |
|
|
216 | /*****************************************************************************/ |
|
|
217 | |
137 | static struct ev_timer **atimers; |
218 | static struct ev_timer **timers; |
138 | static int atimermax, atimercnt; |
219 | static int timermax, timercnt; |
139 | |
220 | |
140 | static struct ev_timer **rtimers; |
221 | static struct ev_periodic **periodics; |
141 | static int rtimermax, rtimercnt; |
222 | static int periodicmax, periodiccnt; |
142 | |
223 | |
143 | static void |
224 | static void |
144 | upheap (struct ev_timer **timers, int k) |
225 | upheap (WT *timers, int k) |
145 | { |
226 | { |
146 | struct ev_timer *w = timers [k]; |
227 | WT w = timers [k]; |
147 | |
228 | |
148 | while (k && timers [k >> 1]->at > w->at) |
229 | while (k && timers [k >> 1]->at > w->at) |
149 | { |
230 | { |
150 | timers [k] = timers [k >> 1]; |
231 | timers [k] = timers [k >> 1]; |
151 | timers [k]->active = k + 1; |
232 | timers [k]->active = k + 1; |
… | |
… | |
156 | timers [k]->active = k + 1; |
237 | timers [k]->active = k + 1; |
157 | |
238 | |
158 | } |
239 | } |
159 | |
240 | |
160 | static void |
241 | static void |
161 | downheap (struct ev_timer **timers, int N, int k) |
242 | downheap (WT *timers, int N, int k) |
162 | { |
243 | { |
163 | struct ev_timer *w = timers [k]; |
244 | WT w = timers [k]; |
164 | |
245 | |
165 | while (k < (N >> 1)) |
246 | while (k < (N >> 1)) |
166 | { |
247 | { |
167 | int j = k << 1; |
248 | int j = k << 1; |
168 | |
249 | |
… | |
… | |
179 | |
260 | |
180 | timers [k] = w; |
261 | timers [k] = w; |
181 | timers [k]->active = k + 1; |
262 | timers [k]->active = k + 1; |
182 | } |
263 | } |
183 | |
264 | |
184 | static struct ev_signal **signals; |
265 | /*****************************************************************************/ |
|
|
266 | |
|
|
267 | typedef struct |
|
|
268 | { |
|
|
269 | struct ev_signal *head; |
|
|
270 | sig_atomic_t gotsig; |
|
|
271 | } ANSIG; |
|
|
272 | |
|
|
273 | static ANSIG *signals; |
185 | static int signalmax; |
274 | static int signalmax; |
186 | |
275 | |
|
|
276 | static int sigpipe [2]; |
|
|
277 | static sig_atomic_t gotsig; |
|
|
278 | static struct ev_io sigev; |
|
|
279 | |
187 | static void |
280 | static void |
188 | signals_init (struct ev_signal **base, int count) |
281 | signals_init (ANSIG *base, int count) |
189 | { |
282 | { |
190 | while (count--) |
283 | while (count--) |
191 | *base++ = 0; |
284 | { |
|
|
285 | base->head = 0; |
|
|
286 | base->gotsig = 0; |
|
|
287 | ++base; |
|
|
288 | } |
192 | } |
289 | } |
|
|
290 | |
|
|
291 | static void |
|
|
292 | sighandler (int signum) |
|
|
293 | { |
|
|
294 | signals [signum - 1].gotsig = 1; |
|
|
295 | |
|
|
296 | if (!gotsig) |
|
|
297 | { |
|
|
298 | gotsig = 1; |
|
|
299 | write (sigpipe [1], &gotsig, 1); |
|
|
300 | } |
|
|
301 | } |
|
|
302 | |
|
|
303 | static void |
|
|
304 | sigcb (struct ev_io *iow, int revents) |
|
|
305 | { |
|
|
306 | struct ev_signal *w; |
|
|
307 | int sig; |
|
|
308 | |
|
|
309 | gotsig = 0; |
|
|
310 | read (sigpipe [0], &revents, 1); |
|
|
311 | |
|
|
312 | for (sig = signalmax; sig--; ) |
|
|
313 | if (signals [sig].gotsig) |
|
|
314 | { |
|
|
315 | signals [sig].gotsig = 0; |
|
|
316 | |
|
|
317 | for (w = signals [sig].head; w; w = w->next) |
|
|
318 | event ((W)w, EV_SIGNAL); |
|
|
319 | } |
|
|
320 | } |
|
|
321 | |
|
|
322 | static void |
|
|
323 | siginit (void) |
|
|
324 | { |
|
|
325 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
326 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
327 | |
|
|
328 | /* rather than sort out wether we really need nb, set it */ |
|
|
329 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
330 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
331 | |
|
|
332 | evio_set (&sigev, sigpipe [0], EV_READ); |
|
|
333 | evio_start (&sigev); |
|
|
334 | } |
|
|
335 | |
|
|
336 | /*****************************************************************************/ |
|
|
337 | |
|
|
338 | static struct ev_idle **idles; |
|
|
339 | static int idlemax, idlecnt; |
|
|
340 | |
|
|
341 | static struct ev_prepare **prepares; |
|
|
342 | static int preparemax, preparecnt; |
|
|
343 | |
|
|
344 | static struct ev_check **checks; |
|
|
345 | static int checkmax, checkcnt; |
|
|
346 | |
|
|
347 | /*****************************************************************************/ |
|
|
348 | |
|
|
349 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
350 | static struct ev_signal childev; |
|
|
351 | |
|
|
352 | #ifndef WCONTINUED |
|
|
353 | # define WCONTINUED 0 |
|
|
354 | #endif |
|
|
355 | |
|
|
356 | static void |
|
|
357 | childcb (struct ev_signal *sw, int revents) |
|
|
358 | { |
|
|
359 | struct ev_child *w; |
|
|
360 | int pid, status; |
|
|
361 | |
|
|
362 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
363 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
364 | if (w->pid == pid || w->pid == -1) |
|
|
365 | { |
|
|
366 | w->status = status; |
|
|
367 | event ((W)w, EV_CHILD); |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | /*****************************************************************************/ |
193 | |
372 | |
194 | #if HAVE_EPOLL |
373 | #if HAVE_EPOLL |
195 | # include "ev_epoll.c" |
374 | # include "ev_epoll.c" |
196 | #endif |
375 | #endif |
197 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
198 | # include "ev_select.c" |
377 | # include "ev_select.c" |
199 | #endif |
378 | #endif |
200 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
201 | int ev_init (int flags) |
392 | int ev_init (int flags) |
202 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
203 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
204 | { |
397 | { |
205 | struct timespec ts; |
398 | struct timespec ts; |
206 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
207 | have_monotonic = 1; |
400 | have_monotonic = 1; |
208 | } |
401 | } |
209 | #endif |
402 | #endif |
210 | |
403 | |
211 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
212 | now = get_clock (); |
405 | now = get_clock (); |
213 | diff = ev_now - now; |
406 | diff = ev_now - now; |
214 | |
407 | |
|
|
408 | if (pipe (sigpipe)) |
|
|
409 | return 0; |
|
|
410 | |
|
|
411 | ev_method = EVMETHOD_NONE; |
215 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
216 | if (epoll_init (flags)) |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
217 | return ev_method; |
|
|
218 | #endif |
414 | #endif |
219 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
220 | if (select_init (flags)) |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
221 | return ev_method; |
|
|
222 | #endif |
417 | #endif |
223 | |
418 | |
224 | ev_method = EVMETHOD_NONE; |
419 | if (ev_method) |
|
|
420 | { |
|
|
421 | evw_init (&sigev, sigcb); |
|
|
422 | siginit (); |
|
|
423 | |
|
|
424 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
425 | evsignal_start (&childev); |
|
|
426 | } |
|
|
427 | } |
|
|
428 | |
225 | return ev_method; |
429 | return ev_method; |
226 | } |
430 | } |
227 | |
431 | |
|
|
432 | /*****************************************************************************/ |
|
|
433 | |
|
|
434 | void |
228 | void ev_prefork (void) |
435 | ev_prefork (void) |
229 | { |
436 | { |
|
|
437 | /* nop */ |
230 | } |
438 | } |
231 | |
439 | |
|
|
440 | void |
232 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
233 | { |
442 | { |
|
|
443 | /* nop */ |
234 | } |
444 | } |
235 | |
445 | |
|
|
446 | void |
236 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
237 | { |
448 | { |
238 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
239 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
240 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
241 | #endif |
452 | #endif |
|
|
453 | |
|
|
454 | evio_stop (&sigev); |
|
|
455 | close (sigpipe [0]); |
|
|
456 | close (sigpipe [1]); |
|
|
457 | pipe (sigpipe); |
|
|
458 | siginit (); |
242 | } |
459 | } |
|
|
460 | |
|
|
461 | /*****************************************************************************/ |
243 | |
462 | |
244 | static void |
463 | static void |
245 | fd_reify (void) |
464 | fd_reify (void) |
246 | { |
465 | { |
247 | int i; |
466 | int i; |
… | |
… | |
266 | |
485 | |
267 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
268 | } |
487 | } |
269 | |
488 | |
270 | static void |
489 | static void |
271 | call_pending () |
490 | call_pending (void) |
272 | { |
491 | { |
273 | int i; |
492 | while (pendingcnt) |
274 | |
|
|
275 | for (i = 0; i < pendingcnt; ++i) |
|
|
276 | { |
493 | { |
277 | ANPENDING *p = pendings + i; |
494 | ANPENDING *p = pendings + --pendingcnt; |
278 | |
495 | |
279 | if (p->w) |
496 | if (p->w) |
280 | { |
497 | { |
281 | p->w->pending = 0; |
498 | p->w->pending = 0; |
282 | p->w->cb (p->w, p->events); |
499 | p->w->cb (p->w, p->events); |
283 | } |
500 | } |
284 | } |
501 | } |
285 | |
|
|
286 | pendingcnt = 0; |
|
|
287 | } |
502 | } |
288 | |
503 | |
289 | static void |
504 | static void |
290 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
505 | timers_reify (void) |
291 | { |
506 | { |
292 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
293 | { |
508 | { |
294 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
|
|
510 | |
|
|
511 | event ((W)w, EV_TIMEOUT); |
295 | |
512 | |
296 | /* first reschedule or stop timer */ |
513 | /* first reschedule or stop timer */ |
297 | if (w->repeat) |
514 | if (w->repeat) |
298 | { |
515 | { |
299 | if (w->is_abs) |
|
|
300 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
301 | else |
|
|
302 | w->at = now + w->repeat; |
516 | w->at = now + w->repeat; |
303 | |
517 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
304 | assert (w->at > now); |
|
|
305 | |
|
|
306 | downheap (timers, timercnt, 0); |
518 | downheap ((WT *)timers, timercnt, 0); |
307 | } |
519 | } |
308 | else |
520 | else |
309 | { |
|
|
310 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
311 | --timercnt; /* maybe pass by reference instead? */ |
522 | } |
|
|
523 | } |
|
|
524 | |
|
|
525 | static void |
|
|
526 | periodics_reify (void) |
|
|
527 | { |
|
|
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
529 | { |
|
|
530 | struct ev_periodic *w = periodics [0]; |
|
|
531 | |
|
|
532 | /* first reschedule or stop timer */ |
|
|
533 | if (w->interval) |
312 | } |
534 | { |
|
|
535 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
536 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
537 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
538 | } |
|
|
539 | else |
|
|
540 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
313 | |
541 | |
314 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
542 | event ((W)w, EV_TIMEOUT); |
315 | } |
543 | } |
316 | } |
544 | } |
317 | |
545 | |
318 | static void |
546 | static void |
319 | time_update () |
547 | periodics_reschedule (ev_tstamp diff) |
320 | { |
548 | { |
321 | int i; |
549 | int i; |
|
|
550 | |
|
|
551 | /* adjust periodics after time jump */ |
|
|
552 | for (i = 0; i < periodiccnt; ++i) |
|
|
553 | { |
|
|
554 | struct ev_periodic *w = periodics [i]; |
|
|
555 | |
|
|
556 | if (w->interval) |
|
|
557 | { |
|
|
558 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
559 | |
|
|
560 | if (fabs (diff) >= 1e-4) |
|
|
561 | { |
|
|
562 | evperiodic_stop (w); |
|
|
563 | evperiodic_start (w); |
|
|
564 | |
|
|
565 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
566 | } |
|
|
567 | } |
|
|
568 | } |
|
|
569 | } |
|
|
570 | |
|
|
571 | static void |
|
|
572 | time_update (void) |
|
|
573 | { |
|
|
574 | int i; |
|
|
575 | |
322 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
323 | |
577 | |
324 | if (have_monotonic) |
578 | if (have_monotonic) |
325 | { |
579 | { |
326 | ev_tstamp odiff = diff; |
580 | ev_tstamp odiff = diff; |
327 | |
581 | |
328 | /* detecting time jumps is much more difficult */ |
|
|
329 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
582 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
330 | { |
583 | { |
331 | now = get_clock (); |
584 | now = get_clock (); |
332 | diff = ev_now - now; |
585 | diff = ev_now - now; |
333 | |
586 | |
334 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
587 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
335 | return; /* all is well */ |
588 | return; /* all is well */ |
336 | |
589 | |
337 | ev_now = ev_time (); |
590 | ev_now = ev_time (); |
338 | } |
591 | } |
339 | |
592 | |
340 | /* time jump detected, reschedule atimers */ |
593 | periodics_reschedule (diff - odiff); |
341 | for (i = 0; i < atimercnt; ++i) |
594 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
342 | { |
|
|
343 | struct ev_timer *w = atimers [i]; |
|
|
344 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
345 | } |
|
|
346 | } |
595 | } |
347 | else |
596 | else |
348 | { |
597 | { |
349 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
598 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
350 | /* time jump detected, adjust rtimers */ |
599 | { |
|
|
600 | periodics_reschedule (ev_now - now); |
|
|
601 | |
|
|
602 | /* adjust timers. this is easy, as the offset is the same for all */ |
351 | for (i = 0; i < rtimercnt; ++i) |
603 | for (i = 0; i < timercnt; ++i) |
352 | rtimers [i]->at += ev_now - now; |
604 | timers [i]->at += diff; |
|
|
605 | } |
353 | |
606 | |
354 | now = ev_now; |
607 | now = ev_now; |
355 | } |
608 | } |
356 | } |
609 | } |
357 | |
610 | |
358 | int ev_loop_done; |
611 | int ev_loop_done; |
359 | |
612 | |
360 | void ev_loop (int flags) |
613 | void ev_loop (int flags) |
361 | { |
614 | { |
362 | double block; |
615 | double block; |
363 | ev_loop_done = flags & EVLOOP_ONESHOT; |
616 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
364 | |
617 | |
365 | do |
618 | do |
366 | { |
619 | { |
|
|
620 | /* queue check watchers (and execute them) */ |
|
|
621 | if (preparecnt) |
|
|
622 | { |
|
|
623 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
624 | call_pending (); |
|
|
625 | } |
|
|
626 | |
367 | /* update fd-related kernel structures */ |
627 | /* update fd-related kernel structures */ |
368 | fd_reify (); |
628 | fd_reify (); |
369 | |
629 | |
370 | /* calculate blocking time */ |
630 | /* calculate blocking time */ |
|
|
631 | |
|
|
632 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
633 | always have timers, we just calculate it always */ |
|
|
634 | ev_now = ev_time (); |
|
|
635 | |
371 | if (flags & EVLOOP_NONBLOCK) |
636 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
372 | block = 0.; |
637 | block = 0.; |
373 | else |
638 | else |
374 | { |
639 | { |
375 | block = MAX_BLOCKTIME; |
640 | block = MAX_BLOCKTIME; |
376 | |
641 | |
377 | if (rtimercnt) |
642 | if (timercnt) |
378 | { |
643 | { |
379 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
644 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
380 | if (block > to) block = to; |
645 | if (block > to) block = to; |
381 | } |
646 | } |
382 | |
647 | |
383 | if (atimercnt) |
648 | if (periodiccnt) |
384 | { |
649 | { |
385 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
650 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
386 | if (block > to) block = to; |
651 | if (block > to) block = to; |
387 | } |
652 | } |
388 | |
653 | |
389 | if (block < 0.) block = 0.; |
654 | if (block < 0.) block = 0.; |
390 | } |
655 | } |
… | |
… | |
392 | method_poll (block); |
657 | method_poll (block); |
393 | |
658 | |
394 | /* update ev_now, do magic */ |
659 | /* update ev_now, do magic */ |
395 | time_update (); |
660 | time_update (); |
396 | |
661 | |
397 | /* put pending timers into pendign queue and reschedule them */ |
662 | /* queue pending timers and reschedule them */ |
398 | /* absolute timers first */ |
663 | timers_reify (); /* relative timers called last */ |
399 | timers_reify (atimers, atimercnt, ev_now); |
664 | periodics_reify (); /* absolute timers called first */ |
400 | /* relative timers second */ |
665 | |
401 | timers_reify (rtimers, rtimercnt, now); |
666 | /* queue idle watchers unless io or timers are pending */ |
|
|
667 | if (!pendingcnt) |
|
|
668 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
|
|
669 | |
|
|
670 | /* queue check watchers, to be executed first */ |
|
|
671 | if (checkcnt) |
|
|
672 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
402 | |
673 | |
403 | call_pending (); |
674 | call_pending (); |
404 | } |
675 | } |
405 | while (!ev_loop_done); |
676 | while (!ev_loop_done); |
406 | } |
|
|
407 | |
677 | |
|
|
678 | if (ev_loop_done != 2) |
|
|
679 | ev_loop_done = 0; |
|
|
680 | } |
|
|
681 | |
|
|
682 | /*****************************************************************************/ |
|
|
683 | |
408 | static void |
684 | static void |
409 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
685 | wlist_add (WL *head, WL elem) |
410 | { |
686 | { |
411 | elem->next = *head; |
687 | elem->next = *head; |
412 | *head = elem; |
688 | *head = elem; |
413 | } |
689 | } |
414 | |
690 | |
415 | static void |
691 | static void |
416 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
692 | wlist_del (WL *head, WL elem) |
417 | { |
693 | { |
418 | while (*head) |
694 | while (*head) |
419 | { |
695 | { |
420 | if (*head == elem) |
696 | if (*head == elem) |
421 | { |
697 | { |
… | |
… | |
426 | head = &(*head)->next; |
702 | head = &(*head)->next; |
427 | } |
703 | } |
428 | } |
704 | } |
429 | |
705 | |
430 | static void |
706 | static void |
431 | ev_start (struct ev_watcher *w, int active) |
707 | ev_clear (W w) |
432 | { |
708 | { |
|
|
709 | if (w->pending) |
|
|
710 | { |
|
|
711 | pendings [w->pending - 1].w = 0; |
433 | w->pending = 0; |
712 | w->pending = 0; |
|
|
713 | } |
|
|
714 | } |
|
|
715 | |
|
|
716 | static void |
|
|
717 | ev_start (W w, int active) |
|
|
718 | { |
434 | w->active = active; |
719 | w->active = active; |
435 | } |
720 | } |
436 | |
721 | |
437 | static void |
722 | static void |
438 | ev_stop (struct ev_watcher *w) |
723 | ev_stop (W w) |
439 | { |
724 | { |
440 | if (w->pending) |
|
|
441 | pendings [w->pending - 1].w = 0; |
|
|
442 | |
|
|
443 | w->active = 0; |
725 | w->active = 0; |
444 | /* nop */ |
|
|
445 | } |
726 | } |
|
|
727 | |
|
|
728 | /*****************************************************************************/ |
446 | |
729 | |
447 | void |
730 | void |
448 | evio_start (struct ev_io *w) |
731 | evio_start (struct ev_io *w) |
449 | { |
732 | { |
450 | if (ev_is_active (w)) |
733 | if (ev_is_active (w)) |
451 | return; |
734 | return; |
452 | |
735 | |
453 | int fd = w->fd; |
736 | int fd = w->fd; |
454 | |
737 | |
455 | ev_start ((struct ev_watcher *)w, 1); |
738 | ev_start ((W)w, 1); |
456 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
739 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
457 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
458 | |
741 | |
459 | ++fdchangecnt; |
742 | ++fdchangecnt; |
460 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
461 | fdchanges [fdchangecnt - 1] = fd; |
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
462 | } |
748 | } |
463 | |
749 | |
464 | void |
750 | void |
465 | evio_stop (struct ev_io *w) |
751 | evio_stop (struct ev_io *w) |
466 | { |
752 | { |
|
|
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
|
|
755 | ev_clear ((W)w); |
467 | if (!ev_is_active (w)) |
756 | if (!ev_is_active (w)) |
468 | return; |
757 | return; |
469 | |
758 | |
470 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
471 | ev_stop ((struct ev_watcher *)w); |
760 | ev_stop ((W)w); |
472 | |
761 | |
473 | ++fdchangecnt; |
762 | ++fdchangecnt; |
474 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
763 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
475 | fdchanges [fdchangecnt - 1] = w->fd; |
764 | fdchanges [fdchangecnt - 1] = w->fd; |
476 | } |
765 | } |
… | |
… | |
479 | evtimer_start (struct ev_timer *w) |
768 | evtimer_start (struct ev_timer *w) |
480 | { |
769 | { |
481 | if (ev_is_active (w)) |
770 | if (ev_is_active (w)) |
482 | return; |
771 | return; |
483 | |
772 | |
484 | if (w->is_abs) |
773 | w->at += now; |
|
|
774 | |
|
|
775 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
776 | |
|
|
777 | ev_start ((W)w, ++timercnt); |
|
|
778 | array_needsize (timers, timermax, timercnt, ); |
|
|
779 | timers [timercnt - 1] = w; |
|
|
780 | upheap ((WT *)timers, timercnt - 1); |
|
|
781 | } |
|
|
782 | |
|
|
783 | void |
|
|
784 | evtimer_stop (struct ev_timer *w) |
|
|
785 | { |
|
|
786 | ev_clear ((W)w); |
|
|
787 | if (!ev_is_active (w)) |
|
|
788 | return; |
|
|
789 | |
|
|
790 | if (w->active < timercnt--) |
|
|
791 | { |
|
|
792 | timers [w->active - 1] = timers [timercnt]; |
|
|
793 | downheap ((WT *)timers, timercnt, w->active - 1); |
485 | { |
794 | } |
486 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
795 | |
|
|
796 | w->at = w->repeat; |
|
|
797 | |
|
|
798 | ev_stop ((W)w); |
|
|
799 | } |
|
|
800 | |
|
|
801 | void |
|
|
802 | evtimer_again (struct ev_timer *w) |
|
|
803 | { |
|
|
804 | if (ev_is_active (w)) |
|
|
805 | { |
487 | if (w->repeat) |
806 | if (w->repeat) |
|
|
807 | { |
|
|
808 | w->at = now + w->repeat; |
|
|
809 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
810 | } |
|
|
811 | else |
|
|
812 | evtimer_stop (w); |
|
|
813 | } |
|
|
814 | else if (w->repeat) |
|
|
815 | evtimer_start (w); |
|
|
816 | } |
|
|
817 | |
|
|
818 | void |
|
|
819 | evperiodic_start (struct ev_periodic *w) |
|
|
820 | { |
|
|
821 | if (ev_is_active (w)) |
|
|
822 | return; |
|
|
823 | |
|
|
824 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
825 | |
|
|
826 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
827 | if (w->interval) |
488 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
828 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
489 | |
829 | |
490 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
830 | ev_start ((W)w, ++periodiccnt); |
491 | array_needsize (atimers, atimermax, atimercnt, ); |
831 | array_needsize (periodics, periodicmax, periodiccnt, ); |
492 | atimers [atimercnt - 1] = w; |
832 | periodics [periodiccnt - 1] = w; |
493 | upheap (atimers, atimercnt - 1); |
833 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
834 | } |
|
|
835 | |
|
|
836 | void |
|
|
837 | evperiodic_stop (struct ev_periodic *w) |
|
|
838 | { |
|
|
839 | ev_clear ((W)w); |
|
|
840 | if (!ev_is_active (w)) |
|
|
841 | return; |
|
|
842 | |
|
|
843 | if (w->active < periodiccnt--) |
494 | } |
844 | { |
|
|
845 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
846 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
847 | } |
|
|
848 | |
|
|
849 | ev_stop ((W)w); |
|
|
850 | } |
|
|
851 | |
|
|
852 | void |
|
|
853 | evsignal_start (struct ev_signal *w) |
|
|
854 | { |
|
|
855 | if (ev_is_active (w)) |
|
|
856 | return; |
|
|
857 | |
|
|
858 | ev_start ((W)w, 1); |
|
|
859 | array_needsize (signals, signalmax, w->signum, signals_init); |
|
|
860 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
|
|
861 | |
|
|
862 | if (!w->next) |
|
|
863 | { |
|
|
864 | struct sigaction sa; |
|
|
865 | sa.sa_handler = sighandler; |
|
|
866 | sigfillset (&sa.sa_mask); |
|
|
867 | sa.sa_flags = 0; |
|
|
868 | sigaction (w->signum, &sa, 0); |
|
|
869 | } |
|
|
870 | } |
|
|
871 | |
|
|
872 | void |
|
|
873 | evsignal_stop (struct ev_signal *w) |
|
|
874 | { |
|
|
875 | ev_clear ((W)w); |
|
|
876 | if (!ev_is_active (w)) |
|
|
877 | return; |
|
|
878 | |
|
|
879 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
|
|
880 | ev_stop ((W)w); |
|
|
881 | |
|
|
882 | if (!signals [w->signum - 1].head) |
|
|
883 | signal (w->signum, SIG_DFL); |
|
|
884 | } |
|
|
885 | |
|
|
886 | void evidle_start (struct ev_idle *w) |
|
|
887 | { |
|
|
888 | if (ev_is_active (w)) |
|
|
889 | return; |
|
|
890 | |
|
|
891 | ev_start ((W)w, ++idlecnt); |
|
|
892 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
893 | idles [idlecnt - 1] = w; |
|
|
894 | } |
|
|
895 | |
|
|
896 | void evidle_stop (struct ev_idle *w) |
|
|
897 | { |
|
|
898 | ev_clear ((W)w); |
|
|
899 | if (ev_is_active (w)) |
|
|
900 | return; |
|
|
901 | |
|
|
902 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
903 | ev_stop ((W)w); |
|
|
904 | } |
|
|
905 | |
|
|
906 | void evprepare_start (struct ev_prepare *w) |
|
|
907 | { |
|
|
908 | if (ev_is_active (w)) |
|
|
909 | return; |
|
|
910 | |
|
|
911 | ev_start ((W)w, ++preparecnt); |
|
|
912 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
913 | prepares [preparecnt - 1] = w; |
|
|
914 | } |
|
|
915 | |
|
|
916 | void evprepare_stop (struct ev_prepare *w) |
|
|
917 | { |
|
|
918 | ev_clear ((W)w); |
|
|
919 | if (ev_is_active (w)) |
|
|
920 | return; |
|
|
921 | |
|
|
922 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
923 | ev_stop ((W)w); |
|
|
924 | } |
|
|
925 | |
|
|
926 | void evcheck_start (struct ev_check *w) |
|
|
927 | { |
|
|
928 | if (ev_is_active (w)) |
|
|
929 | return; |
|
|
930 | |
|
|
931 | ev_start ((W)w, ++checkcnt); |
|
|
932 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
933 | checks [checkcnt - 1] = w; |
|
|
934 | } |
|
|
935 | |
|
|
936 | void evcheck_stop (struct ev_check *w) |
|
|
937 | { |
|
|
938 | ev_clear ((W)w); |
|
|
939 | if (ev_is_active (w)) |
|
|
940 | return; |
|
|
941 | |
|
|
942 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
943 | ev_stop ((W)w); |
|
|
944 | } |
|
|
945 | |
|
|
946 | void evchild_start (struct ev_child *w) |
|
|
947 | { |
|
|
948 | if (ev_is_active (w)) |
|
|
949 | return; |
|
|
950 | |
|
|
951 | ev_start ((W)w, 1); |
|
|
952 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
953 | } |
|
|
954 | |
|
|
955 | void evchild_stop (struct ev_child *w) |
|
|
956 | { |
|
|
957 | ev_clear ((W)w); |
|
|
958 | if (ev_is_active (w)) |
|
|
959 | return; |
|
|
960 | |
|
|
961 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
962 | ev_stop ((W)w); |
|
|
963 | } |
|
|
964 | |
|
|
965 | /*****************************************************************************/ |
|
|
966 | |
|
|
967 | struct ev_once |
|
|
968 | { |
|
|
969 | struct ev_io io; |
|
|
970 | struct ev_timer to; |
|
|
971 | void (*cb)(int revents, void *arg); |
|
|
972 | void *arg; |
|
|
973 | }; |
|
|
974 | |
|
|
975 | static void |
|
|
976 | once_cb (struct ev_once *once, int revents) |
|
|
977 | { |
|
|
978 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
979 | void *arg = once->arg; |
|
|
980 | |
|
|
981 | evio_stop (&once->io); |
|
|
982 | evtimer_stop (&once->to); |
|
|
983 | free (once); |
|
|
984 | |
|
|
985 | cb (revents, arg); |
|
|
986 | } |
|
|
987 | |
|
|
988 | static void |
|
|
989 | once_cb_io (struct ev_io *w, int revents) |
|
|
990 | { |
|
|
991 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
992 | } |
|
|
993 | |
|
|
994 | static void |
|
|
995 | once_cb_to (struct ev_timer *w, int revents) |
|
|
996 | { |
|
|
997 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
998 | } |
|
|
999 | |
|
|
1000 | void |
|
|
1001 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
1002 | { |
|
|
1003 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
1004 | |
|
|
1005 | if (!once) |
|
|
1006 | cb (EV_ERROR, arg); |
495 | else |
1007 | else |
496 | { |
1008 | { |
497 | w->at += now; |
1009 | once->cb = cb; |
|
|
1010 | once->arg = arg; |
498 | |
1011 | |
499 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
1012 | evw_init (&once->io, once_cb_io); |
500 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
501 | rtimers [rtimercnt - 1] = w; |
|
|
502 | upheap (rtimers, rtimercnt - 1); |
|
|
503 | } |
|
|
504 | |
1013 | |
505 | } |
1014 | if (fd >= 0) |
506 | |
|
|
507 | void |
|
|
508 | evtimer_stop (struct ev_timer *w) |
|
|
509 | { |
|
|
510 | if (!ev_is_active (w)) |
|
|
511 | return; |
|
|
512 | |
|
|
513 | if (w->is_abs) |
|
|
514 | { |
|
|
515 | if (w->active < atimercnt--) |
|
|
516 | { |
|
|
517 | atimers [w->active - 1] = atimers [atimercnt]; |
|
|
518 | downheap (atimers, atimercnt, w->active - 1); |
|
|
519 | } |
1015 | { |
520 | } |
1016 | evio_set (&once->io, fd, events); |
521 | else |
1017 | evio_start (&once->io); |
522 | { |
|
|
523 | if (w->active < rtimercnt--) |
|
|
524 | { |
1018 | } |
525 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
1019 | |
526 | downheap (rtimers, rtimercnt, w->active - 1); |
1020 | evw_init (&once->to, once_cb_to); |
|
|
1021 | |
|
|
1022 | if (timeout >= 0.) |
527 | } |
1023 | { |
|
|
1024 | evtimer_set (&once->to, timeout, 0.); |
|
|
1025 | evtimer_start (&once->to); |
|
|
1026 | } |
528 | } |
1027 | } |
529 | |
|
|
530 | ev_stop ((struct ev_watcher *)w); |
|
|
531 | } |
1028 | } |
532 | |
1029 | |
533 | void |
|
|
534 | evsignal_start (struct ev_signal *w) |
|
|
535 | { |
|
|
536 | if (ev_is_active (w)) |
|
|
537 | return; |
|
|
538 | |
|
|
539 | ev_start ((struct ev_watcher *)w, 1); |
|
|
540 | array_needsize (signals, signalmax, w->signum, signals_init); |
|
|
541 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
|
|
542 | } |
|
|
543 | |
|
|
544 | void |
|
|
545 | evsignal_stop (struct ev_signal *w) |
|
|
546 | { |
|
|
547 | if (!ev_is_active (w)) |
|
|
548 | return; |
|
|
549 | |
|
|
550 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
|
|
551 | ev_stop ((struct ev_watcher *)w); |
|
|
552 | } |
|
|
553 | |
|
|
554 | /*****************************************************************************/ |
1030 | /*****************************************************************************/ |
|
|
1031 | |
555 | #if 1 |
1032 | #if 0 |
|
|
1033 | |
|
|
1034 | struct ev_io wio; |
556 | |
1035 | |
557 | static void |
1036 | static void |
558 | sin_cb (struct ev_io *w, int revents) |
1037 | sin_cb (struct ev_io *w, int revents) |
559 | { |
1038 | { |
560 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
1039 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
566 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1045 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
567 | evtimer_stop (w); |
1046 | evtimer_stop (w); |
568 | evtimer_start (w); |
1047 | evtimer_start (w); |
569 | } |
1048 | } |
570 | |
1049 | |
|
|
1050 | static void |
|
|
1051 | scb (struct ev_signal *w, int revents) |
|
|
1052 | { |
|
|
1053 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1054 | evio_stop (&wio); |
|
|
1055 | evio_start (&wio); |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | static void |
|
|
1059 | gcb (struct ev_signal *w, int revents) |
|
|
1060 | { |
|
|
1061 | fprintf (stderr, "generic %x\n", revents); |
|
|
1062 | |
|
|
1063 | } |
|
|
1064 | |
571 | int main (void) |
1065 | int main (void) |
572 | { |
1066 | { |
573 | struct ev_io sin; |
|
|
574 | |
|
|
575 | ev_init (0); |
1067 | ev_init (0); |
576 | |
1068 | |
577 | evw_init (&sin, sin_cb, 55); |
|
|
578 | evio_set (&sin, 0, EV_READ); |
1069 | evio_init (&wio, sin_cb, 0, EV_READ); |
579 | evio_start (&sin); |
1070 | evio_start (&wio); |
580 | |
1071 | |
581 | struct ev_timer t[10000]; |
1072 | struct ev_timer t[10000]; |
582 | |
1073 | |
583 | #if 1 |
1074 | #if 0 |
584 | int i; |
1075 | int i; |
585 | for (i = 0; i < 10000; ++i) |
1076 | for (i = 0; i < 10000; ++i) |
586 | { |
1077 | { |
587 | struct ev_timer *w = t + i; |
1078 | struct ev_timer *w = t + i; |
588 | evw_init (w, ocb, i); |
1079 | evw_init (w, ocb, i); |
589 | evtimer_set_abs (w, drand48 (), 0.99775533); |
1080 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
590 | evtimer_start (w); |
1081 | evtimer_start (w); |
591 | if (drand48 () < 0.5) |
1082 | if (drand48 () < 0.5) |
592 | evtimer_stop (w); |
1083 | evtimer_stop (w); |
593 | } |
1084 | } |
594 | #endif |
1085 | #endif |
595 | |
1086 | |
596 | struct ev_timer t1; |
1087 | struct ev_timer t1; |
597 | evw_init (&t1, ocb, 0); |
1088 | evtimer_init (&t1, ocb, 5, 10); |
598 | evtimer_set_abs (&t1, 5, 10); |
|
|
599 | evtimer_start (&t1); |
1089 | evtimer_start (&t1); |
600 | |
1090 | |
|
|
1091 | struct ev_signal sig; |
|
|
1092 | evsignal_init (&sig, scb, SIGQUIT); |
|
|
1093 | evsignal_start (&sig); |
|
|
1094 | |
|
|
1095 | struct ev_check cw; |
|
|
1096 | evcheck_init (&cw, gcb); |
|
|
1097 | evcheck_start (&cw); |
|
|
1098 | |
|
|
1099 | struct ev_idle iw; |
|
|
1100 | evidle_init (&iw, gcb); |
|
|
1101 | evidle_start (&iw); |
|
|
1102 | |
601 | ev_loop (0); |
1103 | ev_loop (0); |
602 | |
1104 | |
603 | return 0; |
1105 | return 0; |
604 | } |
1106 | } |
605 | |
1107 | |