libev.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #if defined(LIBEV_EMBED)
  2. #include "ev.c"
  3. #else
  4. #include "ev.h"
  5. #ifndef _WIN32
  6. #include <signal.h>
  7. #endif
  8. #endif
  9. #ifndef _WIN32
  10. static struct sigaction libev_sigchld;
  11. /*
  12. * Track the state of whether we have installed
  13. * the libev sigchld handler specifically.
  14. * If it's non-zero, libev_sigchld will be valid and set to the action
  15. * that libev needs to do.
  16. * If it's 1, we need to install libev_sigchld to make libev
  17. * child handlers work (on request).
  18. */
  19. static int sigchld_state = 0;
  20. static struct ev_loop* gevent_ev_default_loop(unsigned int flags)
  21. {
  22. struct ev_loop* result;
  23. struct sigaction tmp;
  24. if (sigchld_state)
  25. return ev_default_loop(flags);
  26. // Request the old SIGCHLD handler
  27. sigaction(SIGCHLD, NULL, &tmp);
  28. // Get the loop, which will install a SIGCHLD handler
  29. result = ev_default_loop(flags);
  30. // XXX what if SIGCHLD received there?
  31. // Now restore the previous SIGCHLD handler
  32. sigaction(SIGCHLD, &tmp, &libev_sigchld);
  33. sigchld_state = 1;
  34. return result;
  35. }
  36. static void gevent_install_sigchld_handler(void) {
  37. if (sigchld_state == 1) {
  38. sigaction(SIGCHLD, &libev_sigchld, NULL);
  39. sigchld_state = 2;
  40. }
  41. }
  42. static void gevent_reset_sigchld_handler(void) {
  43. // We could have any state at this point, depending on
  44. // whether the default loop has been used. If it has,
  45. // then always be in state 1 ("need to install)
  46. if (sigchld_state) {
  47. sigchld_state = 1;
  48. }
  49. }
  50. #else
  51. #define gevent_ev_default_loop ev_default_loop
  52. static void gevent_install_sigchld_handler(void) { }
  53. #endif