ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.1 by root, Tue Oct 30 20:59:31 2007 UTC vs.
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC

1#include <math.h> 1#include <math.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <signal.h>
6#include <stddef.h>
3 7
4#include <stdio.h> 8#include <stdio.h>
5 9
10#include <assert.h>
6#include <errno.h> 11#include <errno.h>
7#include <sys/time.h> 12#include <sys/time.h>
8#include <time.h> 13#include <time.h>
9 14
10#ifdef CLOCK_MONOTONIC
11# define HAVE_MONOTONIC 1
12#endif
13
14#define HAVE_EPOLL 1 15#define HAVE_EPOLL 1
15#define HAVE_REALTIME 1 16
17#ifndef HAVE_MONOTONIC
18# ifdef CLOCK_MONOTONIC
19# define HAVE_MONOTONIC 1
20# endif
21#endif
22
23#ifndef HAVE_SELECT
16#define HAVE_SELECT 0 24# define HAVE_SELECT 1
25#endif
17 26
27#ifndef HAVE_EPOLL
28# define HAVE_EPOLL 0
29#endif
30
31#ifndef HAVE_REALTIME
32# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
33#endif
34
35#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
18#define MAX_BLOCKTIME 60. 36#define MAX_BLOCKTIME 60.
19 37
20#include "ev.h" 38#include "ev.h"
21 39
22struct ev_watcher { 40typedef struct ev_watcher *W;
23 EV_WATCHER (ev_watcher);
24};
25
26struct ev_watcher_list { 41typedef struct ev_watcher_list *WL;
27 EV_WATCHER_LIST (ev_watcher_list); 42typedef struct ev_watcher_time *WT;
28};
29 43
44static ev_tstamp now, diff; /* monotonic clock */
30ev_tstamp ev_now; 45ev_tstamp ev_now;
31int ev_method; 46int ev_method;
32 47
33static int have_monotonic; /* runtime */ 48static int have_monotonic; /* runtime */
34 49
35static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 50static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
36static void (*method_reify)(void); 51static void (*method_modify)(int fd, int oev, int nev);
37static void (*method_poll)(ev_tstamp timeout); 52static void (*method_poll)(ev_tstamp timeout);
53
54/*****************************************************************************/
38 55
39ev_tstamp 56ev_tstamp
40ev_time (void) 57ev_time (void)
41{ 58{
42#if HAVE_REALTIME 59#if HAVE_REALTIME
66} 83}
67 84
68#define array_needsize(base,cur,cnt,init) \ 85#define array_needsize(base,cur,cnt,init) \
69 if ((cnt) > cur) \ 86 if ((cnt) > cur) \
70 { \ 87 { \
71 int newcnt = cur; \ 88 int newcnt = cur ? cur << 1 : 16; \
72 do \
73 { \
74 newcnt += (newcnt >> 1) + 16; \
75 } \
76 while ((cnt) > newcnt); \
77 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
78 base = realloc (base, sizeof (*base) * (newcnt)); \ 89 base = realloc (base, sizeof (*base) * (newcnt)); \
79 init (base + cur, newcnt - cur); \ 90 init (base + cur, newcnt - cur); \
80 cur = newcnt; \ 91 cur = newcnt; \
81 } 92 }
82 93
94/*****************************************************************************/
95
83typedef struct 96typedef struct
84{ 97{
85 struct ev_io *head; 98 struct ev_io *head;
86 unsigned char wev, rev; /* want, received event set */ 99 unsigned char wev, rev; /* want, received event set */
87} ANFD; 100} ANFD;
103 } 116 }
104} 117}
105 118
106typedef struct 119typedef struct
107{ 120{
108 struct ev_watcher *w; 121 W w;
109 int events; 122 int events;
110} ANPENDING; 123} ANPENDING;
111 124
112static ANPENDING *pendings; 125static ANPENDING *pendings;
113static int pendingmax, pendingcnt; 126static int pendingmax, pendingcnt;
114 127
115static void 128static void
116event (struct ev_watcher *w, int events) 129event (W w, int events)
117{ 130{
131 if (w->active)
132 {
118 w->pending = ++pendingcnt; 133 w->pending = ++pendingcnt;
119 array_needsize (pendings, pendingmax, pendingcnt, ); 134 array_needsize (pendings, pendingmax, pendingcnt, );
120 pendings [pendingcnt - 1].w = w; 135 pendings [pendingcnt - 1].w = w;
121 pendings [pendingcnt - 1].events = events; 136 pendings [pendingcnt - 1].events = events;
137 }
122} 138}
123 139
124static void 140static void
125fd_event (int fd, int events) 141fd_event (int fd, int events)
126{ 142{
130 for (w = anfd->head; w; w = w->next) 146 for (w = anfd->head; w; w = w->next)
131 { 147 {
132 int ev = w->events & events; 148 int ev = w->events & events;
133 149
134 if (ev) 150 if (ev)
135 event ((struct ev_watcher *)w, ev); 151 event ((W)w, ev);
136 } 152 }
137} 153}
154
155static void
156queue_events (W *events, int eventcnt, int type)
157{
158 int i;
159
160 for (i = 0; i < eventcnt; ++i)
161 event (events [i], type);
162}
163
164/*****************************************************************************/
138 165
139static struct ev_timer **timers; 166static struct ev_timer **timers;
140static int timermax, timercnt; 167static int timermax, timercnt;
141 168
169static struct ev_periodic **periodics;
170static int periodicmax, periodiccnt;
171
142static void 172static void
143upheap (int k) 173upheap (WT *timers, int k)
144{ 174{
145 struct ev_timer *w = timers [k]; 175 WT w = timers [k];
146 176
147 while (k && timers [k >> 1]->at > w->at) 177 while (k && timers [k >> 1]->at > w->at)
148 { 178 {
149 timers [k] = timers [k >> 1]; 179 timers [k] = timers [k >> 1];
150 timers [k]->active = k + 1; 180 timers [k]->active = k + 1;
155 timers [k]->active = k + 1; 185 timers [k]->active = k + 1;
156 186
157} 187}
158 188
159static void 189static void
160downheap (int k) 190downheap (WT *timers, int N, int k)
161{ 191{
162 struct ev_timer *w = timers [k]; 192 WT w = timers [k];
163 193
164 while (k <= (timercnt >> 1)) 194 while (k < (N >> 1))
165 { 195 {
166 int j = k << 1; 196 int j = k << 1;
167 197
168 if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) 198 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
169 ++j; 199 ++j;
170 200
171 if (w->at <= timers [j]->at) 201 if (w->at <= timers [j]->at)
172 break; 202 break;
173 203
174 timers [k] = timers [j]; 204 timers [k] = timers [j];
175 timers [k]->active = k; 205 timers [k]->active = k + 1;
176 k = j; 206 k = j;
177 } 207 }
178 208
179 timers [k] = w; 209 timers [k] = w;
180 timers [k]->active = k + 1; 210 timers [k]->active = k + 1;
181} 211}
182 212
183static struct ev_signal **signals; 213/*****************************************************************************/
214
215typedef struct
216{
217 struct ev_signal *head;
218 sig_atomic_t gotsig;
219} ANSIG;
220
221static ANSIG *signals;
184static int signalmax, signalcnt; 222static int signalmax;
185 223
224static int sigpipe [2];
225static sig_atomic_t gotsig;
226static struct ev_io sigev;
227
186static void 228static void
187signals_init (struct ev_signal **base, int count) 229signals_init (ANSIG *base, int count)
188{ 230{
189 while (count--) 231 while (count--)
190 *base++ = 0; 232 {
233 base->head = 0;
234 base->gotsig = 0;
235 ++base;
236 }
191} 237}
238
239static void
240sighandler (int signum)
241{
242 signals [signum - 1].gotsig = 1;
243
244 if (!gotsig)
245 {
246 gotsig = 1;
247 write (sigpipe [1], &gotsig, 1);
248 }
249}
250
251static void
252sigcb (struct ev_io *iow, int revents)
253{
254 struct ev_signal *w;
255 int sig;
256
257 gotsig = 0;
258 read (sigpipe [0], &revents, 1);
259
260 for (sig = signalmax; sig--; )
261 if (signals [sig].gotsig)
262 {
263 signals [sig].gotsig = 0;
264
265 for (w = signals [sig].head; w; w = w->next)
266 event ((W)w, EV_SIGNAL);
267 }
268}
269
270static void
271siginit (void)
272{
273 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
274 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
275
276 /* rather than sort out wether we really need nb, set it */
277 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
278 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
279
280 evio_set (&sigev, sigpipe [0], EV_READ);
281 evio_start (&sigev);
282}
283
284/*****************************************************************************/
285
286static struct ev_idle **idles;
287static int idlemax, idlecnt;
288
289static struct ev_check **checks;
290static int checkmax, checkcnt;
291
292/*****************************************************************************/
192 293
193#if HAVE_EPOLL 294#if HAVE_EPOLL
194# include "ev_epoll.c" 295# include "ev_epoll.c"
195#endif 296#endif
196#if HAVE_SELECT 297#if HAVE_SELECT
206 have_monotonic = 1; 307 have_monotonic = 1;
207 } 308 }
208#endif 309#endif
209 310
210 ev_now = ev_time (); 311 ev_now = ev_time ();
312 now = get_clock ();
313 diff = ev_now - now;
211 314
315 if (pipe (sigpipe))
316 return 0;
317
318 ev_method = EVMETHOD_NONE;
212#if HAVE_EPOLL 319#if HAVE_EPOLL
213 if (epoll_init (flags)) 320 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
214 return ev_method;
215#endif 321#endif
216#if HAVE_SELECT 322#if HAVE_SELECT
217 if (select_init (flags)) 323 if (ev_method == EVMETHOD_NONE) select_init (flags);
218 return ev_method;
219#endif 324#endif
220 325
221 ev_method = EVMETHOD_NONE; 326 if (ev_method)
327 {
328 evw_init (&sigev, sigcb);
329 siginit ();
330 }
331
222 return ev_method; 332 return ev_method;
223} 333}
224 334
335/*****************************************************************************/
336
225void ev_prefork (void) 337void ev_prefork (void)
226{ 338{
339 /* nop */
227} 340}
228 341
229void ev_postfork_parent (void) 342void ev_postfork_parent (void)
230{ 343{
344 /* nop */
231} 345}
232 346
233void ev_postfork_child (void) 347void ev_postfork_child (void)
234{ 348{
235#if HAVE_EPOLL 349#if HAVE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL)
236 epoll_postfork_child (); 351 epoll_postfork_child ();
237#endif 352#endif
353
354 evio_stop (&sigev);
355 close (sigpipe [0]);
356 close (sigpipe [1]);
357 pipe (sigpipe);
358 siginit ();
359}
360
361/*****************************************************************************/
362
363static void
364fd_reify (void)
365{
366 int i;
367
368 for (i = 0; i < fdchangecnt; ++i)
369 {
370 int fd = fdchanges [i];
371 ANFD *anfd = anfds + fd;
372 struct ev_io *w;
373
374 int wev = 0;
375
376 for (w = anfd->head; w; w = w->next)
377 wev |= w->events;
378
379 if (anfd->wev != wev)
380 {
381 method_modify (fd, anfd->wev, wev);
382 anfd->wev = wev;
383 }
384 }
385
386 fdchangecnt = 0;
238} 387}
239 388
240static void 389static void
241call_pending () 390call_pending ()
242{ 391{
255 404
256 pendingcnt = 0; 405 pendingcnt = 0;
257} 406}
258 407
259static void 408static void
260timer_reify (void) 409timers_reify ()
261{ 410{
262 while (timercnt && timers [0]->at <= ev_now) 411 while (timercnt && timers [0]->at <= now)
263 { 412 {
264 struct ev_timer *w = timers [0]; 413 struct ev_timer *w = timers [0];
265 414
415 event ((W)w, EV_TIMEOUT);
416
266 /* first reschedule timer */ 417 /* first reschedule or stop timer */
267 if (w->repeat) 418 if (w->repeat)
268 { 419 {
269 fprintf (stderr, "a %f now %f repeat %f, %f\n", w->at, ev_now, w->repeat, w->repeat *1e30);//D
270 if (w->is_abs)
271 w->at += floor ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
272 else
273 w->at = ev_now + w->repeat; 420 w->at = now + w->repeat;
274 421 assert (("timer timeout in the past, negative repeat?", w->at > now));
275 fprintf (stderr, "b %f\n", w->at);//D 422 downheap ((WT *)timers, timercnt, 0);
276
277 downheap (0);
278 } 423 }
279 else 424 else
280 evtimer_stop (w); /* nonrepeating: stop timer */ 425 evtimer_stop (w); /* nonrepeating: stop timer */
426 }
427}
281 428
429static void
430periodics_reify ()
431{
432 while (periodiccnt && periodics [0]->at <= ev_now)
433 {
434 struct ev_periodic *w = periodics [0];
435
436 /* first reschedule or stop timer */
437 if (w->interval)
438 {
439 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
440 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
441 downheap ((WT *)periodics, periodiccnt, 0);
442 }
443 else
444 evperiodic_stop (w); /* nonrepeating: stop timer */
445
282 event ((struct ev_watcher *)w, EV_TIMEOUT); 446 event ((W)w, EV_TIMEOUT);
447 }
448}
449
450static void
451periodics_reschedule (ev_tstamp diff)
452{
453 int i;
454
455 /* adjust periodics after time jump */
456 for (i = 0; i < periodiccnt; ++i)
457 {
458 struct ev_periodic *w = periodics [i];
459
460 if (w->interval)
461 {
462 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
463
464 if (fabs (diff) >= 1e-4)
465 {
466 evperiodic_stop (w);
467 evperiodic_start (w);
468
469 i = 0; /* restart loop, inefficient, but time jumps should be rare */
470 }
471 }
472 }
473}
474
475static void
476time_update ()
477{
478 int i;
479
480 ev_now = ev_time ();
481
482 if (have_monotonic)
483 {
484 ev_tstamp odiff = diff;
485
486 for (i = 4; --i; ) /* loop a few times, before making important decisions */
487 {
488 now = get_clock ();
489 diff = ev_now - now;
490
491 if (fabs (odiff - diff) < MIN_TIMEJUMP)
492 return; /* all is well */
493
494 ev_now = ev_time ();
495 }
496
497 periodics_reschedule (diff - odiff);
498 /* no timer adjustment, as the monotonic clock doesn't jump */
499 }
500 else
501 {
502 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
503 {
504 periodics_reschedule (ev_now - now);
505
506 /* adjust timers. this is easy, as the offset is the same for all */
507 for (i = 0; i < timercnt; ++i)
508 timers [i]->at += diff;
509 }
510
511 now = ev_now;
283 } 512 }
284} 513}
285 514
286int ev_loop_done; 515int ev_loop_done;
287 516
288int ev_loop (int flags) 517void ev_loop (int flags)
289{ 518{
290 double block; 519 double block;
291 ev_loop_done = flags & EVLOOP_ONESHOT; 520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
521
522 if (checkcnt)
523 {
524 queue_events ((W *)checks, checkcnt, EV_CHECK);
525 call_pending ();
526 }
292 527
293 do 528 do
294 { 529 {
295 /* update fd-related kernel structures */ 530 /* update fd-related kernel structures */
296 method_reify (); fdchangecnt = 0; 531 fd_reify ();
297 532
298 /* calculate blocking time */ 533 /* calculate blocking time */
534
535 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
299 ev_now = ev_time (); 536 ev_now = ev_time ();
300 537
301 if (flags & EVLOOP_NONBLOCK) 538 if (flags & EVLOOP_NONBLOCK || idlecnt)
302 block = 0.; 539 block = 0.;
303 else if (!timercnt)
304 block = MAX_BLOCKTIME;
305 else 540 else
306 { 541 {
542 block = MAX_BLOCKTIME;
543
544 if (timercnt)
545 {
546 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
547 if (block > to) block = to;
548 }
549
550 if (periodiccnt)
551 {
307 block = timers [0]->at - ev_now + method_fudge; 552 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
553 if (block > to) block = to;
554 }
555
308 if (block < 0.) block = 0.; 556 if (block < 0.) block = 0.;
309 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
310 } 557 }
311 558
312 fprintf (stderr, "block %f\n", block);//D
313 method_poll (block); 559 method_poll (block);
314 560
561 /* update ev_now, do magic */
562 time_update ();
563
315 /* put pending timers into pendign queue and reschedule them */ 564 /* queue pending timers and reschedule them */
316 timer_reify (); 565 periodics_reify (); /* absolute timers first */
566 timers_reify (); /* relative timers second */
317 567
318 ev_now = ev_time (); 568 /* queue idle watchers unless io or timers are pending */
569 if (!pendingcnt)
570 queue_events ((W *)idles, idlecnt, EV_IDLE);
571
572 /* queue check and possibly idle watchers */
573 queue_events ((W *)checks, checkcnt, EV_CHECK);
574
319 call_pending (); 575 call_pending ();
320 } 576 }
321 while (!ev_loop_done); 577 while (!ev_loop_done);
322}
323 578
579 if (ev_loop_done != 2)
580 ev_loop_done = 0;
581}
582
583/*****************************************************************************/
584
324static void 585static void
325wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 586wlist_add (WL *head, WL elem)
326{ 587{
327 elem->next = *head; 588 elem->next = *head;
328 *head = elem; 589 *head = elem;
329} 590}
330 591
331static void 592static void
332wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 593wlist_del (WL *head, WL elem)
333{ 594{
334 while (*head) 595 while (*head)
335 { 596 {
336 if (*head == elem) 597 if (*head == elem)
337 { 598 {
342 head = &(*head)->next; 603 head = &(*head)->next;
343 } 604 }
344} 605}
345 606
346static void 607static void
347ev_start (struct ev_watcher *w, int active) 608ev_clear (W w)
348{ 609{
610 if (w->pending)
611 {
612 pendings [w->pending - 1].w = 0;
349 w->pending = 0; 613 w->pending = 0;
614 }
615}
616
617static void
618ev_start (W w, int active)
619{
350 w->active = active; 620 w->active = active;
351} 621}
352 622
353static void 623static void
354ev_stop (struct ev_watcher *w) 624ev_stop (W w)
355{ 625{
356 if (w->pending)
357 pendings [w->pending - 1].w = 0;
358
359 w->active = 0; 626 w->active = 0;
360 /* nop */
361} 627}
628
629/*****************************************************************************/
362 630
363void 631void
364evio_start (struct ev_io *w) 632evio_start (struct ev_io *w)
365{ 633{
366 if (ev_is_active (w)) 634 if (ev_is_active (w))
367 return; 635 return;
368 636
369 int fd = w->fd; 637 int fd = w->fd;
370 638
371 ev_start ((struct ev_watcher *)w, 1); 639 ev_start ((W)w, 1);
372 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 640 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
373 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 641 wlist_add ((WL *)&anfds[fd].head, (WL)w);
374 642
375 ++fdchangecnt; 643 ++fdchangecnt;
376 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 644 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
377 fdchanges [fdchangecnt - 1] = fd; 645 fdchanges [fdchangecnt - 1] = fd;
378} 646}
379 647
380void 648void
381evio_stop (struct ev_io *w) 649evio_stop (struct ev_io *w)
382{ 650{
651 ev_clear ((W)w);
383 if (!ev_is_active (w)) 652 if (!ev_is_active (w))
384 return; 653 return;
385 654
386 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 655 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
387 ev_stop ((struct ev_watcher *)w); 656 ev_stop ((W)w);
388 657
389 ++fdchangecnt; 658 ++fdchangecnt;
390 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 659 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
391 fdchanges [fdchangecnt - 1] = w->fd; 660 fdchanges [fdchangecnt - 1] = w->fd;
392} 661}
395evtimer_start (struct ev_timer *w) 664evtimer_start (struct ev_timer *w)
396{ 665{
397 if (ev_is_active (w)) 666 if (ev_is_active (w))
398 return; 667 return;
399 668
400 fprintf (stderr, "t1 %f a %d\n", w->at, w->is_abs);//D
401 if (w->is_abs)
402 {
403 if (w->repeat)
404 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
405 }
406 else
407 w->at += ev_now; 669 w->at += now;
408 fprintf (stderr, "t2 %f a %d\n", w->at, w->is_abs);//D
409 670
410 ev_start ((struct ev_watcher *)w, ++timercnt); 671 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
672
673 ev_start ((W)w, ++timercnt);
411 array_needsize (timers, timermax, timercnt, ); 674 array_needsize (timers, timermax, timercnt, );
412 timers [timercnt - 1] = w; 675 timers [timercnt - 1] = w;
413 upheap (timercnt - 1); 676 upheap ((WT *)timers, timercnt - 1);
414} 677}
415 678
416void 679void
417evtimer_stop (struct ev_timer *w) 680evtimer_stop (struct ev_timer *w)
418{ 681{
682 ev_clear ((W)w);
419 if (!ev_is_active (w)) 683 if (!ev_is_active (w))
420 return; 684 return;
421 685
686 if (w->active < timercnt--)
687 {
422 timers [w->active - 1] = timers [--timercnt]; 688 timers [w->active - 1] = timers [timercnt];
423 downheap (w->active - 1); 689 downheap ((WT *)timers, timercnt, w->active - 1);
424 ev_stop ((struct ev_watcher *)w); 690 }
691
692 w->at = w->repeat;
693
694 ev_stop ((W)w);
695}
696
697void
698evtimer_again (struct ev_timer *w)
699{
700 if (ev_is_active (w))
701 {
702 if (w->repeat)
703 {
704 w->at = now + w->repeat;
705 downheap ((WT *)timers, timercnt, w->active - 1);
706 }
707 else
708 evtimer_stop (w);
709 }
710 else if (w->repeat)
711 evtimer_start (w);
712}
713
714void
715evperiodic_start (struct ev_periodic *w)
716{
717 if (ev_is_active (w))
718 return;
719
720 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
721
722 /* this formula differs from the one in periodic_reify because we do not always round up */
723 if (w->interval)
724 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
725
726 ev_start ((W)w, ++periodiccnt);
727 array_needsize (periodics, periodicmax, periodiccnt, );
728 periodics [periodiccnt - 1] = w;
729 upheap ((WT *)periodics, periodiccnt - 1);
730}
731
732void
733evperiodic_stop (struct ev_periodic *w)
734{
735 ev_clear ((W)w);
736 if (!ev_is_active (w))
737 return;
738
739 if (w->active < periodiccnt--)
740 {
741 periodics [w->active - 1] = periodics [periodiccnt];
742 downheap ((WT *)periodics, periodiccnt, w->active - 1);
743 }
744
745 ev_stop ((W)w);
425} 746}
426 747
427void 748void
428evsignal_start (struct ev_signal *w) 749evsignal_start (struct ev_signal *w)
429{ 750{
430 if (ev_is_active (w)) 751 if (ev_is_active (w))
431 return; 752 return;
432 753
433 ev_start ((struct ev_watcher *)w, 1); 754 ev_start ((W)w, 1);
434 array_needsize (signals, signalmax, w->signum, signals_init); 755 array_needsize (signals, signalmax, w->signum, signals_init);
435 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 756 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
757
758 if (!w->next)
759 {
760 struct sigaction sa;
761 sa.sa_handler = sighandler;
762 sigfillset (&sa.sa_mask);
763 sa.sa_flags = 0;
764 sigaction (w->signum, &sa, 0);
765 }
436} 766}
437 767
438void 768void
439evsignal_stop (struct ev_signal *w) 769evsignal_stop (struct ev_signal *w)
440{ 770{
771 ev_clear ((W)w);
441 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
442 return; 773 return;
443 774
444 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
445 ev_stop ((struct ev_watcher *)w); 776 ev_stop ((W)w);
446}
447 777
778 if (!signals [w->signum - 1].head)
779 signal (w->signum, SIG_DFL);
780}
781
782void evidle_start (struct ev_idle *w)
783{
784 if (ev_is_active (w))
785 return;
786
787 ev_start ((W)w, ++idlecnt);
788 array_needsize (idles, idlemax, idlecnt, );
789 idles [idlecnt - 1] = w;
790}
791
792void evidle_stop (struct ev_idle *w)
793{
794 ev_clear ((W)w);
795 if (ev_is_active (w))
796 return;
797
798 idles [w->active - 1] = idles [--idlecnt];
799 ev_stop ((W)w);
800}
801
802void evcheck_start (struct ev_check *w)
803{
804 if (ev_is_active (w))
805 return;
806
807 ev_start ((W)w, ++checkcnt);
808 array_needsize (checks, checkmax, checkcnt, );
809 checks [checkcnt - 1] = w;
810}
811
812void evcheck_stop (struct ev_check *w)
813{
814 ev_clear ((W)w);
815 if (ev_is_active (w))
816 return;
817
818 checks [w->active - 1] = checks [--checkcnt];
819 ev_stop ((W)w);
820}
821
448/*****************************************************************************/ 822/*****************************************************************************/
823
824struct ev_once
825{
826 struct ev_io io;
827 struct ev_timer to;
828 void (*cb)(int revents, void *arg);
829 void *arg;
830};
831
832static void
833once_cb (struct ev_once *once, int revents)
834{
835 void (*cb)(int revents, void *arg) = once->cb;
836 void *arg = once->arg;
837
838 evio_stop (&once->io);
839 evtimer_stop (&once->to);
840 free (once);
841
842 cb (revents, arg);
843}
844
845static void
846once_cb_io (struct ev_io *w, int revents)
847{
848 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
849}
850
851static void
852once_cb_to (struct ev_timer *w, int revents)
853{
854 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
855}
856
857void
858ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
859{
860 struct ev_once *once = malloc (sizeof (struct ev_once));
861
862 if (!once)
863 cb (EV_ERROR, arg);
864 else
865 {
866 once->cb = cb;
867 once->arg = arg;
868
869 evw_init (&once->io, once_cb_io);
870
871 if (fd >= 0)
872 {
873 evio_set (&once->io, fd, events);
874 evio_start (&once->io);
875 }
876
877 evw_init (&once->to, once_cb_to);
878
879 if (timeout >= 0.)
880 {
881 evtimer_set (&once->to, timeout, 0.);
882 evtimer_start (&once->to);
883 }
884 }
885}
886
887/*****************************************************************************/
888
449#if 1 889#if 0
890
891struct ev_io wio;
450 892
451static void 893static void
452sin_cb (struct ev_io *w, int revents) 894sin_cb (struct ev_io *w, int revents)
453{ 895{
454 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 896 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
455} 897}
456 898
457static void 899static void
458ocb (struct ev_timer *w, int revents) 900ocb (struct ev_timer *w, int revents)
459{ 901{
460 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 902 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
903 evtimer_stop (w);
904 evtimer_start (w);
905}
906
907static void
908scb (struct ev_signal *w, int revents)
909{
910 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
911 evio_stop (&wio);
912 evio_start (&wio);
913}
914
915static void
916gcb (struct ev_signal *w, int revents)
917{
918 fprintf (stderr, "generic %x\n", revents);
919
461} 920}
462 921
463int main (void) 922int main (void)
464{ 923{
465 struct ev_io sin;
466
467 ev_init (0); 924 ev_init (0);
468 925
469 evw_init (&sin, sin_cb, 55);
470 evio_set (&sin, 0, EV_READ); 926 evio_init (&wio, sin_cb, 0, EV_READ);
471 evio_start (&sin); 927 evio_start (&wio);
928
929 struct ev_timer t[10000];
930
931#if 0
932 int i;
933 for (i = 0; i < 10000; ++i)
934 {
935 struct ev_timer *w = t + i;
936 evw_init (w, ocb, i);
937 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
938 evtimer_start (w);
939 if (drand48 () < 0.5)
940 evtimer_stop (w);
941 }
942#endif
472 943
473 struct ev_timer t1; 944 struct ev_timer t1;
474 evw_init (&t1, ocb, 1); 945 evtimer_init (&t1, ocb, 5, 10);
475 evtimer_set_rel (&t1, 1, 0);
476 evtimer_start (&t1); 946 evtimer_start (&t1);
477 947
478 struct ev_timer t2; 948 struct ev_signal sig;
479 evw_init (&t2, ocb, 2); 949 evsignal_init (&sig, scb, SIGQUIT);
480 evtimer_set_abs (&t2, ev_time () + 2, 0); 950 evsignal_start (&sig);
951
952 struct ev_check cw;
953 evcheck_init (&cw, gcb);
954 evcheck_start (&cw);
955
956 struct ev_idle iw;
957 evidle_init (&iw, gcb);
481 evtimer_start (&t2); 958 evidle_start (&iw);
482 959
483 ev_loop (0); 960 ev_loop (0);
484 961
485 return 0; 962 return 0;
486} 963}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines