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

Comparing libev/ev.c (file contents):
Revision 1.40 by root, Fri Nov 2 11:02:23 2007 UTC vs.
Revision 1.50 by root, Sat Nov 3 19:41:55 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#if EV_USE_CONFIG_H 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33#endif 33#endif
34 34
35#include <math.h> 35#include <math.h>
36#include <stdlib.h> 36#include <stdlib.h>
42#include <stdio.h> 42#include <stdio.h>
43 43
44#include <assert.h> 44#include <assert.h>
45#include <errno.h> 45#include <errno.h>
46#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
47#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
48#include <sys/time.h> 50#include <sys/time.h>
49#include <time.h> 51#include <time.h>
50 52
51/**/ 53/**/
52 54
56 58
57#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
59#endif 61#endif
60 62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
61#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
62# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
69#endif
70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
63#endif 73#endif
64 74
65#ifndef EV_USE_REALTIME 75#ifndef EV_USE_REALTIME
66# define EV_USE_REALTIME 1 76# define EV_USE_REALTIME 1
67#endif 77#endif
95# define inline static 105# define inline static
96#endif 106#endif
97 107
98#define expect_false(expr) expect ((expr) != 0, 0) 108#define expect_false(expr) expect ((expr) != 0, 0)
99#define expect_true(expr) expect ((expr) != 0, 1) 109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
100 113
101typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
102typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
103typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
104 117
162 175
163/*****************************************************************************/ 176/*****************************************************************************/
164 177
165typedef struct 178typedef struct
166{ 179{
167 struct ev_io *head; 180 struct ev_watcher_list *head;
168 unsigned char events; 181 unsigned char events;
169 unsigned char reify; 182 unsigned char reify;
170} ANFD; 183} ANFD;
171 184
172static ANFD *anfds; 185static ANFD *anfds;
189{ 202{
190 W w; 203 W w;
191 int events; 204 int events;
192} ANPENDING; 205} ANPENDING;
193 206
194static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
195static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
196 209
197static void 210static void
198event (W w, int events) 211event (W w, int events)
199{ 212{
200 if (w->pending) 213 if (w->pending)
201 { 214 {
202 pendings [w->pending - 1].events |= events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
203 return; 216 return;
204 } 217 }
205 218
206 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
207 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
208 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
209 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
210} 223}
211 224
212static void 225static void
213queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
214{ 227{
222fd_event (int fd, int events) 235fd_event (int fd, int events)
223{ 236{
224 ANFD *anfd = anfds + fd; 237 ANFD *anfd = anfds + fd;
225 struct ev_io *w; 238 struct ev_io *w;
226 239
227 for (w = anfd->head; w; w = w->next) 240 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
228 { 241 {
229 int ev = w->events & events; 242 int ev = w->events & events;
230 243
231 if (ev) 244 if (ev)
232 event ((W)w, ev); 245 event ((W)w, ev);
249 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
250 struct ev_io *w; 263 struct ev_io *w;
251 264
252 int events = 0; 265 int events = 0;
253 266
254 for (w = anfd->head; w; w = w->next) 267 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
255 events |= w->events; 268 events |= w->events;
256 269
257 anfd->reify = 0; 270 anfd->reify = 0;
258 271
259 if (anfd->events != events) 272 if (anfd->events != events)
277 ++fdchangecnt; 290 ++fdchangecnt;
278 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
279 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
280} 293}
281 294
295static void
296fd_kill (int fd)
297{
298 struct ev_io *w;
299
300 while ((w = (struct ev_io *)anfds [fd].head))
301 {
302 ev_io_stop (w);
303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
304 }
305}
306
282/* called on EBADF to verify fds */ 307/* called on EBADF to verify fds */
283static void 308static void
284fd_recheck (void) 309fd_ebadf (void)
285{ 310{
286 int fd; 311 int fd;
287 312
288 for (fd = 0; fd < anfdmax; ++fd) 313 for (fd = 0; fd < anfdmax; ++fd)
289 if (anfds [fd].events) 314 if (anfds [fd].events)
290 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 315 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
291 while (anfds [fd].head) 316 fd_kill (fd);
317}
318
319/* called on ENOMEM in select/poll to kill some fds and retry */
320static void
321fd_enomem (void)
322{
323 int fd = anfdmax;
324
325 while (fd--)
326 if (anfds [fd].events)
292 { 327 {
293 ev_io_stop (anfds [fd].head); 328 close (fd);
294 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 329 fd_kill (fd);
330 return;
295 } 331 }
296} 332}
297 333
298/*****************************************************************************/ 334/*****************************************************************************/
299 335
300static struct ev_timer **timers; 336static struct ev_timer **timers;
346 382
347/*****************************************************************************/ 383/*****************************************************************************/
348 384
349typedef struct 385typedef struct
350{ 386{
351 struct ev_signal *head; 387 struct ev_watcher_list *head;
352 sig_atomic_t volatile gotsig; 388 sig_atomic_t volatile gotsig;
353} ANSIG; 389} ANSIG;
354 390
355static ANSIG *signals; 391static ANSIG *signals;
356static int signalmax; 392static int signalmax;
376{ 412{
377 signals [signum - 1].gotsig = 1; 413 signals [signum - 1].gotsig = 1;
378 414
379 if (!gotsig) 415 if (!gotsig)
380 { 416 {
417 int old_errno = errno;
381 gotsig = 1; 418 gotsig = 1;
382 write (sigpipe [1], &signum, 1); 419 write (sigpipe [1], &signum, 1);
420 errno = old_errno;
383 } 421 }
384} 422}
385 423
386static void 424static void
387sigcb (struct ev_io *iow, int revents) 425sigcb (struct ev_io *iow, int revents)
388{ 426{
389 struct ev_signal *w; 427 struct ev_watcher_list *w;
390 int signum; 428 int signum;
391 429
392 read (sigpipe [0], &revents, 1); 430 read (sigpipe [0], &revents, 1);
393 gotsig = 0; 431 gotsig = 0;
394 432
403} 441}
404 442
405static void 443static void
406siginit (void) 444siginit (void)
407{ 445{
446#ifndef WIN32
408 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
409 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
410 449
411 /* rather than sort out wether we really need nb, set it */ 450 /* rather than sort out wether we really need nb, set it */
412 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
413 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
453#endif
414 454
415 ev_io_set (&sigev, sigpipe [0], EV_READ); 455 ev_io_set (&sigev, sigpipe [0], EV_READ);
416 ev_io_start (&sigev); 456 ev_io_start (&sigev);
417} 457}
418 458
430/*****************************************************************************/ 470/*****************************************************************************/
431 471
432static struct ev_child *childs [PID_HASHSIZE]; 472static struct ev_child *childs [PID_HASHSIZE];
433static struct ev_signal childev; 473static struct ev_signal childev;
434 474
475#ifndef WIN32
476
435#ifndef WCONTINUED 477#ifndef WCONTINUED
436# define WCONTINUED 0 478# define WCONTINUED 0
437#endif 479#endif
438 480
439static void 481static void
482child_reap (struct ev_signal *sw, int chain, int pid, int status)
483{
484 struct ev_child *w;
485
486 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
487 if (w->pid == pid || !w->pid)
488 {
489 w->priority = sw->priority; /* need to do it *now* */
490 w->rpid = pid;
491 w->rstatus = status;
492 event ((W)w, EV_CHILD);
493 }
494}
495
496static void
440childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
441{ 498{
442 struct ev_child *w;
443 int pid, status; 499 int pid, status;
444 500
445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 502 {
447 if (w->pid == pid || !w->pid) 503 /* make sure we are called again until all childs have been reaped */
448 {
449 w->status = status;
450 event ((W)w, EV_CHILD); 504 event ((W)sw, EV_SIGNAL);
451 } 505
506 child_reap (sw, pid, pid, status);
507 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
508 }
452} 509}
510
511#endif
453 512
454/*****************************************************************************/ 513/*****************************************************************************/
455 514
515#if EV_USE_KQUEUE
516# include "ev_kqueue.c"
517#endif
456#if EV_USE_EPOLL 518#if EV_USE_EPOLL
457# include "ev_epoll.c" 519# include "ev_epoll.c"
458#endif 520#endif
521#if EV_USE_POLL
522# include "ev_poll.c"
523#endif
459#if EV_USE_SELECT 524#if EV_USE_SELECT
460# include "ev_select.c" 525# include "ev_select.c"
461#endif 526#endif
462 527
463int 528int
470ev_version_minor (void) 535ev_version_minor (void)
471{ 536{
472 return EV_VERSION_MINOR; 537 return EV_VERSION_MINOR;
473} 538}
474 539
540/* return true if we are running with elevated privileges and should ignore env variables */
541static int
542enable_secure ()
543{
544#ifdef WIN32
545 return 0;
546#else
547 return getuid () != geteuid ()
548 || getgid () != getegid ();
549#endif
550}
551
475int ev_init (int flags) 552int ev_init (int methods)
476{ 553{
477 if (!ev_method) 554 if (!ev_method)
478 { 555 {
479#if EV_USE_MONOTONIC 556#if EV_USE_MONOTONIC
480 { 557 {
490 diff = ev_now - now; 567 diff = ev_now - now;
491 568
492 if (pipe (sigpipe)) 569 if (pipe (sigpipe))
493 return 0; 570 return 0;
494 571
572 if (methods == EVMETHOD_AUTO)
573 if (!enable_secure () && getenv ("LIBEV_METHODS"))
574 methods = atoi (getenv ("LIBEV_METHODS"));
575 else
495 ev_method = EVMETHOD_NONE; 576 methods = EVMETHOD_ANY;
577
578 ev_method = 0;
579#if EV_USE_KQUEUE
580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
581#endif
496#if EV_USE_EPOLL 582#if EV_USE_EPOLL
497 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 583 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
584#endif
585#if EV_USE_POLL
586 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
498#endif 587#endif
499#if EV_USE_SELECT 588#if EV_USE_SELECT
500 if (ev_method == EVMETHOD_NONE) select_init (flags); 589 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
501#endif 590#endif
502 591
503 if (ev_method) 592 if (ev_method)
504 { 593 {
505 ev_watcher_init (&sigev, sigcb); 594 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI);
506 siginit (); 596 siginit ();
507 597
598#ifndef WIN32
508 ev_signal_init (&childev, childcb, SIGCHLD); 599 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI);
509 ev_signal_start (&childev); 601 ev_signal_start (&childev);
602#endif
510 } 603 }
511 } 604 }
512 605
513 return ev_method; 606 return ev_method;
514} 607}
545/*****************************************************************************/ 638/*****************************************************************************/
546 639
547static void 640static void
548call_pending (void) 641call_pending (void)
549{ 642{
643 int pri;
644
645 for (pri = NUMPRI; pri--; )
550 while (pendingcnt) 646 while (pendingcnt [pri])
551 { 647 {
552 ANPENDING *p = pendings + --pendingcnt; 648 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
553 649
554 if (p->w) 650 if (p->w)
555 { 651 {
556 p->w->pending = 0; 652 p->w->pending = 0;
557 p->w->cb (p->w, p->events); 653 p->w->cb (p->w, p->events);
558 } 654 }
559 } 655 }
560} 656}
561 657
562static void 658static void
563timers_reify (void) 659timers_reify (void)
564{ 660{
796static void 892static void
797ev_clear_pending (W w) 893ev_clear_pending (W w)
798{ 894{
799 if (w->pending) 895 if (w->pending)
800 { 896 {
801 pendings [w->pending - 1].w = 0; 897 pendings [ABSPRI (w)][w->pending - 1].w = 0;
802 w->pending = 0; 898 w->pending = 0;
803 } 899 }
804} 900}
805 901
806static void 902static void
807ev_start (W w, int active) 903ev_start (W w, int active)
808{ 904{
905 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
906 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
907
809 w->active = active; 908 w->active = active;
810} 909}
811 910
812static void 911static void
813ev_stop (W w) 912ev_stop (W w)
930 } 1029 }
931 1030
932 ev_stop ((W)w); 1031 ev_stop ((W)w);
933} 1032}
934 1033
1034#ifndef SA_RESTART
1035# define SA_RESTART 0
1036#endif
1037
935void 1038void
936ev_signal_start (struct ev_signal *w) 1039ev_signal_start (struct ev_signal *w)
937{ 1040{
938 if (ev_is_active (w)) 1041 if (ev_is_active (w))
939 return; 1042 return;
947 if (!w->next) 1050 if (!w->next)
948 { 1051 {
949 struct sigaction sa; 1052 struct sigaction sa;
950 sa.sa_handler = sighandler; 1053 sa.sa_handler = sighandler;
951 sigfillset (&sa.sa_mask); 1054 sigfillset (&sa.sa_mask);
952 sa.sa_flags = 0; 1055 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
953 sigaction (w->signum, &sa, 0); 1056 sigaction (w->signum, &sa, 0);
954 } 1057 }
955} 1058}
956 1059
957void 1060void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines