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

Comparing libev/ev.c (file contents):
Revision 1.4 by root, Tue Oct 30 23:10:33 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
6#include <assert.h> 10#include <assert.h>
7#include <errno.h> 11#include <errno.h>
8#include <sys/time.h> 12#include <sys/time.h>
9#include <time.h> 13#include <time.h>
10 14
11#ifdef CLOCK_MONOTONIC
12# define HAVE_MONOTONIC 1
13#endif
14
15#define HAVE_EPOLL 1 15#define HAVE_EPOLL 1
16#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
17#define HAVE_SELECT 0 24# define HAVE_SELECT 1
25#endif
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
18 34
19#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 35#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
20#define MAX_BLOCKTIME 60. 36#define MAX_BLOCKTIME 60.
21 37
22#include "ev.h" 38#include "ev.h"
23 39
24struct ev_watcher { 40typedef struct ev_watcher *W;
25 EV_WATCHER (ev_watcher);
26};
27
28struct ev_watcher_list { 41typedef struct ev_watcher_list *WL;
29 EV_WATCHER_LIST (ev_watcher_list); 42typedef struct ev_watcher_time *WT;
30};
31 43
32static ev_tstamp now, diff; /* monotonic clock */ 44static ev_tstamp now, diff; /* monotonic clock */
33ev_tstamp ev_now; 45ev_tstamp ev_now;
34int ev_method; 46int ev_method;
35 47
36static int have_monotonic; /* runtime */ 48static int have_monotonic; /* runtime */
37 49
38static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 50static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
39static void (*method_reify)(void); 51static void (*method_modify)(int fd, int oev, int nev);
40static void (*method_poll)(ev_tstamp timeout); 52static void (*method_poll)(ev_tstamp timeout);
53
54/*****************************************************************************/
41 55
42ev_tstamp 56ev_tstamp
43ev_time (void) 57ev_time (void)
44{ 58{
45#if HAVE_REALTIME 59#if HAVE_REALTIME
70 84
71#define array_needsize(base,cur,cnt,init) \ 85#define array_needsize(base,cur,cnt,init) \
72 if ((cnt) > cur) \ 86 if ((cnt) > cur) \
73 { \ 87 { \
74 int newcnt = cur ? cur << 1 : 16; \ 88 int newcnt = cur ? cur << 1 : 16; \
75 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
76 base = realloc (base, sizeof (*base) * (newcnt)); \ 89 base = realloc (base, sizeof (*base) * (newcnt)); \
77 init (base + cur, newcnt - cur); \ 90 init (base + cur, newcnt - cur); \
78 cur = newcnt; \ 91 cur = newcnt; \
79 } 92 }
80 93
94/*****************************************************************************/
95
81typedef struct 96typedef struct
82{ 97{
83 struct ev_io *head; 98 struct ev_io *head;
84 unsigned char wev, rev; /* want, received event set */ 99 unsigned char wev, rev; /* want, received event set */
85} ANFD; 100} ANFD;
101 } 116 }
102} 117}
103 118
104typedef struct 119typedef struct
105{ 120{
106 struct ev_watcher *w; 121 W w;
107 int events; 122 int events;
108} ANPENDING; 123} ANPENDING;
109 124
110static ANPENDING *pendings; 125static ANPENDING *pendings;
111static int pendingmax, pendingcnt; 126static int pendingmax, pendingcnt;
112 127
113static void 128static void
114event (struct ev_watcher *w, int events) 129event (W w, int events)
115{ 130{
131 if (w->active)
132 {
116 w->pending = ++pendingcnt; 133 w->pending = ++pendingcnt;
117 array_needsize (pendings, pendingmax, pendingcnt, ); 134 array_needsize (pendings, pendingmax, pendingcnt, );
118 pendings [pendingcnt - 1].w = w; 135 pendings [pendingcnt - 1].w = w;
119 pendings [pendingcnt - 1].events = events; 136 pendings [pendingcnt - 1].events = events;
137 }
120} 138}
121 139
122static void 140static void
123fd_event (int fd, int events) 141fd_event (int fd, int events)
124{ 142{
128 for (w = anfd->head; w; w = w->next) 146 for (w = anfd->head; w; w = w->next)
129 { 147 {
130 int ev = w->events & events; 148 int ev = w->events & events;
131 149
132 if (ev) 150 if (ev)
133 event ((struct ev_watcher *)w, ev); 151 event ((W)w, ev);
134 } 152 }
135} 153}
136 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/*****************************************************************************/
165
137static struct ev_timer **atimers; 166static struct ev_timer **timers;
138static int atimermax, atimercnt; 167static int timermax, timercnt;
139 168
140static struct ev_timer **rtimers; 169static struct ev_periodic **periodics;
141static int rtimermax, rtimercnt; 170static int periodicmax, periodiccnt;
142 171
143static void 172static void
144upheap (struct ev_timer **timers, int k) 173upheap (WT *timers, int k)
145{ 174{
146 struct ev_timer *w = timers [k]; 175 WT w = timers [k];
147 176
148 while (k && timers [k >> 1]->at > w->at) 177 while (k && timers [k >> 1]->at > w->at)
149 { 178 {
150 timers [k] = timers [k >> 1]; 179 timers [k] = timers [k >> 1];
151 timers [k]->active = k + 1; 180 timers [k]->active = k + 1;
156 timers [k]->active = k + 1; 185 timers [k]->active = k + 1;
157 186
158} 187}
159 188
160static void 189static void
161downheap (struct ev_timer **timers, int N, int k) 190downheap (WT *timers, int N, int k)
162{ 191{
163 struct ev_timer *w = timers [k]; 192 WT w = timers [k];
164 193
165 while (k < (N >> 1)) 194 while (k < (N >> 1))
166 { 195 {
167 int j = k << 1; 196 int j = k << 1;
168 197
179 208
180 timers [k] = w; 209 timers [k] = w;
181 timers [k]->active = k + 1; 210 timers [k]->active = k + 1;
182} 211}
183 212
184static 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;
185static int signalmax; 222static int signalmax;
186 223
224static int sigpipe [2];
225static sig_atomic_t gotsig;
226static struct ev_io sigev;
227
187static void 228static void
188signals_init (struct ev_signal **base, int count) 229signals_init (ANSIG *base, int count)
189{ 230{
190 while (count--) 231 while (count--)
191 *base++ = 0; 232 {
233 base->head = 0;
234 base->gotsig = 0;
235 ++base;
236 }
192} 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/*****************************************************************************/
193 293
194#if HAVE_EPOLL 294#if HAVE_EPOLL
195# include "ev_epoll.c" 295# include "ev_epoll.c"
196#endif 296#endif
197#if HAVE_SELECT 297#if HAVE_SELECT
210 310
211 ev_now = ev_time (); 311 ev_now = ev_time ();
212 now = get_clock (); 312 now = get_clock ();
213 diff = ev_now - now; 313 diff = ev_now - now;
214 314
315 if (pipe (sigpipe))
316 return 0;
317
318 ev_method = EVMETHOD_NONE;
215#if HAVE_EPOLL 319#if HAVE_EPOLL
216 if (epoll_init (flags)) 320 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
217 return ev_method;
218#endif 321#endif
219#if HAVE_SELECT 322#if HAVE_SELECT
220 if (select_init (flags)) 323 if (ev_method == EVMETHOD_NONE) select_init (flags);
221 return ev_method;
222#endif 324#endif
223 325
224 ev_method = EVMETHOD_NONE; 326 if (ev_method)
327 {
328 evw_init (&sigev, sigcb);
329 siginit ();
330 }
331
225 return ev_method; 332 return ev_method;
226} 333}
227 334
335/*****************************************************************************/
336
228void ev_prefork (void) 337void ev_prefork (void)
229{ 338{
339 /* nop */
230} 340}
231 341
232void ev_postfork_parent (void) 342void ev_postfork_parent (void)
233{ 343{
344 /* nop */
234} 345}
235 346
236void ev_postfork_child (void) 347void ev_postfork_child (void)
237{ 348{
238#if HAVE_EPOLL 349#if HAVE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL)
239 epoll_postfork_child (); 351 epoll_postfork_child ();
240#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;
241} 387}
242 388
243static void 389static void
244call_pending () 390call_pending ()
245{ 391{
258 404
259 pendingcnt = 0; 405 pendingcnt = 0;
260} 406}
261 407
262static void 408static void
263timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 409timers_reify ()
264{ 410{
265 while (timercnt && timers [0]->at <= now) 411 while (timercnt && timers [0]->at <= now)
266 { 412 {
267 struct ev_timer *w = timers [0]; 413 struct ev_timer *w = timers [0];
414
415 event ((W)w, EV_TIMEOUT);
268 416
269 /* first reschedule or stop timer */ 417 /* first reschedule or stop timer */
270 if (w->repeat) 418 if (w->repeat)
271 { 419 {
272 if (w->is_abs)
273 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
274 else
275 w->at = now + w->repeat; 420 w->at = now + w->repeat;
276 421 assert (("timer timeout in the past, negative repeat?", w->at > now));
277 assert (w->at > now);
278
279 downheap (timers, timercnt, 0); 422 downheap ((WT *)timers, timercnt, 0);
280 } 423 }
281 else 424 else
282 {
283 evtimer_stop (w); /* nonrepeating: stop timer */ 425 evtimer_stop (w); /* nonrepeating: stop timer */
284 --timercnt; /* maybe pass by reference instead? */ 426 }
427}
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)
285 } 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 */
286 445
287 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 }
288 } 472 }
289} 473}
290 474
291static void 475static void
292time_update () 476time_update ()
293{ 477{
294 int i; 478 int i;
479
295 ev_now = ev_time (); 480 ev_now = ev_time ();
296 481
297 if (have_monotonic) 482 if (have_monotonic)
298 { 483 {
299 ev_tstamp odiff = diff; 484 ev_tstamp odiff = diff;
300 485
301 /* detecting time jumps is much more difficult */
302 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 486 for (i = 4; --i; ) /* loop a few times, before making important decisions */
303 { 487 {
304 now = get_clock (); 488 now = get_clock ();
305 diff = ev_now - now; 489 diff = ev_now - now;
306 490
307 if (fabs (odiff - diff) < MIN_TIMEJUMP) 491 if (fabs (odiff - diff) < MIN_TIMEJUMP)
308 return; /* all is well */ 492 return; /* all is well */
309 493
310 ev_now = ev_time (); 494 ev_now = ev_time ();
311 } 495 }
312 496
313 /* time jump detected, reschedule atimers */ 497 periodics_reschedule (diff - odiff);
314 for (i = 0; i < atimercnt; ++i) 498 /* no timer adjustment, as the monotonic clock doesn't jump */
315 {
316 struct ev_timer *w = atimers [i];
317 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
318 }
319 } 499 }
320 else 500 else
321 { 501 {
322 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 502 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
323 /* time jump detected, adjust rtimers */ 503 {
504 periodics_reschedule (ev_now - now);
505
506 /* adjust timers. this is easy, as the offset is the same for all */
324 for (i = 0; i < rtimercnt; ++i) 507 for (i = 0; i < timercnt; ++i)
325 rtimers [i]->at += ev_now - now; 508 timers [i]->at += diff;
509 }
326 510
327 now = ev_now; 511 now = ev_now;
328 } 512 }
329} 513}
330 514
331int ev_loop_done; 515int ev_loop_done;
332 516
333void ev_loop (int flags) 517void ev_loop (int flags)
334{ 518{
335 double block; 519 double block;
336 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 }
337 527
338 do 528 do
339 { 529 {
340 /* update fd-related kernel structures */ 530 /* update fd-related kernel structures */
341 method_reify (); fdchangecnt = 0; 531 fd_reify ();
342 532
343 /* 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 */
536 ev_now = ev_time ();
537
344 if (flags & EVLOOP_NONBLOCK) 538 if (flags & EVLOOP_NONBLOCK || idlecnt)
345 block = 0.; 539 block = 0.;
346 else 540 else
347 { 541 {
348 block = MAX_BLOCKTIME; 542 block = MAX_BLOCKTIME;
349 543
350 if (rtimercnt) 544 if (timercnt)
351 { 545 {
352 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 546 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
353 if (block > to) block = to; 547 if (block > to) block = to;
354 } 548 }
355 549
356 if (atimercnt) 550 if (periodiccnt)
357 { 551 {
358 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 552 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
359 if (block > to) block = to; 553 if (block > to) block = to;
360 } 554 }
361 555
362 if (block < 0.) block = 0.; 556 if (block < 0.) block = 0.;
363 } 557 }
365 method_poll (block); 559 method_poll (block);
366 560
367 /* update ev_now, do magic */ 561 /* update ev_now, do magic */
368 time_update (); 562 time_update ();
369 563
370 /* put pending timers into pendign queue and reschedule them */ 564 /* queue pending timers and reschedule them */
371 /* absolute timers first */ 565 periodics_reify (); /* absolute timers first */
372 timers_reify (atimers, atimercnt, ev_now);
373 /* relative timers second */ 566 timers_reify (); /* relative timers second */
374 timers_reify (rtimers, rtimercnt, now); 567
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);
375 574
376 call_pending (); 575 call_pending ();
377 } 576 }
378 while (!ev_loop_done); 577 while (!ev_loop_done);
379}
380 578
579 if (ev_loop_done != 2)
580 ev_loop_done = 0;
581}
582
583/*****************************************************************************/
584
381static void 585static void
382wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 586wlist_add (WL *head, WL elem)
383{ 587{
384 elem->next = *head; 588 elem->next = *head;
385 *head = elem; 589 *head = elem;
386} 590}
387 591
388static void 592static void
389wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 593wlist_del (WL *head, WL elem)
390{ 594{
391 while (*head) 595 while (*head)
392 { 596 {
393 if (*head == elem) 597 if (*head == elem)
394 { 598 {
399 head = &(*head)->next; 603 head = &(*head)->next;
400 } 604 }
401} 605}
402 606
403static void 607static void
404ev_start (struct ev_watcher *w, int active) 608ev_clear (W w)
405{ 609{
610 if (w->pending)
611 {
612 pendings [w->pending - 1].w = 0;
406 w->pending = 0; 613 w->pending = 0;
614 }
615}
616
617static void
618ev_start (W w, int active)
619{
407 w->active = active; 620 w->active = active;
408} 621}
409 622
410static void 623static void
411ev_stop (struct ev_watcher *w) 624ev_stop (W w)
412{ 625{
413 if (w->pending)
414 pendings [w->pending - 1].w = 0;
415
416 w->active = 0; 626 w->active = 0;
417 /* nop */
418} 627}
628
629/*****************************************************************************/
419 630
420void 631void
421evio_start (struct ev_io *w) 632evio_start (struct ev_io *w)
422{ 633{
423 if (ev_is_active (w)) 634 if (ev_is_active (w))
424 return; 635 return;
425 636
426 int fd = w->fd; 637 int fd = w->fd;
427 638
428 ev_start ((struct ev_watcher *)w, 1); 639 ev_start ((W)w, 1);
429 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 640 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
430 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 641 wlist_add ((WL *)&anfds[fd].head, (WL)w);
431 642
432 ++fdchangecnt; 643 ++fdchangecnt;
433 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 644 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
434 fdchanges [fdchangecnt - 1] = fd; 645 fdchanges [fdchangecnt - 1] = fd;
435} 646}
436 647
437void 648void
438evio_stop (struct ev_io *w) 649evio_stop (struct ev_io *w)
439{ 650{
651 ev_clear ((W)w);
440 if (!ev_is_active (w)) 652 if (!ev_is_active (w))
441 return; 653 return;
442 654
443 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);
444 ev_stop ((struct ev_watcher *)w); 656 ev_stop ((W)w);
445 657
446 ++fdchangecnt; 658 ++fdchangecnt;
447 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 659 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
448 fdchanges [fdchangecnt - 1] = w->fd; 660 fdchanges [fdchangecnt - 1] = w->fd;
449} 661}
452evtimer_start (struct ev_timer *w) 664evtimer_start (struct ev_timer *w)
453{ 665{
454 if (ev_is_active (w)) 666 if (ev_is_active (w))
455 return; 667 return;
456 668
457 if (w->is_abs) 669 w->at += now;
458 {
459 /* this formula differs from the one in timer_reify becuse we do not round up */
460 if (w->repeat)
461 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
462 670
463 ev_start ((struct ev_watcher *)w, ++atimercnt); 671 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
672
673 ev_start ((W)w, ++timercnt);
464 array_needsize (atimers, atimermax, atimercnt, ); 674 array_needsize (timers, timermax, timercnt, );
465 atimers [atimercnt - 1] = w; 675 timers [timercnt - 1] = w;
466 upheap (atimers, atimercnt - 1); 676 upheap ((WT *)timers, timercnt - 1);
467 }
468 else
469 {
470 w->at += now;
471
472 ev_start ((struct ev_watcher *)w, ++rtimercnt);
473 array_needsize (rtimers, rtimermax, rtimercnt, );
474 rtimers [rtimercnt - 1] = w;
475 upheap (rtimers, rtimercnt - 1);
476 }
477
478} 677}
479 678
480void 679void
481evtimer_stop (struct ev_timer *w) 680evtimer_stop (struct ev_timer *w)
482{ 681{
682 ev_clear ((W)w);
483 if (!ev_is_active (w)) 683 if (!ev_is_active (w))
484 return; 684 return;
485 685
486 if (w->is_abs)
487 {
488 if (w->active < atimercnt--) 686 if (w->active < timercnt--)
489 { 687 {
490 atimers [w->active - 1] = atimers [atimercnt]; 688 timers [w->active - 1] = timers [timercnt];
689 downheap ((WT *)timers, timercnt, w->active - 1);
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;
491 downheap (atimers, atimercnt, w->active - 1); 705 downheap ((WT *)timers, timercnt, w->active - 1);
492 }
493 }
494 else
495 {
496 if (w->active < rtimercnt--)
497 { 706 }
498 rtimers [w->active - 1] = rtimers [rtimercnt]; 707 else
499 downheap (rtimers, rtimercnt, w->active - 1); 708 evtimer_stop (w);
500 } 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--)
501 } 740 {
741 periodics [w->active - 1] = periodics [periodiccnt];
742 downheap ((WT *)periodics, periodiccnt, w->active - 1);
743 }
502 744
503 ev_stop ((struct ev_watcher *)w); 745 ev_stop ((W)w);
504} 746}
505 747
506void 748void
507evsignal_start (struct ev_signal *w) 749evsignal_start (struct ev_signal *w)
508{ 750{
509 if (ev_is_active (w)) 751 if (ev_is_active (w))
510 return; 752 return;
511 753
512 ev_start ((struct ev_watcher *)w, 1); 754 ev_start ((W)w, 1);
513 array_needsize (signals, signalmax, w->signum, signals_init); 755 array_needsize (signals, signalmax, w->signum, signals_init);
514 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 }
515} 766}
516 767
517void 768void
518evsignal_stop (struct ev_signal *w) 769evsignal_stop (struct ev_signal *w)
519{ 770{
771 ev_clear ((W)w);
520 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
521 return; 773 return;
522 774
523 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);
524 ev_stop ((struct ev_watcher *)w); 776 ev_stop ((W)w);
525}
526 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
527/*****************************************************************************/ 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
528#if 1 889#if 0
890
891struct ev_io wio;
529 892
530static void 893static void
531sin_cb (struct ev_io *w, int revents) 894sin_cb (struct ev_io *w, int revents)
532{ 895{
533 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 896 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
539 //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);
540 evtimer_stop (w); 903 evtimer_stop (w);
541 evtimer_start (w); 904 evtimer_start (w);
542} 905}
543 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
920}
921
544int main (void) 922int main (void)
545{ 923{
546 struct ev_io sin;
547
548 ev_init (0); 924 ev_init (0);
549 925
550 evw_init (&sin, sin_cb, 55);
551 evio_set (&sin, 0, EV_READ); 926 evio_init (&wio, sin_cb, 0, EV_READ);
552 evio_start (&sin); 927 evio_start (&wio);
553 928
554 struct ev_timer t[10000]; 929 struct ev_timer t[10000];
555 930
556#if 1 931#if 0
557 int i; 932 int i;
558 for (i = 0; i < 10000; ++i) 933 for (i = 0; i < 10000; ++i)
559 { 934 {
560 struct ev_timer *w = t + i; 935 struct ev_timer *w = t + i;
561 evw_init (w, ocb, i); 936 evw_init (w, ocb, i);
562 evtimer_set_abs (w, drand48 (), 0.99775533); 937 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
563 evtimer_start (w); 938 evtimer_start (w);
564 if (drand48 () < 0.5) 939 if (drand48 () < 0.5)
565 evtimer_stop (w); 940 evtimer_stop (w);
566 } 941 }
567#endif 942#endif
568 943
569 struct ev_timer t1; 944 struct ev_timer t1;
570 evw_init (&t1, ocb, 0); 945 evtimer_init (&t1, ocb, 5, 10);
571 evtimer_set_abs (&t1, 5, 10);
572 evtimer_start (&t1); 946 evtimer_start (&t1);
573 947
948 struct ev_signal sig;
949 evsignal_init (&sig, scb, SIGQUIT);
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);
958 evidle_start (&iw);
959
574 ev_loop (0); 960 ev_loop (0);
575 961
576 return 0; 962 return 0;
577} 963}
578 964

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines