pycore_signal.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Define Py_NSIG constant for signal handling.
  2. #ifndef Py_INTERNAL_SIGNAL_H
  3. #define Py_INTERNAL_SIGNAL_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #ifndef Py_BUILD_CORE
  8. # error "this header requires Py_BUILD_CORE define"
  9. #endif
  10. #include "pycore_atomic.h" // _Py_atomic_address
  11. #include <signal.h> // NSIG
  12. #ifdef _SIG_MAXSIG
  13. // gh-91145: On FreeBSD, <signal.h> defines NSIG as 32: it doesn't include
  14. // realtime signals: [SIGRTMIN,SIGRTMAX]. Use _SIG_MAXSIG instead. For
  15. // example on x86-64 FreeBSD 13, SIGRTMAX is 126 and _SIG_MAXSIG is 128.
  16. # define Py_NSIG _SIG_MAXSIG
  17. #elif defined(NSIG)
  18. # define Py_NSIG NSIG
  19. #elif defined(_NSIG)
  20. # define Py_NSIG _NSIG // BSD/SysV
  21. #elif defined(_SIGMAX)
  22. # define Py_NSIG (_SIGMAX + 1) // QNX
  23. #elif defined(SIGMAX)
  24. # define Py_NSIG (SIGMAX + 1) // djgpp
  25. #else
  26. # define Py_NSIG 64 // Use a reasonable default value
  27. #endif
  28. #define INVALID_FD (-1)
  29. struct _signals_runtime_state {
  30. volatile struct {
  31. _Py_atomic_int tripped;
  32. /* func is atomic to ensure that PyErr_SetInterrupt is async-signal-safe
  33. * (even though it would probably be otherwise, anyway).
  34. */
  35. _Py_atomic_address func;
  36. } handlers[Py_NSIG];
  37. volatile struct {
  38. #ifdef MS_WINDOWS
  39. /* This would be "SOCKET fd" if <winsock2.h> were always included.
  40. It isn't so we must cast to SOCKET where appropriate. */
  41. volatile int fd;
  42. #elif defined(__VXWORKS__)
  43. int fd;
  44. #else
  45. sig_atomic_t fd;
  46. #endif
  47. int warn_on_full_buffer;
  48. #ifdef MS_WINDOWS
  49. int use_send;
  50. #endif
  51. } wakeup;
  52. /* Speed up sigcheck() when none tripped */
  53. _Py_atomic_int is_tripped;
  54. /* These objects necessarily belong to the main interpreter. */
  55. PyObject *default_handler;
  56. PyObject *ignore_handler;
  57. #ifdef MS_WINDOWS
  58. /* This would be "HANDLE sigint_event" if <windows.h> were always included.
  59. It isn't so we must cast to HANDLE everywhere "sigint_event" is used. */
  60. void *sigint_event;
  61. #endif
  62. /* True if the main interpreter thread exited due to an unhandled
  63. * KeyboardInterrupt exception, suggesting the user pressed ^C. */
  64. int unhandled_keyboard_interrupt;
  65. };
  66. #ifdef MS_WINDOWS
  67. # define _signals_WAKEUP_INIT \
  68. {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0}
  69. #else
  70. # define _signals_WAKEUP_INIT \
  71. {.fd = INVALID_FD, .warn_on_full_buffer = 1}
  72. #endif
  73. #define _signals_RUNTIME_INIT \
  74. { \
  75. .wakeup = _signals_WAKEUP_INIT, \
  76. }
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif // !Py_INTERNAL_SIGNAL_H