blob: 7a2dcade8b826d96089e41013d030f27b27dcd1a [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughes2950f132017-08-01 23:02:48 -070028
Elliott Hughes31fbc142021-02-19 14:32:41 -080029#pragma once
30
31/**
32 * @file sched.h
33 * @brief Thread execution scheduling.
34 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Elliott Hughes414dd2d2024-10-16 14:48:30 +000036#include <sys/cdefs.h>
37
Elliott Hughes5704c422016-01-25 18:06:24 -080038#include <bits/timespec.h>
Elliott Hughes887e1142014-01-02 12:05:50 -080039#include <linux/sched.h>
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041__BEGIN_DECLS
42
Elliott Hughes31fbc142021-02-19 14:32:41 -080043/*
44 * @def SCHED_NORMAL
45 * The standard (as opposed to real-time) round-robin scheduling policy.
46 *
47 * (Linux's name for POSIX's SCHED_OTHER.)
48 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000049 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080050 */
51
52/*
53 * @def SCHED_FIFO
54 * The real-time first-in/first-out scheduling policy.
55 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000056 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080057 */
58
59/*
60 * @def SCHED_RR
61 * The real-time round-robin policy. (See also SCHED_NORMAL/SCHED_OTHER.)
62 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000063 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080064 */
65
66/*
67 * @def SCHED_BATCH
68 * The batch scheduling policy.
69 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000070 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080071 */
72
73/*
74 * @def SCHED_IDLE
75 * The low priority "only when otherwise idle" scheduling priority.
76 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000077 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080078 */
79
80/*
81 * @def SCHED_DEADLINE
82 * The deadline scheduling policy.
83 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000084 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080085 */
86
87/*
88 * The standard (as opposed to real-time) round-robin scheduling policy.
89 *
90 * (POSIX's name for Linux's SCHED_NORMAL.)
91 */
Elliott Hughes887e1142014-01-02 12:05:50 -080092#define SCHED_OTHER SCHED_NORMAL
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes31fbc142021-02-19 14:32:41 -080094/**
95 * See sched_getparam()/sched_setparam() and
96 * sched_getscheduler()/sched_setscheduler().
97 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098struct sched_param {
Dan Albertf6640342014-11-21 10:22:09 -080099 int sched_priority;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100};
101
Elliott Hughes31fbc142021-02-19 14:32:41 -0800102/**
Florian Mayer7d098bb2024-04-16 21:12:40 +0000103 * [sched_setscheduler(2)](https://man7.org/linux/man-pages/man2/sched_setscheduler.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800104 * sets the scheduling policy and associated parameters for the given thread.
105 *
106 * Returns 0 on success and returns -1 and sets `errno` on failure.
107 */
zijunzhao21463032023-03-18 00:38:14 +0000108int sched_setscheduler(pid_t __pid, int __policy, const struct sched_param* _Nonnull __param);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800109
110/**
Florian Mayer7d098bb2024-04-16 21:12:40 +0000111 * [sched_getscheduler(2)](https://man7.org/linux/man-pages/man2/sched_getscheduler.2)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800112 * gets the scheduling policy for the given thread.
113 *
114 * Returns a non-negative thread policy on success and returns -1 and sets
115 * `errno` on failure.
116 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700117int sched_getscheduler(pid_t __pid);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800118
119/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000120 * [sched_yield(2)](https://man7.org/linux/man-pages/man2/sched_yield.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800121 * voluntarily gives up using the CPU so that another thread can run.
122 *
123 * Returns 0 on success and returns -1 and sets `errno` on failure.
124 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700125int sched_yield(void);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800126
127/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000128 * [sched_get_priority_max(2)](https://man7.org/linux/man-pages/man2/sched_get_priority_max.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800129 * gets the maximum priority value allowed for the given scheduling policy.
130 *
131 * Returns a priority on success and returns -1 and sets `errno` on failure.
132 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700133int sched_get_priority_max(int __policy);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800134
135/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000136 * [sched_get_priority_min(2)](https://man7.org/linux/man-pages/man2/sched_get_priority_min.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800137 * gets the minimum priority value allowed for the given scheduling policy.
138 *
139 * Returns a priority on success and returns -1 and sets `errno` on failure.
140 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700141int sched_get_priority_min(int __policy);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800142
143/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000144 * [sched_setparam(2)](https://man7.org/linux/man-pages/man2/sched_setparam.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800145 * sets the scheduling parameters for the given thread.
146 *
147 * Returns 0 on success and returns -1 and sets `errno` on failure.
148 */
zijunzhao21463032023-03-18 00:38:14 +0000149int sched_setparam(pid_t __pid, const struct sched_param* _Nonnull __param);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800150
151/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000152 * [sched_getparam(2)](https://man7.org/linux/man-pages/man2/sched_getparam.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800153 * gets the scheduling parameters for the given thread.
154 *
155 * Returns 0 on success and returns -1 and sets `errno` on failure.
156 */
zijunzhao21463032023-03-18 00:38:14 +0000157int sched_getparam(pid_t __pid, struct sched_param* _Nonnull __param);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800158
159/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000160 * [sched_rr_get_interval(2)](https://man7.org/linux/man-pages/man2/sched_rr_get_interval.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800161 * queries the round-robin time quantum for the given thread.
162 *
163 * Returns 0 on success and returns -1 and sets `errno` on failure.
164 */
zijunzhao21463032023-03-18 00:38:14 +0000165int sched_rr_get_interval(pid_t __pid, struct timespec* _Nonnull __quantum);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700167#if defined(__USE_GNU)
Elliott Hughes887e1142014-01-02 12:05:50 -0800168
Elliott Hughes31fbc142021-02-19 14:32:41 -0800169/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000170 * [clone(2)](https://man7.org/linux/man-pages/man2/clone.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800171 * creates a new child process.
172 *
173 * Returns the pid of the child to the caller on success and
174 * returns -1 and sets `errno` on failure.
175 */
Elliott Hughesdb36e082023-05-26 12:58:14 -0700176int clone(int (* __BIONIC_COMPLICATED_NULLNESS __fn)(void* __BIONIC_COMPLICATED_NULLNESS ), void* __BIONIC_COMPLICATED_NULLNESS __child_stack, int __flags, void* _Nullable __arg, ...);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800177
178/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000179 * [unshare(2)](https://man7.org/linux/man-pages/man2/unshare.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800180 * disassociates part of the caller's execution context.
181 *
182 * Returns 0 on success and returns -1 and sets `errno` on failure.
Elliott Hughes31fbc142021-02-19 14:32:41 -0800183 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700184int unshare(int __flags);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800185
186/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000187 * [setns(2)](https://man7.org/linux/man-pages/man2/setns.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800188 * reassociates a thread with a different namespace.
189 *
190 * Returns 0 on success and returns -1 and sets `errno` on failure.
Elliott Hughes31fbc142021-02-19 14:32:41 -0800191 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700192int setns(int __fd, int __ns_type);
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100193
Elliott Hughes31fbc142021-02-19 14:32:41 -0800194/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000195 * [sched_getcpu(3)](https://man7.org/linux/man-pages/man3/sched_getcpu.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800196 * reports which CPU the caller is running on.
197 *
198 * Returns a non-negative CPU number on success and returns -1 and sets
199 * `errno` on failure.
200 */
201int sched_getcpu(void);
202
Calin Juravledd096992014-05-13 16:01:43 +0100203#ifdef __LP64__
Calin Juravleb7437902014-04-29 20:25:26 +0100204#define CPU_SETSIZE 1024
Calin Juravledd096992014-05-13 16:01:43 +0100205#else
206#define CPU_SETSIZE 32
Calin Juravleb7437902014-04-29 20:25:26 +0100207#endif
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100208
Calin Juravleb7437902014-04-29 20:25:26 +0100209#define __CPU_BITTYPE unsigned long int /* mandated by the kernel */
210#define __CPU_BITS (8 * sizeof(__CPU_BITTYPE))
211#define __CPU_ELT(x) ((x) / __CPU_BITS)
212#define __CPU_MASK(x) ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1)))
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100213
Elliott Hughes31fbc142021-02-19 14:32:41 -0800214/**
215 * [cpu_set_t](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) is a
216 * statically-sized CPU set. See `CPU_ALLOC` for dynamically-sized CPU sets.
217 */
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100218typedef struct {
Calin Juravleb7437902014-04-29 20:25:26 +0100219 __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ];
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100220} cpu_set_t;
221
Elliott Hughes31fbc142021-02-19 14:32:41 -0800222/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000223 * [sched_setaffinity(2)](https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800224 * sets the CPU affinity mask for the given thread.
225 *
226 * Returns 0 on success and returns -1 and sets `errno` on failure.
227 */
zijunzhao21463032023-03-18 00:38:14 +0000228int sched_setaffinity(pid_t __pid, size_t __set_size, const cpu_set_t* _Nonnull __set);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800229
230/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000231 * [sched_getaffinity(2)](https://man7.org/linux/man-pages/man2/sched_getaffinity.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800232 * gets the CPU affinity mask for the given thread.
233 *
234 * Returns 0 on success and returns -1 and sets `errno` on failure.
235 */
zijunzhao21463032023-03-18 00:38:14 +0000236int sched_getaffinity(pid_t __pid, size_t __set_size, cpu_set_t* _Nonnull __set);
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100237
Elliott Hughes31fbc142021-02-19 14:32:41 -0800238/**
239 * [CPU_ZERO](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears all
240 * bits in a static CPU set.
241 */
Calin Juravleb7437902014-04-29 20:25:26 +0100242#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800243/**
244 * [CPU_ZERO_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears all
245 * bits in a dynamic CPU set allocated by `CPU_ALLOC`.
246 */
Calin Juravleb7437902014-04-29 20:25:26 +0100247#define CPU_ZERO_S(setsize, set) __builtin_memset(set, 0, setsize)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100248
Elliott Hughes31fbc142021-02-19 14:32:41 -0800249/**
250 * [CPU_SET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one
251 * bit in a static CPU set.
252 */
253#define CPU_SET(cpu, set) CPU_SET_S(cpu, sizeof(cpu_set_t), set)
254/**
255 * [CPU_SET_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one
256 * bit in a dynamic CPU set allocated by `CPU_ALLOC`.
257 */
Calin Juravleb7437902014-04-29 20:25:26 +0100258#define CPU_SET_S(cpu, setsize, set) \
259 do { \
260 size_t __cpu = (cpu); \
261 if (__cpu < 8 * (setsize)) \
262 (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
263 } while (0)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100264
Elliott Hughes31fbc142021-02-19 14:32:41 -0800265/**
266 * [CPU_CLR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears one
267 * bit in a static CPU set.
268 */
269#define CPU_CLR(cpu, set) CPU_CLR_S(cpu, sizeof(cpu_set_t), set)
270/**
271 * [CPU_CLR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears one
272 * bit in a dynamic CPU set allocated by `CPU_ALLOC`.
273 */
Calin Juravleb7437902014-04-29 20:25:26 +0100274#define CPU_CLR_S(cpu, setsize, set) \
275 do { \
276 size_t __cpu = (cpu); \
277 if (__cpu < 8 * (setsize)) \
278 (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
279 } while (0)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100280
Elliott Hughes31fbc142021-02-19 14:32:41 -0800281/**
282 * [CPU_ISSET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
283 * whether the given bit is set in a static CPU set.
284 */
285#define CPU_ISSET(cpu, set) CPU_ISSET_S(cpu, sizeof(cpu_set_t), set)
286/**
287 * [CPU_ISSET_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
288 * whether the given bit is set in a dynamic CPU set allocated by `CPU_ALLOC`.
289 */
Calin Juravleb7437902014-04-29 20:25:26 +0100290#define CPU_ISSET_S(cpu, setsize, set) \
291 (__extension__ ({ \
292 size_t __cpu = (cpu); \
293 (__cpu < 8 * (setsize)) \
294 ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
295 : 0; \
296 }))
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100297
Elliott Hughes31fbc142021-02-19 14:32:41 -0800298/**
299 * [CPU_COUNT](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) counts
300 * how many bits are set in a static CPU set.
301 */
302#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
303/**
304 * [CPU_COUNT_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) counts
305 * how many bits are set in a dynamic CPU set allocated by `CPU_ALLOC`.
306 */
307#define CPU_COUNT_S(setsize, set) __sched_cpucount((setsize), (set))
zijunzhao21463032023-03-18 00:38:14 +0000308int __sched_cpucount(size_t __set_size, const cpu_set_t* _Nonnull __set);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800309
310/**
311 * [CPU_EQUAL](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
312 * whether two static CPU sets have the same bits set and cleared as each other.
313 */
314#define CPU_EQUAL(set1, set2) CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2)
315/**
316 * [CPU_EQUAL_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
317 * whether two dynamic CPU sets allocated by `CPU_ALLOC` have the same bits
318 * set and cleared as each other.
319 */
Calin Juravleb7437902014-04-29 20:25:26 +0100320#define CPU_EQUAL_S(setsize, set1, set2) (__builtin_memcmp(set1, set2, setsize) == 0)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100321
Elliott Hughes31fbc142021-02-19 14:32:41 -0800322/**
323 * [CPU_AND](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ands two
324 * static CPU sets.
325 */
326#define CPU_AND(dst, set1, set2) __CPU_OP(dst, set1, set2, &)
327/**
328 * [CPU_AND_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ands two
329 * dynamic CPU sets allocated by `CPU_ALLOC`.
330 */
Calin Juravleb7437902014-04-29 20:25:26 +0100331#define CPU_AND_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, &)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800332
333/**
334 * [CPU_OR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ors two
335 * static CPU sets.
336 */
337#define CPU_OR(dst, set1, set2) __CPU_OP(dst, set1, set2, |)
338/**
339 * [CPU_OR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ors two
340 * dynamic CPU sets allocated by `CPU_ALLOC`.
341 */
Calin Juravleb7437902014-04-29 20:25:26 +0100342#define CPU_OR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, |)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800343
344/**
345 * [CPU_XOR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
346 * exclusive-ors two static CPU sets.
347 */
348#define CPU_XOR(dst, set1, set2) __CPU_OP(dst, set1, set2, ^)
349/**
350 * [CPU_XOR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
351 * exclusive-ors two dynamic CPU sets allocated by `CPU_ALLOC`.
352 */
Calin Juravleb7437902014-04-29 20:25:26 +0100353#define CPU_XOR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, ^)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100354
Elliott Hughes31fbc142021-02-19 14:32:41 -0800355#define __CPU_OP(dst, set1, set2, op) __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op)
356
Calin Juravleb7437902014-04-29 20:25:26 +0100357#define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \
358 do { \
359 cpu_set_t* __dst = (dstset); \
360 const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
361 const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
362 size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \
363 for (; __nn < __nn_max; __nn++) \
364 (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
365 } while (0)
366
Elliott Hughes31fbc142021-02-19 14:32:41 -0800367/**
368 * [CPU_ALLOC_SIZE](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
369 * returns the size of a CPU set large enough for CPUs in the range 0..count-1.
370 */
371#define CPU_ALLOC_SIZE(count) \
372 __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100373
Elliott Hughes31fbc142021-02-19 14:32:41 -0800374/**
375 * [CPU_ALLOC](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
376 * allocates a CPU set large enough for CPUs in the range 0..count-1.
377 */
378#define CPU_ALLOC(count) __sched_cpualloc((count))
zijunzhao21463032023-03-18 00:38:14 +0000379cpu_set_t* _Nullable __sched_cpualloc(size_t __count);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800380
381/**
382 * [CPU_FREE](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
383 * deallocates a CPU set allocated by `CPU_ALLOC`.
384 */
385#define CPU_FREE(set) __sched_cpufree((set))
zijunzhao21463032023-03-18 00:38:14 +0000386void __sched_cpufree(cpu_set_t* _Nonnull __set);
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100387
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700388#endif /* __USE_GNU */
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100389
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390__END_DECLS