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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines