sigset.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _SIGSET_H_types
  16. # define _SIGSET_H_types 1
  17. typedef int __sig_atomic_t;
  18. /* A 'sigset_t' has a bit for each signal.
  19. * glibc has space for 1024 signals (!), but most arches supported
  20. * by Linux have 64 signals, and only MIPS has 128.
  21. * There seems to be some historical baggage in sparc[64]
  22. * where they might have (or had in the past) 32 signals only,
  23. * I hope it's irrelevant now.
  24. * Signal 0 does not exist, so we have signals 1..64, not 0..63.
  25. * In uclibc, kernel and userspace sigset_t is always the same.
  26. * BTW, struct sigaction is also the same on kernel and userspace side.
  27. */
  28. #if defined(__mips__)
  29. # define _SIGSET_NWORDS (128 / (8 * sizeof (unsigned long)))
  30. #else
  31. # define _SIGSET_NWORDS (64 / (8 * sizeof (unsigned long)))
  32. #endif
  33. typedef struct {
  34. unsigned long __val[_SIGSET_NWORDS];
  35. } __sigset_t;
  36. #endif
  37. /* We only want to define these functions if <signal.h> was actually
  38. included; otherwise we were included just to define the types. Since we
  39. are namespace-clean, it wouldn't hurt to define extra macros. But
  40. trouble can be caused by functions being defined (e.g., any global
  41. register vars declared later will cause compilation errors). */
  42. #if !defined _SIGSET_H_fns && defined _SIGNAL_H
  43. # define _SIGSET_H_fns 1
  44. /* Return a mask that includes the bit for SIG only. */
  45. /* Unsigned cast ensures shift/mask insns are used. */
  46. # define __sigmask(sig) \
  47. (((unsigned long) 1) << ((unsigned)((sig) - 1) % (8 * sizeof (unsigned long))))
  48. /* Return the word index for SIG. */
  49. # define __sigword(sig) ((unsigned)((sig) - 1) / (8 * sizeof (unsigned long)))
  50. /* gcc 4.3.1 is not clever enough to optimize for _SIGSET_NWORDS == 1 and 2,
  51. * which are about the only values which can be there */
  52. # if defined __GNUC__ && __GNUC__ >= 2
  53. # define __sigemptyset(set) \
  54. (__extension__ ({ \
  55. sigset_t *__set = (set); \
  56. if (_SIGSET_NWORDS <= 2) { \
  57. __set->__val[0] = 0; \
  58. if (_SIGSET_NWORDS == 2) \
  59. __set->__val[1] = 0; \
  60. } else { \
  61. int __cnt = _SIGSET_NWORDS; \
  62. while (--__cnt >= 0) __set->__val[__cnt] = 0; \
  63. } \
  64. 0; \
  65. }))
  66. # define __sigfillset(set) \
  67. (__extension__ ({ \
  68. sigset_t *__set = (set); \
  69. if (_SIGSET_NWORDS <= 2) { \
  70. __set->__val[0] = ~0UL; \
  71. if (_SIGSET_NWORDS == 2) \
  72. __set->__val[1] = ~0UL; \
  73. } else { \
  74. int __cnt = _SIGSET_NWORDS; \
  75. while (--__cnt >= 0) __set->__val[__cnt] = ~0UL; \
  76. } \
  77. 0; \
  78. }))
  79. # ifdef __USE_GNU
  80. /* The POSIX does not specify for handling the whole signal set in one
  81. command. This is often wanted and so we define three more functions
  82. here. */
  83. # define __sigisemptyset(set) \
  84. (__extension__ ({ \
  85. long __ret; \
  86. const sigset_t *__set = (set); \
  87. if (_SIGSET_NWORDS == 1) { \
  88. __ret = __set->__val[0]; \
  89. } else if (_SIGSET_NWORDS == 2) { \
  90. __ret = __set->__val[0] | __set->__val[1]; \
  91. } else { \
  92. int __cnt = _SIGSET_NWORDS; \
  93. __ret = __set->__val[--__cnt]; \
  94. while (!__ret && --__cnt >= 0) \
  95. __ret = __set->__val[__cnt]; \
  96. } \
  97. __ret == 0; \
  98. }))
  99. # define __sigandset(dest, left, right) \
  100. (__extension__ ({ \
  101. sigset_t *__dest = (dest); \
  102. const sigset_t *__left = (left); \
  103. const sigset_t *__right = (right); \
  104. if (_SIGSET_NWORDS <= 2) { \
  105. __dest->__val[0] = __left->__val[0] & __right->__val[0];\
  106. if (_SIGSET_NWORDS == 2) \
  107. __dest->__val[1] = __left->__val[1] & __right->__val[1];\
  108. } else { \
  109. int __cnt = _SIGSET_NWORDS; \
  110. while (--__cnt >= 0) \
  111. __dest->__val[__cnt] = (__left->__val[__cnt] \
  112. & __right->__val[__cnt]); \
  113. } \
  114. 0; \
  115. }))
  116. # define __sigorset(dest, left, right) \
  117. (__extension__ ({ \
  118. sigset_t *__dest = (dest); \
  119. const sigset_t *__left = (left); \
  120. const sigset_t *__right = (right); \
  121. if (_SIGSET_NWORDS <= 2) { \
  122. __dest->__val[0] = __left->__val[0] | __right->__val[0];\
  123. if (_SIGSET_NWORDS == 2) \
  124. __dest->__val[1] = __left->__val[1] | __right->__val[1];\
  125. } else { \
  126. int __cnt = _SIGSET_NWORDS; \
  127. while (--__cnt >= 0) \
  128. __dest->__val[__cnt] = (__left->__val[__cnt] \
  129. | __right->__val[__cnt]); \
  130. } \
  131. 0; \
  132. }))
  133. # endif
  134. # endif
  135. /* These functions needn't check for a bogus signal number -- error
  136. checking is done in the non __ versions. */
  137. # if !defined __USE_EXTERN_INLINES || defined __PROVIDE_OUT_OF_LINE_SIGSETFN
  138. extern int __sigismember (const __sigset_t *, int);
  139. libc_hidden_proto(__sigismember)
  140. extern int __sigaddset (__sigset_t *, int);
  141. libc_hidden_proto(__sigaddset)
  142. extern int __sigdelset (__sigset_t *, int);
  143. libc_hidden_proto(__sigdelset)
  144. # endif
  145. # ifdef __USE_EXTERN_INLINES
  146. # undef _EXTERN_INLINE
  147. # ifdef __PROVIDE_OUT_OF_LINE_SIGSETFN
  148. # define _EXTERN_INLINE
  149. # else /* normal case */
  150. /* dropped extern below: otherwise every module with __USE_EXTERN_INLINES
  151. * will have its own copy of out-of line function emitted. */
  152. # define _EXTERN_INLINE /*extern*/ __always_inline
  153. # endif
  154. # define __SIGSETFN(NAME, BODY, CONST) \
  155. _EXTERN_INLINE int \
  156. NAME (CONST __sigset_t *__set, int __sig) \
  157. { \
  158. unsigned long __mask = __sigmask (__sig); \
  159. unsigned __word = __sigword (__sig); \
  160. return BODY; \
  161. }
  162. __SIGSETFN (__sigismember, (__set->__val[__word] & __mask) ? 1 : 0, const)
  163. __SIGSETFN (__sigaddset, ((__set->__val[__word] |= __mask), 0), )
  164. __SIGSETFN (__sigdelset, ((__set->__val[__word] &= ~__mask), 0), )
  165. # undef __SIGSETFN
  166. # endif
  167. # ifdef _LIBC
  168. /* It's far too much PITA to __USE_EXTERN_INLINES from within libc.
  169. * Especially since we want to inline only calls with const sig,
  170. * but __USE_EXTERN_INLINES will inline all calls!
  171. */
  172. static __always_inline unsigned long
  173. const_sigismember(const __sigset_t *set, int sig)
  174. {
  175. unsigned long mask = __sigmask(sig);
  176. unsigned word = __sigword(sig);
  177. return (set->__val[word] & mask);
  178. }
  179. # define __sigismember(set, sig) \
  180. (__builtin_constant_p(sig) ? (const_sigismember(set, sig) != 0) : __sigismember(set, sig))
  181. static __always_inline void
  182. const_sigaddset(__sigset_t *set, int sig)
  183. {
  184. unsigned long mask = __sigmask(sig);
  185. unsigned word = __sigword(sig);
  186. set->__val[word] |= mask;
  187. }
  188. # define __sigaddset(set, sig) \
  189. (__builtin_constant_p(sig) ? (const_sigaddset(set, sig), 0) : __sigaddset(set, sig))
  190. static __always_inline void
  191. const_sigdelset(__sigset_t *set, int sig)
  192. {
  193. unsigned long mask = __sigmask(sig);
  194. unsigned word = __sigword(sig);
  195. set->__val[word] &= ~mask;
  196. }
  197. # define __sigdelset(set, sig) \
  198. (__builtin_constant_p(sig) ? (const_sigdelset(set, sig), 0) : __sigdelset(set, sig))
  199. # endif
  200. #endif /* ! _SIGSET_H_fns. */