sigset.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* __sig_atomic_t, __sigset_t, and related definitions. Linux version.
  2. Copyright (C) 1991, 1992, 1994, 1996, 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _SIGSET_H_types
  17. # define _SIGSET_H_types 1
  18. typedef int __sig_atomic_t;
  19. /* A 'sigset_t' has a bit for each signal.
  20. * glibc has space for 1024 signals (!), but most arches supported
  21. * by Linux have 64 signals, and only MIPS has 128.
  22. * There seems to be some historical baggage in sparc[64]
  23. * where they might have (or had in the past) 32 signals only,
  24. * I hope it's irrelevant now.
  25. * Signal 0 does not exist, so we have signals 1..64, not 0..63.
  26. * Note that struct sigaction has embedded sigset_t,
  27. * and this necessitates translation in sigaction()
  28. * to convert it to struct kernel_sigaction.
  29. * See libc/.../sigaction.c, libc/.../kernel_sigaction.h
  30. */
  31. #if defined(__mips__)
  32. # define _SIGSET_NWORDS (128 / (8 * sizeof (unsigned long)))
  33. #else
  34. # define _SIGSET_NWORDS (64 / (8 * sizeof (unsigned long)))
  35. #endif
  36. typedef struct {
  37. unsigned long __val[_SIGSET_NWORDS];
  38. } __sigset_t;
  39. #endif
  40. /* We only want to define these functions if <signal.h> was actually
  41. included; otherwise we were included just to define the types. Since we
  42. are namespace-clean, it wouldn't hurt to define extra macros. But
  43. trouble can be caused by functions being defined (e.g., any global
  44. register vars declared later will cause compilation errors). */
  45. #if !defined _SIGSET_H_fns && defined _SIGNAL_H
  46. # define _SIGSET_H_fns 1
  47. # ifndef _EXTERN_INLINE
  48. # define _EXTERN_INLINE extern __inline
  49. # endif
  50. /* Return a mask that includes the bit for SIG only. */
  51. /* Unsigned cast ensures shift/mask insns are used. */
  52. # define __sigmask(sig) \
  53. (((unsigned long) 1) << ((unsigned)((sig) - 1) % (8 * sizeof (unsigned long))))
  54. /* Return the word index for SIG. */
  55. # define __sigword(sig) ((unsigned)((sig) - 1) / (8 * sizeof (unsigned long)))
  56. /* gcc 4.3.1 is not clever enough to optimize for _SIGSET_NWORDS == 1 and 2,
  57. * which are about the only values which can be there */
  58. # if defined __GNUC__ && __GNUC__ >= 2
  59. # define __sigemptyset(set) \
  60. (__extension__ ({ \
  61. sigset_t *__set = (set); \
  62. if (_SIGSET_NWORDS <= 2) { \
  63. __set->__val[0] = 0; \
  64. if (_SIGSET_NWORDS == 2) \
  65. __set->__val[1] = 0; \
  66. } else { \
  67. int __cnt = _SIGSET_NWORDS; \
  68. while (--__cnt >= 0) __set->__val[__cnt] = 0; \
  69. } \
  70. 0; \
  71. }))
  72. # define __sigfillset(set) \
  73. (__extension__ ({ \
  74. sigset_t *__set = (set); \
  75. if (_SIGSET_NWORDS <= 2) { \
  76. __set->__val[0] = ~0UL; \
  77. if (_SIGSET_NWORDS == 2) \
  78. __set->__val[1] = ~0UL; \
  79. } else { \
  80. int __cnt = _SIGSET_NWORDS; \
  81. while (--__cnt >= 0) __set->__val[__cnt] = ~0UL; \
  82. } \
  83. 0; \
  84. }))
  85. # ifdef __USE_GNU
  86. /* The POSIX does not specify for handling the whole signal set in one
  87. command. This is often wanted and so we define three more functions
  88. here. */
  89. # define __sigisemptyset(set) \
  90. (__extension__ ({ \
  91. long __ret; \
  92. const sigset_t *__set = (set); \
  93. if (_SIGSET_NWORDS == 1) { \
  94. __ret = __set->__val[0]; \
  95. } else if (_SIGSET_NWORDS == 2) { \
  96. __ret = __set->__val[0] | __set->__val[1]; \
  97. } else { \
  98. int __cnt = _SIGSET_NWORDS; \
  99. __ret = __set->__val[--__cnt]; \
  100. while (!__ret && --__cnt >= 0) \
  101. __ret = __set->__val[__cnt]; \
  102. } \
  103. __ret == 0; \
  104. }))
  105. # define __sigandset(dest, left, right) \
  106. (__extension__ ({ \
  107. sigset_t *__dest = (dest); \
  108. const sigset_t *__left = (left); \
  109. const sigset_t *__right = (right); \
  110. if (_SIGSET_NWORDS <= 2) { \
  111. __dest->__val[0] = __left->__val[0] & __right->__val[0];\
  112. if (_SIGSET_NWORDS == 2) \
  113. __dest->__val[1] = __left->__val[1] & __right->__val[1];\
  114. } else { \
  115. int __cnt = _SIGSET_NWORDS; \
  116. while (--__cnt >= 0) \
  117. __dest->__val[__cnt] = (__left->__val[__cnt] \
  118. & __right->__val[__cnt]); \
  119. } \
  120. 0; \
  121. }))
  122. # define __sigorset(dest, left, right) \
  123. (__extension__ ({ \
  124. sigset_t *__dest = (dest); \
  125. const sigset_t *__left = (left); \
  126. const sigset_t *__right = (right); \
  127. if (_SIGSET_NWORDS <= 2) { \
  128. __dest->__val[0] = __left->__val[0] | __right->__val[0];\
  129. if (_SIGSET_NWORDS == 2) \
  130. __dest->__val[1] = __left->__val[1] | __right->__val[1];\
  131. } else { \
  132. int __cnt = _SIGSET_NWORDS; \
  133. while (--__cnt >= 0) \
  134. __dest->__val[__cnt] = (__left->__val[__cnt] \
  135. | __right->__val[__cnt]); \
  136. } \
  137. 0; \
  138. }))
  139. # endif
  140. # endif
  141. /* These functions needn't check for a bogus signal number -- error
  142. checking is done in the non __ versions. */
  143. extern int __sigismember (__const __sigset_t *, int);
  144. libc_hidden_proto(__sigismember)
  145. extern int __sigaddset (__sigset_t *, int);
  146. libc_hidden_proto(__sigaddset)
  147. extern int __sigdelset (__sigset_t *, int);
  148. libc_hidden_proto(__sigdelset)
  149. # ifdef __USE_EXTERN_INLINES
  150. # define __SIGSETFN(NAME, BODY, CONST) \
  151. _EXTERN_INLINE int \
  152. NAME (CONST __sigset_t *__set, int __sig) \
  153. { \
  154. unsigned long __mask = __sigmask (__sig); \
  155. unsigned long __word = __sigword (__sig); \
  156. return BODY; \
  157. }
  158. __SIGSETFN (__sigismember, (__set->__val[__word] & __mask) ? 1 : 0, __const)
  159. __SIGSETFN (__sigaddset, ((__set->__val[__word] |= __mask), 0), )
  160. __SIGSETFN (__sigdelset, ((__set->__val[__word] &= ~__mask), 0), )
  161. # undef __SIGSETFN
  162. # endif
  163. #endif /* ! _SIGSET_H_fns. */