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