sigset.h 7.4 KB

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