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

Comparing libev/ev.c (file contents):
Revision 1.5 by root, Tue Oct 30 23:54:38 2007 UTC vs.
Revision 1.17 by root, Wed Oct 31 14:44:15 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines