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.21 by root, Wed Oct 31 18:37:38 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_EPOLL 1 50#ifndef HAVE_SELECT
16#define HAVE_REALTIME 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_prepare **prepares;
330static int preparemax, preparecnt;
331
332static struct ev_check **checks;
333static int checkmax, checkcnt;
334
335/*****************************************************************************/
193 336
194#if HAVE_EPOLL 337#if HAVE_EPOLL
195# include "ev_epoll.c" 338# include "ev_epoll.c"
196#endif 339#endif
197#if HAVE_SELECT 340#if HAVE_SELECT
210 353
211 ev_now = ev_time (); 354 ev_now = ev_time ();
212 now = get_clock (); 355 now = get_clock ();
213 diff = ev_now - now; 356 diff = ev_now - now;
214 357
358 if (pipe (sigpipe))
359 return 0;
360
361 ev_method = EVMETHOD_NONE;
215#if HAVE_EPOLL 362#if HAVE_EPOLL
216 if (epoll_init (flags)) 363 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
217 return ev_method;
218#endif 364#endif
219#if HAVE_SELECT 365#if HAVE_SELECT
220 if (select_init (flags)) 366 if (ev_method == EVMETHOD_NONE) select_init (flags);
221 return ev_method;
222#endif 367#endif
223 368
224 ev_method = EVMETHOD_NONE; 369 if (ev_method)
370 {
371 evw_init (&sigev, sigcb);
372 siginit ();
373 }
374
225 return ev_method; 375 return ev_method;
226} 376}
227 377
378/*****************************************************************************/
379
228void ev_prefork (void) 380void ev_prefork (void)
229{ 381{
382 /* nop */
230} 383}
231 384
232void ev_postfork_parent (void) 385void ev_postfork_parent (void)
233{ 386{
387 /* nop */
234} 388}
235 389
236void ev_postfork_child (void) 390void ev_postfork_child (void)
237{ 391{
238#if HAVE_EPOLL 392#if HAVE_EPOLL
239 if (ev_method == EVMETHOD_EPOLL) 393 if (ev_method == EVMETHOD_EPOLL)
240 epoll_postfork_child (); 394 epoll_postfork_child ();
241#endif 395#endif
396
397 evio_stop (&sigev);
398 close (sigpipe [0]);
399 close (sigpipe [1]);
400 pipe (sigpipe);
401 siginit ();
242} 402}
403
404/*****************************************************************************/
243 405
244static void 406static void
245fd_reify (void) 407fd_reify (void)
246{ 408{
247 int i; 409 int i;
268} 430}
269 431
270static void 432static void
271call_pending () 433call_pending ()
272{ 434{
273 int i; 435 while (pendingcnt)
274
275 for (i = 0; i < pendingcnt; ++i)
276 { 436 {
277 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
278 438
279 if (p->w) 439 if (p->w)
280 { 440 {
281 p->w->pending = 0; 441 p->w->pending = 0;
282 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
283 } 443 }
284 } 444 }
285
286 pendingcnt = 0;
287} 445}
288 446
289static void 447static void
290timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 448timers_reify ()
291{ 449{
292 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
293 { 451 {
294 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
295 455
296 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
297 if (w->repeat) 457 if (w->repeat)
298 { 458 {
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; 459 w->at = now + w->repeat;
303 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
304 assert (w->at > now);
305
306 downheap (timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
307 } 462 }
308 else 463 else
309 {
310 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
311 --timercnt; /* maybe pass by reference instead? */ 465 }
466}
467
468static void
469periodics_reify ()
470{
471 while (periodiccnt && periodics [0]->at <= ev_now)
472 {
473 struct ev_periodic *w = periodics [0];
474
475 /* first reschedule or stop timer */
476 if (w->interval)
312 } 477 {
478 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
479 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
480 downheap ((WT *)periodics, periodiccnt, 0);
481 }
482 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */
313 484
314 event ((struct ev_watcher *)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
486 }
487}
488
489static void
490periodics_reschedule (ev_tstamp diff)
491{
492 int i;
493
494 /* adjust periodics after time jump */
495 for (i = 0; i < periodiccnt; ++i)
496 {
497 struct ev_periodic *w = periodics [i];
498
499 if (w->interval)
500 {
501 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
502
503 if (fabs (diff) >= 1e-4)
504 {
505 evperiodic_stop (w);
506 evperiodic_start (w);
507
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 }
510 }
315 } 511 }
316} 512}
317 513
318static void 514static void
319time_update () 515time_update ()
320{ 516{
321 int i; 517 int i;
518
322 ev_now = ev_time (); 519 ev_now = ev_time ();
323 520
324 if (have_monotonic) 521 if (have_monotonic)
325 { 522 {
326 ev_tstamp odiff = diff; 523 ev_tstamp odiff = diff;
327 524
328 /* detecting time jumps is much more difficult */
329 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 525 for (i = 4; --i; ) /* loop a few times, before making important decisions */
330 { 526 {
331 now = get_clock (); 527 now = get_clock ();
332 diff = ev_now - now; 528 diff = ev_now - now;
333 529
334 if (fabs (odiff - diff) < MIN_TIMEJUMP) 530 if (fabs (odiff - diff) < MIN_TIMEJUMP)
335 return; /* all is well */ 531 return; /* all is well */
336 532
337 ev_now = ev_time (); 533 ev_now = ev_time ();
338 } 534 }
339 535
340 /* time jump detected, reschedule atimers */ 536 periodics_reschedule (diff - odiff);
341 for (i = 0; i < atimercnt; ++i) 537 /* 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 } 538 }
347 else 539 else
348 { 540 {
349 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
350 /* time jump detected, adjust rtimers */ 542 {
543 periodics_reschedule (ev_now - now);
544
545 /* adjust timers. this is easy, as the offset is the same for all */
351 for (i = 0; i < rtimercnt; ++i) 546 for (i = 0; i < timercnt; ++i)
352 rtimers [i]->at += ev_now - now; 547 timers [i]->at += diff;
548 }
353 549
354 now = ev_now; 550 now = ev_now;
355 } 551 }
356} 552}
357 553
358int ev_loop_done; 554int ev_loop_done;
359 555
360void ev_loop (int flags) 556void ev_loop (int flags)
361{ 557{
362 double block; 558 double block;
363 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
364 560
365 do 561 do
366 { 562 {
563 /* queue check watchers (and execute them) */
564 if (preparecnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
367 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
368 fd_reify (); 571 fd_reify ();
369 572
370 /* calculate blocking time */ 573 /* calculate blocking time */
574
575 /* we only need this for !monotonic clockor timers, but as we basically
576 always have timers, we just calculate it always */
577 ev_now = ev_time ();
578
371 if (flags & EVLOOP_NONBLOCK) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
372 block = 0.; 580 block = 0.;
373 else 581 else
374 { 582 {
375 block = MAX_BLOCKTIME; 583 block = MAX_BLOCKTIME;
376 584
377 if (rtimercnt) 585 if (timercnt)
378 { 586 {
379 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 587 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
380 if (block > to) block = to; 588 if (block > to) block = to;
381 } 589 }
382 590
383 if (atimercnt) 591 if (periodiccnt)
384 { 592 {
385 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 593 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
386 if (block > to) block = to; 594 if (block > to) block = to;
387 } 595 }
388 596
389 if (block < 0.) block = 0.; 597 if (block < 0.) block = 0.;
390 } 598 }
392 method_poll (block); 600 method_poll (block);
393 601
394 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
395 time_update (); 603 time_update ();
396 604
397 /* put pending timers into pendign queue and reschedule them */ 605 /* queue pending timers and reschedule them */
398 /* absolute timers first */ 606 timers_reify (); /* relative timers called last */
399 timers_reify (atimers, atimercnt, ev_now); 607 periodics_reify (); /* absolute timers called first */
400 /* relative timers second */ 608
401 timers_reify (rtimers, rtimercnt, now); 609 /* queue idle watchers unless io or timers are pending */
610 if (!pendingcnt)
611 queue_events ((W *)idles, idlecnt, EV_IDLE);
612
613 /* queue check watchers, to be executed first */
614 if (checkcnt)
615 queue_events ((W *)checks, checkcnt, EV_CHECK);
402 616
403 call_pending (); 617 call_pending ();
404 } 618 }
405 while (!ev_loop_done); 619 while (!ev_loop_done);
406}
407 620
621 if (ev_loop_done != 2)
622 ev_loop_done = 0;
623}
624
625/*****************************************************************************/
626
408static void 627static void
409wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 628wlist_add (WL *head, WL elem)
410{ 629{
411 elem->next = *head; 630 elem->next = *head;
412 *head = elem; 631 *head = elem;
413} 632}
414 633
415static void 634static void
416wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 635wlist_del (WL *head, WL elem)
417{ 636{
418 while (*head) 637 while (*head)
419 { 638 {
420 if (*head == elem) 639 if (*head == elem)
421 { 640 {
426 head = &(*head)->next; 645 head = &(*head)->next;
427 } 646 }
428} 647}
429 648
430static void 649static void
431ev_start (struct ev_watcher *w, int active) 650ev_clear (W w)
432{ 651{
652 if (w->pending)
653 {
654 pendings [w->pending - 1].w = 0;
433 w->pending = 0; 655 w->pending = 0;
656 }
657}
658
659static void
660ev_start (W w, int active)
661{
434 w->active = active; 662 w->active = active;
435} 663}
436 664
437static void 665static void
438ev_stop (struct ev_watcher *w) 666ev_stop (W w)
439{ 667{
440 if (w->pending)
441 pendings [w->pending - 1].w = 0;
442
443 w->active = 0; 668 w->active = 0;
444 /* nop */
445} 669}
670
671/*****************************************************************************/
446 672
447void 673void
448evio_start (struct ev_io *w) 674evio_start (struct ev_io *w)
449{ 675{
450 if (ev_is_active (w)) 676 if (ev_is_active (w))
451 return; 677 return;
452 678
453 int fd = w->fd; 679 int fd = w->fd;
454 680
455 ev_start ((struct ev_watcher *)w, 1); 681 ev_start ((W)w, 1);
456 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 682 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
457 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 683 wlist_add ((WL *)&anfds[fd].head, (WL)w);
458 684
459 ++fdchangecnt; 685 ++fdchangecnt;
460 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 686 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
461 fdchanges [fdchangecnt - 1] = fd; 687 fdchanges [fdchangecnt - 1] = fd;
462} 688}
463 689
464void 690void
465evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
466{ 692{
693 ev_clear ((W)w);
467 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
468 return; 695 return;
469 696
470 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
471 ev_stop ((struct ev_watcher *)w); 698 ev_stop ((W)w);
472 699
473 ++fdchangecnt; 700 ++fdchangecnt;
474 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
475 fdchanges [fdchangecnt - 1] = w->fd; 702 fdchanges [fdchangecnt - 1] = w->fd;
476} 703}
479evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
480{ 707{
481 if (ev_is_active (w)) 708 if (ev_is_active (w))
482 return; 709 return;
483 710
484 if (w->is_abs) 711 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 712
490 ev_start ((struct ev_watcher *)w, ++atimercnt); 713 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
714
715 ev_start ((W)w, ++timercnt);
491 array_needsize (atimers, atimermax, atimercnt, ); 716 array_needsize (timers, timermax, timercnt, );
492 atimers [atimercnt - 1] = w; 717 timers [timercnt - 1] = w;
493 upheap (atimers, atimercnt - 1); 718 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} 719}
506 720
507void 721void
508evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
509{ 723{
724 ev_clear ((W)w);
510 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
511 return; 726 return;
512 727
513 if (w->is_abs)
514 {
515 if (w->active < atimercnt--) 728 if (w->active < timercnt--)
516 { 729 {
517 atimers [w->active - 1] = atimers [atimercnt]; 730 timers [w->active - 1] = timers [timercnt];
731 downheap ((WT *)timers, timercnt, w->active - 1);
732 }
733
734 w->at = w->repeat;
735
736 ev_stop ((W)w);
737}
738
739void
740evtimer_again (struct ev_timer *w)
741{
742 if (ev_is_active (w))
743 {
744 if (w->repeat)
745 {
746 w->at = now + w->repeat;
518 downheap (atimers, atimercnt, w->active - 1); 747 downheap ((WT *)timers, timercnt, w->active - 1);
519 }
520 }
521 else
522 {
523 if (w->active < rtimercnt--)
524 { 748 }
525 rtimers [w->active - 1] = rtimers [rtimercnt]; 749 else
526 downheap (rtimers, rtimercnt, w->active - 1); 750 evtimer_stop (w);
527 } 751 }
752 else if (w->repeat)
753 evtimer_start (w);
754}
755
756void
757evperiodic_start (struct ev_periodic *w)
758{
759 if (ev_is_active (w))
760 return;
761
762 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
763
764 /* this formula differs from the one in periodic_reify because we do not always round up */
765 if (w->interval)
766 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
767
768 ev_start ((W)w, ++periodiccnt);
769 array_needsize (periodics, periodicmax, periodiccnt, );
770 periodics [periodiccnt - 1] = w;
771 upheap ((WT *)periodics, periodiccnt - 1);
772}
773
774void
775evperiodic_stop (struct ev_periodic *w)
776{
777 ev_clear ((W)w);
778 if (!ev_is_active (w))
779 return;
780
781 if (w->active < periodiccnt--)
528 } 782 {
783 periodics [w->active - 1] = periodics [periodiccnt];
784 downheap ((WT *)periodics, periodiccnt, w->active - 1);
785 }
529 786
530 ev_stop ((struct ev_watcher *)w); 787 ev_stop ((W)w);
531} 788}
532 789
533void 790void
534evsignal_start (struct ev_signal *w) 791evsignal_start (struct ev_signal *w)
535{ 792{
536 if (ev_is_active (w)) 793 if (ev_is_active (w))
537 return; 794 return;
538 795
539 ev_start ((struct ev_watcher *)w, 1); 796 ev_start ((W)w, 1);
540 array_needsize (signals, signalmax, w->signum, signals_init); 797 array_needsize (signals, signalmax, w->signum, signals_init);
541 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 798 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
799
800 if (!w->next)
801 {
802 struct sigaction sa;
803 sa.sa_handler = sighandler;
804 sigfillset (&sa.sa_mask);
805 sa.sa_flags = 0;
806 sigaction (w->signum, &sa, 0);
807 }
542} 808}
543 809
544void 810void
545evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
546{ 812{
813 ev_clear ((W)w);
547 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
548 return; 815 return;
549 816
550 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
551 ev_stop ((struct ev_watcher *)w); 818 ev_stop ((W)w);
552}
553 819
820 if (!signals [w->signum - 1].head)
821 signal (w->signum, SIG_DFL);
822}
823
824void evidle_start (struct ev_idle *w)
825{
826 if (ev_is_active (w))
827 return;
828
829 ev_start ((W)w, ++idlecnt);
830 array_needsize (idles, idlemax, idlecnt, );
831 idles [idlecnt - 1] = w;
832}
833
834void evidle_stop (struct ev_idle *w)
835{
836 ev_clear ((W)w);
837 if (ev_is_active (w))
838 return;
839
840 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w);
842}
843
844void evprepare_start (struct ev_prepare *w)
845{
846 if (ev_is_active (w))
847 return;
848
849 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w;
852}
853
854void evprepare_stop (struct ev_prepare *w)
855{
856 ev_clear ((W)w);
857 if (ev_is_active (w))
858 return;
859
860 prepares [w->active - 1] = prepares [--preparecnt];
861 ev_stop ((W)w);
862}
863
864void evcheck_start (struct ev_check *w)
865{
866 if (ev_is_active (w))
867 return;
868
869 ev_start ((W)w, ++checkcnt);
870 array_needsize (checks, checkmax, checkcnt, );
871 checks [checkcnt - 1] = w;
872}
873
874void evcheck_stop (struct ev_check *w)
875{
876 ev_clear ((W)w);
877 if (ev_is_active (w))
878 return;
879
880 checks [w->active - 1] = checks [--checkcnt];
881 ev_stop ((W)w);
882}
883
554/*****************************************************************************/ 884/*****************************************************************************/
885
886struct ev_once
887{
888 struct ev_io io;
889 struct ev_timer to;
890 void (*cb)(int revents, void *arg);
891 void *arg;
892};
893
894static void
895once_cb (struct ev_once *once, int revents)
896{
897 void (*cb)(int revents, void *arg) = once->cb;
898 void *arg = once->arg;
899
900 evio_stop (&once->io);
901 evtimer_stop (&once->to);
902 free (once);
903
904 cb (revents, arg);
905}
906
907static void
908once_cb_io (struct ev_io *w, int revents)
909{
910 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
911}
912
913static void
914once_cb_to (struct ev_timer *w, int revents)
915{
916 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
917}
918
919void
920ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
921{
922 struct ev_once *once = malloc (sizeof (struct ev_once));
923
924 if (!once)
925 cb (EV_ERROR, arg);
926 else
927 {
928 once->cb = cb;
929 once->arg = arg;
930
931 evw_init (&once->io, once_cb_io);
932
933 if (fd >= 0)
934 {
935 evio_set (&once->io, fd, events);
936 evio_start (&once->io);
937 }
938
939 evw_init (&once->to, once_cb_to);
940
941 if (timeout >= 0.)
942 {
943 evtimer_set (&once->to, timeout, 0.);
944 evtimer_start (&once->to);
945 }
946 }
947}
948
949/*****************************************************************************/
950
555#if 1 951#if 0
952
953struct ev_io wio;
556 954
557static void 955static void
558sin_cb (struct ev_io *w, int revents) 956sin_cb (struct ev_io *w, int revents)
559{ 957{
560 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 958 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); 964 //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); 965 evtimer_stop (w);
568 evtimer_start (w); 966 evtimer_start (w);
569} 967}
570 968
969static void
970scb (struct ev_signal *w, int revents)
971{
972 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
973 evio_stop (&wio);
974 evio_start (&wio);
975}
976
977static void
978gcb (struct ev_signal *w, int revents)
979{
980 fprintf (stderr, "generic %x\n", revents);
981
982}
983
571int main (void) 984int main (void)
572{ 985{
573 struct ev_io sin;
574
575 ev_init (0); 986 ev_init (0);
576 987
577 evw_init (&sin, sin_cb, 55);
578 evio_set (&sin, 0, EV_READ); 988 evio_init (&wio, sin_cb, 0, EV_READ);
579 evio_start (&sin); 989 evio_start (&wio);
580 990
581 struct ev_timer t[10000]; 991 struct ev_timer t[10000];
582 992
583#if 1 993#if 0
584 int i; 994 int i;
585 for (i = 0; i < 10000; ++i) 995 for (i = 0; i < 10000; ++i)
586 { 996 {
587 struct ev_timer *w = t + i; 997 struct ev_timer *w = t + i;
588 evw_init (w, ocb, i); 998 evw_init (w, ocb, i);
589 evtimer_set_abs (w, drand48 (), 0.99775533); 999 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
590 evtimer_start (w); 1000 evtimer_start (w);
591 if (drand48 () < 0.5) 1001 if (drand48 () < 0.5)
592 evtimer_stop (w); 1002 evtimer_stop (w);
593 } 1003 }
594#endif 1004#endif
595 1005
596 struct ev_timer t1; 1006 struct ev_timer t1;
597 evw_init (&t1, ocb, 0); 1007 evtimer_init (&t1, ocb, 5, 10);
598 evtimer_set_abs (&t1, 5, 10);
599 evtimer_start (&t1); 1008 evtimer_start (&t1);
600 1009
1010 struct ev_signal sig;
1011 evsignal_init (&sig, scb, SIGQUIT);
1012 evsignal_start (&sig);
1013
1014 struct ev_check cw;
1015 evcheck_init (&cw, gcb);
1016 evcheck_start (&cw);
1017
1018 struct ev_idle iw;
1019 evidle_init (&iw, gcb);
1020 evidle_start (&iw);
1021
601 ev_loop (0); 1022 ev_loop (0);
602 1023
603 return 0; 1024 return 0;
604} 1025}
605 1026

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines