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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines