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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines