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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines