1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
45 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
46 | #include <sys/time.h> |
48 | #include <sys/time.h> |
47 | #include <time.h> |
49 | #include <time.h> |
48 | |
50 | |
49 | #ifndef EV_USE_MONOTONIC |
51 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
52 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
53 | #endif |
|
|
54 | |
|
|
55 | #ifndef CLOCK_MONOTONIC |
|
|
56 | # undef EV_USE_MONOTONIC |
|
|
57 | # define EV_USE_MONOTONIC 0 |
53 | #endif |
58 | #endif |
54 | |
59 | |
55 | #ifndef EV_USE_SELECT |
60 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
57 | #endif |
62 | #endif |
… | |
… | |
323 | /*****************************************************************************/ |
328 | /*****************************************************************************/ |
324 | |
329 | |
325 | typedef struct |
330 | typedef struct |
326 | { |
331 | { |
327 | struct ev_signal *head; |
332 | struct ev_signal *head; |
328 | sig_atomic_t gotsig; |
333 | sig_atomic_t volatile gotsig; |
329 | } ANSIG; |
334 | } ANSIG; |
330 | |
335 | |
331 | static ANSIG *signals; |
336 | static ANSIG *signals; |
332 | static int signalmax; |
337 | static int signalmax; |
333 | |
338 | |
334 | static int sigpipe [2]; |
339 | static int sigpipe [2]; |
335 | static sig_atomic_t gotsig; |
340 | static sig_atomic_t volatile gotsig; |
336 | static struct ev_io sigev; |
341 | static struct ev_io sigev; |
337 | |
342 | |
338 | static void |
343 | static void |
339 | signals_init (ANSIG *base, int count) |
344 | signals_init (ANSIG *base, int count) |
340 | { |
345 | { |
… | |
… | |
353 | signals [signum - 1].gotsig = 1; |
358 | signals [signum - 1].gotsig = 1; |
354 | |
359 | |
355 | if (!gotsig) |
360 | if (!gotsig) |
356 | { |
361 | { |
357 | gotsig = 1; |
362 | gotsig = 1; |
358 | write (sigpipe [1], &gotsig, 1); |
363 | write (sigpipe [1], &signum, 1); |
359 | } |
364 | } |
360 | } |
365 | } |
361 | |
366 | |
362 | static void |
367 | static void |
363 | sigcb (struct ev_io *iow, int revents) |
368 | sigcb (struct ev_io *iow, int revents) |
364 | { |
369 | { |
365 | struct ev_signal *w; |
370 | struct ev_signal *w; |
366 | int sig; |
371 | int signum; |
367 | |
372 | |
|
|
373 | read (sigpipe [0], &revents, 1); |
368 | gotsig = 0; |
374 | gotsig = 0; |
369 | read (sigpipe [0], &revents, 1); |
|
|
370 | |
375 | |
371 | for (sig = signalmax; sig--; ) |
376 | for (signum = signalmax; signum--; ) |
372 | if (signals [sig].gotsig) |
377 | if (signals [signum].gotsig) |
373 | { |
378 | { |
374 | signals [sig].gotsig = 0; |
379 | signals [signum].gotsig = 0; |
375 | |
380 | |
376 | for (w = signals [sig].head; w; w = w->next) |
381 | for (w = signals [signum].head; w; w = w->next) |
377 | event ((W)w, EV_SIGNAL); |
382 | event ((W)w, EV_SIGNAL); |
378 | } |
383 | } |
379 | } |
384 | } |
380 | |
385 | |
381 | static void |
386 | static void |
… | |
… | |
489 | } |
494 | } |
490 | |
495 | |
491 | /*****************************************************************************/ |
496 | /*****************************************************************************/ |
492 | |
497 | |
493 | void |
498 | void |
494 | ev_prefork (void) |
499 | ev_fork_prepare (void) |
495 | { |
500 | { |
496 | /* nop */ |
501 | /* nop */ |
497 | } |
502 | } |
498 | |
503 | |
499 | void |
504 | void |
500 | ev_postfork_parent (void) |
505 | ev_fork_parent (void) |
501 | { |
506 | { |
502 | /* nop */ |
507 | /* nop */ |
503 | } |
508 | } |
504 | |
509 | |
505 | void |
510 | void |
506 | ev_postfork_child (void) |
511 | ev_fork_child (void) |
507 | { |
512 | { |
508 | #if EV_USE_EPOLL |
513 | #if EV_USE_EPOLL |
509 | if (ev_method == EVMETHOD_EPOLL) |
514 | if (ev_method == EVMETHOD_EPOLL) |
510 | epoll_postfork_child (); |
515 | epoll_postfork_child (); |
511 | #endif |
516 | #endif |
… | |
… | |
761 | /*****************************************************************************/ |
766 | /*****************************************************************************/ |
762 | |
767 | |
763 | void |
768 | void |
764 | ev_io_start (struct ev_io *w) |
769 | ev_io_start (struct ev_io *w) |
765 | { |
770 | { |
766 | if (ev_is_active (w)) |
|
|
767 | return; |
|
|
768 | |
|
|
769 | int fd = w->fd; |
771 | int fd = w->fd; |
|
|
772 | |
|
|
773 | if (ev_is_active (w)) |
|
|
774 | return; |
770 | |
775 | |
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
776 | assert (("ev_io_start called with negative fd", fd >= 0)); |
772 | |
777 | |
773 | ev_start ((W)w, 1); |
778 | ev_start ((W)w, 1); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
779 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |