sigset.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. # ifndef _EXTERN_INLINE
  45. # define _EXTERN_INLINE __extern_inline
  46. # endif
  47. /* Return a mask that includes the bit for SIG only. */
  48. /* Unsigned cast ensures shift/mask insns are used. */
  49. # define __sigmask(sig) \
  50. (((unsigned long) 1) << ((unsigned)((sig) - 1) % (8 * sizeof (unsigned long))))
  51. /* Return the word index for SIG. */
  52. # define __sigword(sig) ((unsigned)((sig) - 1) / (8 * sizeof (unsigned long)))
  53. /* gcc 4.3.1 is not clever enough to optimize for _SIGSET_NWORDS == 1 and 2,
  54. * which are about the only values which can be there */
  55. # if defined __GNUC__ && __GNUC__ >= 2
  56. # define __sigemptyset(set) \
  57. (__extension__ ({ \
  58. sigset_t *__set = (set); \
  59. if (_SIGSET_NWORDS <= 2) { \
  60. __set->__val[0] = 0; \
  61. if (_SIGSET_NWORDS == 2) \
  62. __set->__val[1] = 0; \
  63. } else { \
  64. int __cnt = _SIGSET_NWORDS; \
  65. while (--__cnt >= 0) __set->__val[__cnt] = 0; \
  66. } \
  67. 0; \
  68. }))
  69. # define __sigfillset(set) \
  70. (__extension__ ({ \
  71. sigset_t *__set = (set); \
  72. if (_SIGSET_NWORDS <= 2) { \
  73. __set->__val[0] = ~0UL; \
  74. if (_SIGSET_NWORDS == 2) \
  75. __set->__val[1] = ~0UL; \
  76. } else { \
  77. int __cnt = _SIGSET_NWORDS; \
  78. while (--__cnt >= 0) __set->__val[__cnt] = ~0UL; \
  79. } \
  80. 0; \
  81. }))
  82. # ifdef __USE_GNU
  83. /* The POSIX does not specify for handling the whole signal set in one
  84. command. This is often wanted and so we define three more functions
  85. here. */
  86. # define __sigisemptyset(set) \
  87. (__extension__ ({ \
  88. long __ret; \
  89. const sigset_t *__set = (set); \
  90. if (_SIGSET_NWORDS == 1) { \
  91. __ret = __set->__val[0]; \
  92. } else if (_SIGSET_NWORDS == 2) { \
  93. __ret = __set->__val[0] | __set->__val[1]; \
  94. } else { \
  95. int __cnt = _SIGSET_NWORDS; \
  96. __ret = __set->__val[--__cnt]; \
  97. while (!__ret && --__cnt >= 0) \
  98. __ret = __set->__val[__cnt]; \
  99. } \
  100. __ret == 0; \
  101. }))
  102. # define __sigandset(dest, left, right) \
  103. (__extension__ ({ \
  104. sigset_t *__dest = (dest); \
  105. const sigset_t *__left = (left); \
  106. const sigset_t *__right = (right); \
  107. if (_SIGSET_NWORDS <= 2) { \
  108. __dest->__val[0] = __left->__val[0] & __right->__val[0];\
  109. if (_SIGSET_NWORDS == 2) \
  110. __dest->__val[1] = __left->__val[1] & __right->__val[1];\
  111. } else { \
  112. int __cnt = _SIGSET_NWORDS; \
  113. while (--__cnt >= 0) \
  114. __dest->__val[__cnt] = (__left->__val[__cnt] \
  115. & __right->__val[__cnt]); \
  116. } \
  117. 0; \
  118. }))
  119. # define __sigorset(dest, left, right) \
  120. (__extension__ ({ \
  121. sigset_t *__dest = (dest); \
  122. const sigset_t *__left = (left); \
  123. const sigset_t *__right = (right); \
  124. if (_SIGSET_NWORDS <= 2) { \
  125. __dest->__val[0] = __left->__val[0] | __right->__val[0];\
  126. if (_SIGSET_NWORDS == 2) \
  127. __dest->__val[1] = __left->__val[1] | __right->__val[1];\
  128. } else { \
  129. int __cnt = _SIGSET_NWORDS; \
  130. while (--__cnt >= 0) \
  131. __dest->__val[__cnt] = (__left->__val[__cnt] \
  132. | __right->__val[__cnt]); \
  133. } \
  134. 0; \
  135. }))
  136. # endif
  137. # endif
  138. /* These functions needn't check for a bogus signal number -- error
  139. checking is done in the non __ versions. */
  140. extern int __sigismember (const __sigset_t *, int);
  141. libc_hidden_proto(__sigismember)
  142. extern void __sigaddset (__sigset_t *, int);
  143. libc_hidden_proto(__sigaddset)
  144. extern void __sigdelset (__sigset_t *, int);
  145. libc_hidden_proto(__sigdelset)
  146. # ifdef __USE_EXTERN_INLINES
  147. # define __SIGSETFN(RET_TYPE, NAME, BODY, CONST) \
  148. _EXTERN_INLINE RET_TYPE \
  149. NAME (CONST __sigset_t *__set, int __sig) \
  150. { \
  151. unsigned long __mask = __sigmask (__sig); \
  152. unsigned __word = __sigword (__sig); \
  153. BODY; \
  154. }
  155. __SIGSETFN (int, __sigismember, return (__set->__val[__word] & __mask) ? 1 : 0,
  156. const)
  157. __SIGSETFN (void, __sigaddset, (__set->__val[__word] |= __mask), )
  158. __SIGSETFN (void, __sigdelset, (__set->__val[__word] &= ~__mask), )
  159. # undef __SIGSETFN
  160. # endif
  161. # ifdef _LIBC
  162. /* It's far too much PITA to __USE_EXTERN_INLINES from within libc.
  163. * Especially since we want to inline only calls with const sig,
  164. * but __USE_EXTERN_INLINES will inline all calls!
  165. */
  166. static __always_inline unsigned long
  167. const_sigismember(const __sigset_t *set, int sig)
  168. {
  169. unsigned long mask = __sigmask(sig);
  170. unsigned word = __sigword(sig);
  171. return (set->__val[word] & mask);
  172. }
  173. # define __sigismember(set, sig) \
  174. (__builtin_constant_p(sig) ? (const_sigismember(set, sig) != 0) : __sigismember(set, sig))
  175. static __always_inline void
  176. const_sigaddset(__sigset_t *set, int sig)
  177. {
  178. unsigned long mask = __sigmask(sig);
  179. unsigned word = __sigword(sig);
  180. set->__val[word] |= mask;
  181. }
  182. # define __sigaddset(set, sig) \
  183. (__builtin_constant_p(sig) ? const_sigaddset(set, sig) : __sigaddset(set, sig))
  184. static __always_inline void
  185. const_sigdelset(__sigset_t *set, int sig)
  186. {
  187. unsigned long mask = __sigmask(sig);
  188. unsigned word = __sigword(sig);
  189. set->__val[word] &= ~mask;
  190. }
  191. # define __sigdelset(set, sig) \
  192. (__builtin_constant_p(sig) ? const_sigdelset(set, sig) : __sigdelset(set, sig))
  193. # endif
  194. #endif /* ! _SIGSET_H_fns. */