cpu-set.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Definition of the cpu_set_t structure used by the POSIX 1003.1b-1993
  2. scheduling interface.
  3. Copyright (C) 1996-2021 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #ifndef _BITS_CPU_SET_H
  17. #define _BITS_CPU_SET_H 1
  18. #ifndef _SCHED_H
  19. # error "Never include <bits/cpu-set.h> directly; use <sched.h> instead."
  20. #endif
  21. /* Size definition for CPU sets. */
  22. #define __CPU_SETSIZE 1024
  23. #define __NCPUBITS (8 * sizeof (__cpu_mask))
  24. /* Type for array elements in 'cpu_set_t'. */
  25. typedef __CPU_MASK_TYPE __cpu_mask;
  26. /* Basic access functions. */
  27. #define __CPUELT(cpu) ((cpu) / __NCPUBITS)
  28. #define __CPUMASK(cpu) ((__cpu_mask) 1 << ((cpu) % __NCPUBITS))
  29. /* Data structure to describe CPU mask. */
  30. typedef struct
  31. {
  32. __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];
  33. } cpu_set_t;
  34. /* Access functions for CPU masks. */
  35. #if __GNUC_PREREQ (2, 91)
  36. # define __CPU_ZERO_S(setsize, cpusetp) \
  37. do __builtin_memset (cpusetp, '\0', setsize); while (0)
  38. #else
  39. # define __CPU_ZERO_S(setsize, cpusetp) \
  40. do { \
  41. size_t __i; \
  42. size_t __imax = (setsize) / sizeof (__cpu_mask); \
  43. __cpu_mask *__bits = (cpusetp)->__bits; \
  44. for (__i = 0; __i < __imax; ++__i) \
  45. __bits[__i] = 0; \
  46. } while (0)
  47. #endif
  48. #define __CPU_SET_S(cpu, setsize, cpusetp) \
  49. (__extension__ \
  50. ({ size_t __cpu = (cpu); \
  51. __cpu / 8 < (setsize) \
  52. ? (((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)] \
  53. |= __CPUMASK (__cpu)) \
  54. : 0; }))
  55. #define __CPU_CLR_S(cpu, setsize, cpusetp) \
  56. (__extension__ \
  57. ({ size_t __cpu = (cpu); \
  58. __cpu / 8 < (setsize) \
  59. ? (((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)] \
  60. &= ~__CPUMASK (__cpu)) \
  61. : 0; }))
  62. #define __CPU_ISSET_S(cpu, setsize, cpusetp) \
  63. (__extension__ \
  64. ({ size_t __cpu = (cpu); \
  65. __cpu / 8 < (setsize) \
  66. ? ((((const __cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)] \
  67. & __CPUMASK (__cpu))) != 0 \
  68. : 0; }))
  69. #define __CPU_COUNT_S(setsize, cpusetp) \
  70. __sched_cpucount (setsize, cpusetp)
  71. #if __GNUC_PREREQ (2, 91)
  72. # define __CPU_EQUAL_S(setsize, cpusetp1, cpusetp2) \
  73. (__builtin_memcmp (cpusetp1, cpusetp2, setsize) == 0)
  74. #else
  75. # define __CPU_EQUAL_S(setsize, cpusetp1, cpusetp2) \
  76. (__extension__ \
  77. ({ const __cpu_mask *__arr1 = (cpusetp1)->__bits; \
  78. const __cpu_mask *__arr2 = (cpusetp2)->__bits; \
  79. size_t __imax = (setsize) / sizeof (__cpu_mask); \
  80. size_t __i; \
  81. for (__i = 0; __i < __imax; ++__i) \
  82. if (__arr1[__i] != __arr2[__i]) \
  83. break; \
  84. __i == __imax; }))
  85. #endif
  86. #define __CPU_OP_S(setsize, destset, srcset1, srcset2, op) \
  87. (__extension__ \
  88. ({ cpu_set_t *__dest = (destset); \
  89. const __cpu_mask *__arr1 = (srcset1)->__bits; \
  90. const __cpu_mask *__arr2 = (srcset2)->__bits; \
  91. size_t __imax = (setsize) / sizeof (__cpu_mask); \
  92. size_t __i; \
  93. for (__i = 0; __i < __imax; ++__i) \
  94. ((__cpu_mask *) __dest->__bits)[__i] = __arr1[__i] op __arr2[__i]; \
  95. __dest; }))
  96. #define __CPU_ALLOC_SIZE(count) \
  97. ((((count) + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask))
  98. #define __CPU_ALLOC(count) __sched_cpualloc (count)
  99. #define __CPU_FREE(cpuset) __sched_cpufree (cpuset)
  100. __BEGIN_DECLS
  101. extern int __sched_cpucount (size_t __setsize, const cpu_set_t *__setp)
  102. __THROW;
  103. extern cpu_set_t *__sched_cpualloc (size_t __count) __THROW __wur;
  104. extern void __sched_cpufree (cpu_set_t *__set) __THROW;
  105. __END_DECLS
  106. #endif /* bits/cpu-set.h */