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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines