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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines