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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines