cpu-set.h 5.0 KB

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