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

Comparing libev/ev.c (file contents):
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC vs.
Revision 1.27 by root, Wed Oct 31 22:16:36 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>
7 36
8#include <stdio.h> 37#include <stdio.h>
9 38
10#include <assert.h> 39#include <assert.h>
11#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
12#include <sys/time.h> 43#include <sys/time.h>
13#include <time.h> 44#include <time.h>
14
15#define HAVE_EPOLL 1
16 45
17#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
18# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
19# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
20# endif 49# endif
32# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
33#endif 62#endif
34 63
35#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) */
36#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
37 67
38#include "ev.h" 68#include "ev.h"
39 69
40typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
41typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
83} 113}
84 114
85#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
86 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
87 { \ 117 { \
88 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
89 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
91 cur = newcnt; \ 127 cur = newcnt; \
92 } 128 }
93 129
94/*****************************************************************************/ 130/*****************************************************************************/
95 131
96typedef struct 132typedef struct
97{ 133{
98 struct ev_io *head; 134 struct ev_io *head;
99 unsigned char wev, rev; /* want, received event set */ 135 int events;
100} ANFD; 136} ANFD;
101 137
102static ANFD *anfds; 138static ANFD *anfds;
103static int anfdmax; 139static int anfdmax;
104 140
105static int *fdchanges;
106static int fdchangemax, fdchangecnt;
107
108static void 141static void
109anfds_init (ANFD *base, int count) 142anfds_init (ANFD *base, int count)
110{ 143{
111 while (count--) 144 while (count--)
112 { 145 {
113 base->head = 0; 146 base->head = 0;
114 base->wev = base->rev = EV_NONE; 147 base->events = EV_NONE;
115 ++base; 148 ++base;
116 } 149 }
117} 150}
118 151
119typedef struct 152typedef struct
136 pendings [pendingcnt - 1].events = events; 169 pendings [pendingcnt - 1].events = events;
137 } 170 }
138} 171}
139 172
140static void 173static void
174queue_events (W *events, int eventcnt, int type)
175{
176 int i;
177
178 for (i = 0; i < eventcnt; ++i)
179 event (events [i], type);
180}
181
182static void
141fd_event (int fd, int events) 183fd_event (int fd, int events)
142{ 184{
143 ANFD *anfd = anfds + fd; 185 ANFD *anfd = anfds + fd;
144 struct ev_io *w; 186 struct ev_io *w;
145 187
150 if (ev) 192 if (ev)
151 event ((W)w, ev); 193 event ((W)w, ev);
152 } 194 }
153} 195}
154 196
197/*****************************************************************************/
198
199static int *fdchanges;
200static int fdchangemax, fdchangecnt;
201
155static void 202static void
156queue_events (W *events, int eventcnt, int type) 203fd_reify (void)
157{ 204{
158 int i; 205 int i;
159 206
160 for (i = 0; i < eventcnt; ++i) 207 for (i = 0; i < fdchangecnt; ++i)
161 event (events [i], type); 208 {
209 int fd = fdchanges [i];
210 ANFD *anfd = anfds + fd;
211 struct ev_io *w;
212
213 int events = 0;
214
215 for (w = anfd->head; w; w = w->next)
216 events |= w->events;
217
218 anfd->events &= ~EV_REIFY;
219
220 if (anfd->events != events)
221 {
222 method_modify (fd, anfd->events, events);
223 anfd->events = events;
224 }
225 }
226
227 fdchangecnt = 0;
228}
229
230static void
231fd_change (int fd)
232{
233 if (anfds [fd].events & EV_REIFY)
234 return;
235
236 anfds [fd].events |= EV_REIFY;
237
238 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd;
241}
242
243/* called on EBADF to verify fds */
244static void
245fd_recheck (void)
246{
247 int fd;
248
249 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head)
253 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 evio_stop (anfds [fd].head);
256 }
162} 257}
163 258
164/*****************************************************************************/ 259/*****************************************************************************/
165 260
166static struct ev_timer **timers; 261static struct ev_timer **timers;
284/*****************************************************************************/ 379/*****************************************************************************/
285 380
286static struct ev_idle **idles; 381static struct ev_idle **idles;
287static int idlemax, idlecnt; 382static int idlemax, idlecnt;
288 383
384static struct ev_prepare **prepares;
385static int preparemax, preparecnt;
386
289static struct ev_check **checks; 387static struct ev_check **checks;
290static int checkmax, checkcnt; 388static int checkmax, checkcnt;
291 389
292/*****************************************************************************/ 390/*****************************************************************************/
293 391
392static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev;
394
395#ifndef WCONTINUED
396# define WCONTINUED 0
397#endif
398
399static void
400childcb (struct ev_signal *sw, int revents)
401{
402 struct ev_child *w;
403 int pid, status;
404
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1)
408 {
409 w->status = status;
410 event ((W)w, EV_CHILD);
411 }
412}
413
414/*****************************************************************************/
415
294#if HAVE_EPOLL 416#if HAVE_EPOLL
295# include "ev_epoll.c" 417# include "ev_epoll.c"
296#endif 418#endif
297#if HAVE_SELECT 419#if HAVE_SELECT
298# include "ev_select.c" 420# include "ev_select.c"
299#endif 421#endif
300 422
423int
424ev_version_major (void)
425{
426 return EV_VERSION_MAJOR;
427}
428
429int
430ev_version_minor (void)
431{
432 return EV_VERSION_MINOR;
433}
434
301int ev_init (int flags) 435int ev_init (int flags)
302{ 436{
437 if (!ev_method)
438 {
303#if HAVE_MONOTONIC 439#if HAVE_MONOTONIC
304 { 440 {
305 struct timespec ts; 441 struct timespec ts;
306 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
307 have_monotonic = 1; 443 have_monotonic = 1;
308 } 444 }
309#endif 445#endif
310 446
311 ev_now = ev_time (); 447 ev_now = ev_time ();
312 now = get_clock (); 448 now = get_clock ();
313 diff = ev_now - now; 449 diff = ev_now - now;
314 450
315 if (pipe (sigpipe)) 451 if (pipe (sigpipe))
316 return 0; 452 return 0;
317 453
318 ev_method = EVMETHOD_NONE; 454 ev_method = EVMETHOD_NONE;
319#if HAVE_EPOLL 455#if HAVE_EPOLL
320 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
321#endif 457#endif
322#if HAVE_SELECT 458#if HAVE_SELECT
323 if (ev_method == EVMETHOD_NONE) select_init (flags); 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
324#endif 460#endif
325 461
326 if (ev_method) 462 if (ev_method)
327 { 463 {
328 evw_init (&sigev, sigcb); 464 evw_init (&sigev, sigcb);
329 siginit (); 465 siginit ();
466
467 evsignal_init (&childev, childcb, SIGCHLD);
468 evsignal_start (&childev);
469 }
330 } 470 }
331 471
332 return ev_method; 472 return ev_method;
333} 473}
334 474
335/*****************************************************************************/ 475/*****************************************************************************/
336 476
477void
337void ev_prefork (void) 478ev_prefork (void)
338{ 479{
339 /* nop */ 480 /* nop */
340} 481}
341 482
483void
342void ev_postfork_parent (void) 484ev_postfork_parent (void)
343{ 485{
344 /* nop */ 486 /* nop */
345} 487}
346 488
489void
347void ev_postfork_child (void) 490ev_postfork_child (void)
348{ 491{
349#if HAVE_EPOLL 492#if HAVE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
351 epoll_postfork_child (); 494 epoll_postfork_child ();
352#endif 495#endif
359} 502}
360 503
361/*****************************************************************************/ 504/*****************************************************************************/
362 505
363static void 506static void
364fd_reify (void)
365{
366 int i;
367
368 for (i = 0; i < fdchangecnt; ++i)
369 {
370 int fd = fdchanges [i];
371 ANFD *anfd = anfds + fd;
372 struct ev_io *w;
373
374 int wev = 0;
375
376 for (w = anfd->head; w; w = w->next)
377 wev |= w->events;
378
379 if (anfd->wev != wev)
380 {
381 method_modify (fd, anfd->wev, wev);
382 anfd->wev = wev;
383 }
384 }
385
386 fdchangecnt = 0;
387}
388
389static void
390call_pending () 507call_pending (void)
391{ 508{
392 int i; 509 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 510 {
396 ANPENDING *p = pendings + i; 511 ANPENDING *p = pendings + --pendingcnt;
397 512
398 if (p->w) 513 if (p->w)
399 { 514 {
400 p->w->pending = 0; 515 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 516 p->w->cb (p->w, p->events);
402 } 517 }
403 } 518 }
404
405 pendingcnt = 0;
406} 519}
407 520
408static void 521static void
409timers_reify () 522timers_reify (void)
410{ 523{
411 while (timercnt && timers [0]->at <= now) 524 while (timercnt && timers [0]->at <= now)
412 { 525 {
413 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
414 527
425 evtimer_stop (w); /* nonrepeating: stop timer */ 538 evtimer_stop (w); /* nonrepeating: stop timer */
426 } 539 }
427} 540}
428 541
429static void 542static void
430periodics_reify () 543periodics_reify (void)
431{ 544{
432 while (periodiccnt && periodics [0]->at <= ev_now) 545 while (periodiccnt && periodics [0]->at <= ev_now)
433 { 546 {
434 struct ev_periodic *w = periodics [0]; 547 struct ev_periodic *w = periodics [0];
435 548
471 } 584 }
472 } 585 }
473} 586}
474 587
475static void 588static void
476time_update () 589time_update (void)
477{ 590{
478 int i; 591 int i;
479 592
480 ev_now = ev_time (); 593 ev_now = ev_time ();
481 594
515int ev_loop_done; 628int ev_loop_done;
516 629
517void ev_loop (int flags) 630void ev_loop (int flags)
518{ 631{
519 double block; 632 double block;
520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
521
522 if (checkcnt)
523 {
524 queue_events ((W *)checks, checkcnt, EV_CHECK);
525 call_pending ();
526 }
527 634
528 do 635 do
529 { 636 {
637 /* queue check watchers (and execute them) */
638 if (preparecnt)
639 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending ();
642 }
643
530 /* update fd-related kernel structures */ 644 /* update fd-related kernel structures */
531 fd_reify (); 645 fd_reify ();
532 646
533 /* calculate blocking time */ 647 /* calculate blocking time */
534 648
535 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 649 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */
536 ev_now = ev_time (); 651 ev_now = ev_time ();
537 652
538 if (flags & EVLOOP_NONBLOCK || idlecnt) 653 if (flags & EVLOOP_NONBLOCK || idlecnt)
539 block = 0.; 654 block = 0.;
540 else 655 else
560 675
561 /* update ev_now, do magic */ 676 /* update ev_now, do magic */
562 time_update (); 677 time_update ();
563 678
564 /* queue pending timers and reschedule them */ 679 /* queue pending timers and reschedule them */
680 timers_reify (); /* relative timers called last */
565 periodics_reify (); /* absolute timers first */ 681 periodics_reify (); /* absolute timers called first */
566 timers_reify (); /* relative timers second */
567 682
568 /* queue idle watchers unless io or timers are pending */ 683 /* queue idle watchers unless io or timers are pending */
569 if (!pendingcnt) 684 if (!pendingcnt)
570 queue_events ((W *)idles, idlecnt, EV_IDLE); 685 queue_events ((W *)idles, idlecnt, EV_IDLE);
571 686
572 /* queue check and possibly idle watchers */ 687 /* queue check watchers, to be executed first */
688 if (checkcnt)
573 queue_events ((W *)checks, checkcnt, EV_CHECK); 689 queue_events ((W *)checks, checkcnt, EV_CHECK);
574 690
575 call_pending (); 691 call_pending ();
576 } 692 }
577 while (!ev_loop_done); 693 while (!ev_loop_done);
578 694
638 754
639 ev_start ((W)w, 1); 755 ev_start ((W)w, 1);
640 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
641 wlist_add ((WL *)&anfds[fd].head, (WL)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
642 758
643 ++fdchangecnt; 759 fd_change (fd);
644 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
645 fdchanges [fdchangecnt - 1] = fd;
646} 760}
647 761
648void 762void
649evio_stop (struct ev_io *w) 763evio_stop (struct ev_io *w)
650{ 764{
653 return; 767 return;
654 768
655 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
656 ev_stop ((W)w); 770 ev_stop ((W)w);
657 771
658 ++fdchangecnt; 772 fd_change (w->fd);
659 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
660 fdchanges [fdchangecnt - 1] = w->fd;
661} 773}
662 774
663void 775void
664evtimer_start (struct ev_timer *w) 776evtimer_start (struct ev_timer *w)
665{ 777{
797 909
798 idles [w->active - 1] = idles [--idlecnt]; 910 idles [w->active - 1] = idles [--idlecnt];
799 ev_stop ((W)w); 911 ev_stop ((W)w);
800} 912}
801 913
914void evprepare_start (struct ev_prepare *w)
915{
916 if (ev_is_active (w))
917 return;
918
919 ev_start ((W)w, ++preparecnt);
920 array_needsize (prepares, preparemax, preparecnt, );
921 prepares [preparecnt - 1] = w;
922}
923
924void evprepare_stop (struct ev_prepare *w)
925{
926 ev_clear ((W)w);
927 if (ev_is_active (w))
928 return;
929
930 prepares [w->active - 1] = prepares [--preparecnt];
931 ev_stop ((W)w);
932}
933
802void evcheck_start (struct ev_check *w) 934void evcheck_start (struct ev_check *w)
803{ 935{
804 if (ev_is_active (w)) 936 if (ev_is_active (w))
805 return; 937 return;
806 938
814 ev_clear ((W)w); 946 ev_clear ((W)w);
815 if (ev_is_active (w)) 947 if (ev_is_active (w))
816 return; 948 return;
817 949
818 checks [w->active - 1] = checks [--checkcnt]; 950 checks [w->active - 1] = checks [--checkcnt];
951 ev_stop ((W)w);
952}
953
954void evchild_start (struct ev_child *w)
955{
956 if (ev_is_active (w))
957 return;
958
959 ev_start ((W)w, 1);
960 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
961}
962
963void evchild_stop (struct ev_child *w)
964{
965 ev_clear ((W)w);
966 if (ev_is_active (w))
967 return;
968
969 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
819 ev_stop ((W)w); 970 ev_stop ((W)w);
820} 971}
821 972
822/*****************************************************************************/ 973/*****************************************************************************/
823 974

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines