blob: c68ebf0bb9b63af78610a46f82b6ba73703fdd4c [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>
Elliott Hughesf4420fb2025-01-30 11:21:26 -080040#include <linux/sched/types.h>
Elliott Hughes887e1142014-01-02 12:05:50 -080041
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042__BEGIN_DECLS
43
Elliott Hughes31fbc142021-02-19 14:32:41 -080044/*
45 * @def SCHED_NORMAL
46 * The standard (as opposed to real-time) round-robin scheduling policy.
47 *
48 * (Linux's name for POSIX's SCHED_OTHER.)
49 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000050 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080051 */
52
53/*
54 * @def SCHED_FIFO
55 * The real-time first-in/first-out scheduling policy.
56 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000057 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080058 */
59
60/*
61 * @def SCHED_RR
62 * The real-time round-robin policy. (See also SCHED_NORMAL/SCHED_OTHER.)
63 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000064 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080065 */
66
67/*
68 * @def SCHED_BATCH
69 * The batch scheduling policy.
70 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000071 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080072 */
73
74/*
75 * @def SCHED_IDLE
76 * The low priority "only when otherwise idle" scheduling priority.
77 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000078 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080079 */
80
81/*
82 * @def SCHED_DEADLINE
83 * The deadline scheduling policy.
84 *
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000085 * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -080086 */
87
88/*
89 * The standard (as opposed to real-time) round-robin scheduling policy.
90 *
91 * (POSIX's name for Linux's SCHED_NORMAL.)
92 */
Elliott Hughes887e1142014-01-02 12:05:50 -080093#define SCHED_OTHER SCHED_NORMAL
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094
Elliott Hughes31fbc142021-02-19 14:32:41 -080095/**
96 * See sched_getparam()/sched_setparam() and
97 * sched_getscheduler()/sched_setscheduler().
98 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099struct sched_param {
Dan Albertf6640342014-11-21 10:22:09 -0800100 int sched_priority;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101};
102
Elliott Hughes31fbc142021-02-19 14:32:41 -0800103/**
Florian Mayer7d098bb2024-04-16 21:12:40 +0000104 * [sched_setscheduler(2)](https://man7.org/linux/man-pages/man2/sched_setscheduler.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800105 * sets the scheduling policy and associated parameters for the given thread.
106 *
107 * Returns 0 on success and returns -1 and sets `errno` on failure.
108 */
zijunzhao21463032023-03-18 00:38:14 +0000109int sched_setscheduler(pid_t __pid, int __policy, const struct sched_param* _Nonnull __param);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800110
111/**
Florian Mayer7d098bb2024-04-16 21:12:40 +0000112 * [sched_getscheduler(2)](https://man7.org/linux/man-pages/man2/sched_getscheduler.2)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800113 * gets the scheduling policy for the given thread.
114 *
115 * Returns a non-negative thread policy on success and returns -1 and sets
116 * `errno` on failure.
117 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700118int sched_getscheduler(pid_t __pid);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800119
120/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000121 * [sched_yield(2)](https://man7.org/linux/man-pages/man2/sched_yield.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800122 * voluntarily gives up using the CPU so that another thread can run.
123 *
124 * Returns 0 on success and returns -1 and sets `errno` on failure.
125 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700126int sched_yield(void);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800127
128/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000129 * [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 -0800130 * gets the maximum priority value allowed for the given scheduling policy.
131 *
132 * Returns a priority on success and returns -1 and sets `errno` on failure.
133 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700134int sched_get_priority_max(int __policy);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800135
136/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000137 * [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 -0800138 * gets the minimum priority value allowed for the given scheduling policy.
139 *
140 * Returns a priority on success and returns -1 and sets `errno` on failure.
141 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700142int sched_get_priority_min(int __policy);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800143
144/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000145 * [sched_setparam(2)](https://man7.org/linux/man-pages/man2/sched_setparam.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800146 * sets the scheduling parameters for the given thread.
147 *
148 * Returns 0 on success and returns -1 and sets `errno` on failure.
149 */
zijunzhao21463032023-03-18 00:38:14 +0000150int sched_setparam(pid_t __pid, const struct sched_param* _Nonnull __param);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800151
152/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000153 * [sched_getparam(2)](https://man7.org/linux/man-pages/man2/sched_getparam.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800154 * gets the scheduling parameters for the given thread.
155 *
156 * Returns 0 on success and returns -1 and sets `errno` on failure.
157 */
zijunzhao21463032023-03-18 00:38:14 +0000158int sched_getparam(pid_t __pid, struct sched_param* _Nonnull __param);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800159
160/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000161 * [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 -0800162 * queries the round-robin time quantum for the given thread.
163 *
164 * Returns 0 on success and returns -1 and sets `errno` on failure.
165 */
zijunzhao21463032023-03-18 00:38:14 +0000166int sched_rr_get_interval(pid_t __pid, struct timespec* _Nonnull __quantum);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700168#if defined(__USE_GNU)
Elliott Hughes887e1142014-01-02 12:05:50 -0800169
Elliott Hughes31fbc142021-02-19 14:32:41 -0800170/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000171 * [clone(2)](https://man7.org/linux/man-pages/man2/clone.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800172 * creates a new child process.
173 *
174 * Returns the pid of the child to the caller on success and
175 * returns -1 and sets `errno` on failure.
176 */
Elliott Hughesdb36e082023-05-26 12:58:14 -0700177int 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 -0800178
179/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000180 * [unshare(2)](https://man7.org/linux/man-pages/man2/unshare.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800181 * disassociates part of the caller's execution context.
182 *
183 * Returns 0 on success and returns -1 and sets `errno` on failure.
Elliott Hughes31fbc142021-02-19 14:32:41 -0800184 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700185int unshare(int __flags);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800186
187/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000188 * [setns(2)](https://man7.org/linux/man-pages/man2/setns.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800189 * reassociates a thread with a different namespace.
190 *
191 * Returns 0 on success and returns -1 and sets `errno` on failure.
Elliott Hughes31fbc142021-02-19 14:32:41 -0800192 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700193int setns(int __fd, int __ns_type);
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100194
Elliott Hughes31fbc142021-02-19 14:32:41 -0800195/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000196 * [sched_getcpu(3)](https://man7.org/linux/man-pages/man3/sched_getcpu.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800197 * reports which CPU the caller is running on.
198 *
199 * Returns a non-negative CPU number on success and returns -1 and sets
200 * `errno` on failure.
201 */
202int sched_getcpu(void);
203
Calin Juravledd096992014-05-13 16:01:43 +0100204#ifdef __LP64__
Calin Juravleb7437902014-04-29 20:25:26 +0100205#define CPU_SETSIZE 1024
Calin Juravledd096992014-05-13 16:01:43 +0100206#else
207#define CPU_SETSIZE 32
Calin Juravleb7437902014-04-29 20:25:26 +0100208#endif
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100209
Calin Juravleb7437902014-04-29 20:25:26 +0100210#define __CPU_BITTYPE unsigned long int /* mandated by the kernel */
211#define __CPU_BITS (8 * sizeof(__CPU_BITTYPE))
212#define __CPU_ELT(x) ((x) / __CPU_BITS)
213#define __CPU_MASK(x) ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1)))
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100214
Elliott Hughes31fbc142021-02-19 14:32:41 -0800215/**
216 * [cpu_set_t](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) is a
217 * statically-sized CPU set. See `CPU_ALLOC` for dynamically-sized CPU sets.
218 */
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100219typedef struct {
Calin Juravleb7437902014-04-29 20:25:26 +0100220 __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ];
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100221} cpu_set_t;
222
Elliott Hughes31fbc142021-02-19 14:32:41 -0800223/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000224 * [sched_setaffinity(2)](https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800225 * sets the CPU affinity mask for the given thread.
226 *
227 * Returns 0 on success and returns -1 and sets `errno` on failure.
228 */
zijunzhao21463032023-03-18 00:38:14 +0000229int sched_setaffinity(pid_t __pid, size_t __set_size, const cpu_set_t* _Nonnull __set);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800230
231/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000232 * [sched_getaffinity(2)](https://man7.org/linux/man-pages/man2/sched_getaffinity.2.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800233 * gets the CPU affinity mask for the given thread.
234 *
235 * Returns 0 on success and returns -1 and sets `errno` on failure.
236 */
zijunzhao21463032023-03-18 00:38:14 +0000237int sched_getaffinity(pid_t __pid, size_t __set_size, cpu_set_t* _Nonnull __set);
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100238
Elliott Hughes31fbc142021-02-19 14:32:41 -0800239/**
Elliott Hughesf4420fb2025-01-30 11:21:26 -0800240 * [sched_setattr(2)](https://man7.org/linux/man-pages/man2/sched_setattr.2.html)
241 * sets the scheduling attributes for the given thread.
242 *
243 * Returns 0 on success and returns -1 and sets `errno` on failure.
244 */
245int sched_setattr(pid_t __pid, struct sched_attr* _Nonnull __attr, unsigned __flags) __INTRODUCED_IN(37);
246
247/**
248 * [sched_getattr(2)](https://man7.org/linux/man-pages/man2/sched_getattr.2.html)
249 * gets the scheduling attributes for the given thread.
250 *
251 * Returns 0 on success and returns -1 and sets `errno` on failure.
252 */
253int sched_getattr(pid_t __pid, struct sched_attr* _Nonnull __attr, unsigned __size, unsigned __flags) __INTRODUCED_IN(37);
254
255/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000256 * [CPU_ZERO](https://man7.org/linux/man-pages/man3/CPU_ZERO.3.html) clears all
Elliott Hughes31fbc142021-02-19 14:32:41 -0800257 * bits in a static CPU set.
258 */
Calin Juravleb7437902014-04-29 20:25:26 +0100259#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800260/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000261 * [CPU_ZERO_S](https://man7.org/linux/man-pages/man3/CPU_ZERO_S.3.html) clears all
Elliott Hughes31fbc142021-02-19 14:32:41 -0800262 * bits in a dynamic CPU set allocated by `CPU_ALLOC`.
263 */
Calin Juravleb7437902014-04-29 20:25:26 +0100264#define CPU_ZERO_S(setsize, set) __builtin_memset(set, 0, setsize)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100265
Elliott Hughes31fbc142021-02-19 14:32:41 -0800266/**
267 * [CPU_SET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one
268 * bit in a static CPU set.
269 */
270#define CPU_SET(cpu, set) CPU_SET_S(cpu, sizeof(cpu_set_t), set)
271/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000272 * [CPU_SET_S](https://man7.org/linux/man-pages/man3/CPU_SET_S.3.html) sets one
Elliott Hughes31fbc142021-02-19 14:32:41 -0800273 * bit in a dynamic CPU set allocated by `CPU_ALLOC`.
274 */
Calin Juravleb7437902014-04-29 20:25:26 +0100275#define CPU_SET_S(cpu, setsize, set) \
276 do { \
277 size_t __cpu = (cpu); \
278 if (__cpu < 8 * (setsize)) \
279 (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
280 } while (0)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100281
Elliott Hughes31fbc142021-02-19 14:32:41 -0800282/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000283 * [CPU_CLR](https://man7.org/linux/man-pages/man3/CPU_CLR.3.html) clears one
Elliott Hughes31fbc142021-02-19 14:32:41 -0800284 * bit in a static CPU set.
285 */
286#define CPU_CLR(cpu, set) CPU_CLR_S(cpu, sizeof(cpu_set_t), set)
287/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000288 * [CPU_CLR_S](https://man7.org/linux/man-pages/man3/CPU_CLR_S.3.html) clears one
Elliott Hughes31fbc142021-02-19 14:32:41 -0800289 * bit in a dynamic CPU set allocated by `CPU_ALLOC`.
290 */
Calin Juravleb7437902014-04-29 20:25:26 +0100291#define CPU_CLR_S(cpu, setsize, set) \
292 do { \
293 size_t __cpu = (cpu); \
294 if (__cpu < 8 * (setsize)) \
295 (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
296 } while (0)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100297
Elliott Hughes31fbc142021-02-19 14:32:41 -0800298/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000299 * [CPU_ISSET](https://man7.org/linux/man-pages/man3/CPU_ISSET.3.html) tests
Elliott Hughes31fbc142021-02-19 14:32:41 -0800300 * whether the given bit is set in a static CPU set.
301 */
302#define CPU_ISSET(cpu, set) CPU_ISSET_S(cpu, sizeof(cpu_set_t), set)
303/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000304 * [CPU_ISSET_S](https://man7.org/linux/man-pages/man3/CPU_ISSET_S.3.html) tests
Elliott Hughes31fbc142021-02-19 14:32:41 -0800305 * whether the given bit is set in a dynamic CPU set allocated by `CPU_ALLOC`.
306 */
Calin Juravleb7437902014-04-29 20:25:26 +0100307#define CPU_ISSET_S(cpu, setsize, set) \
308 (__extension__ ({ \
309 size_t __cpu = (cpu); \
310 (__cpu < 8 * (setsize)) \
311 ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
312 : 0; \
313 }))
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100314
Elliott Hughes31fbc142021-02-19 14:32:41 -0800315/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000316 * [CPU_COUNT](https://man7.org/linux/man-pages/man3/CPU_COUNT.3.html) counts
Elliott Hughes31fbc142021-02-19 14:32:41 -0800317 * how many bits are set in a static CPU set.
318 */
319#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
320/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000321 * [CPU_COUNT_S](https://man7.org/linux/man-pages/man3/CPU_COUNT_S.3.html) counts
Elliott Hughes31fbc142021-02-19 14:32:41 -0800322 * how many bits are set in a dynamic CPU set allocated by `CPU_ALLOC`.
323 */
324#define CPU_COUNT_S(setsize, set) __sched_cpucount((setsize), (set))
zijunzhao21463032023-03-18 00:38:14 +0000325int __sched_cpucount(size_t __set_size, const cpu_set_t* _Nonnull __set);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800326
327/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000328 * [CPU_EQUAL](https://man7.org/linux/man-pages/man3/CPU_EQUAL.3.html) tests
Elliott Hughes31fbc142021-02-19 14:32:41 -0800329 * whether two static CPU sets have the same bits set and cleared as each other.
330 */
331#define CPU_EQUAL(set1, set2) CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2)
332/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000333 * [CPU_EQUAL_S](https://man7.org/linux/man-pages/man3/CPU_EQUAL_S.3.html) tests
Elliott Hughes31fbc142021-02-19 14:32:41 -0800334 * whether two dynamic CPU sets allocated by `CPU_ALLOC` have the same bits
335 * set and cleared as each other.
336 */
Calin Juravleb7437902014-04-29 20:25:26 +0100337#define CPU_EQUAL_S(setsize, set1, set2) (__builtin_memcmp(set1, set2, setsize) == 0)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100338
Elliott Hughes31fbc142021-02-19 14:32:41 -0800339/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000340 * [CPU_AND](https://man7.org/linux/man-pages/man3/CPU_AND.3.html) ands two
Elliott Hughes31fbc142021-02-19 14:32:41 -0800341 * static CPU sets.
342 */
343#define CPU_AND(dst, set1, set2) __CPU_OP(dst, set1, set2, &)
344/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000345 * [CPU_AND_S](https://man7.org/linux/man-pages/man3/CPU_AND_S.3.html) ands two
Elliott Hughes31fbc142021-02-19 14:32:41 -0800346 * dynamic CPU sets allocated by `CPU_ALLOC`.
347 */
Calin Juravleb7437902014-04-29 20:25:26 +0100348#define CPU_AND_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, &)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800349
350/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000351 * [CPU_OR](https://man7.org/linux/man-pages/man3/CPU_OR.3.html) ors two
Elliott Hughes31fbc142021-02-19 14:32:41 -0800352 * static CPU sets.
353 */
354#define CPU_OR(dst, set1, set2) __CPU_OP(dst, set1, set2, |)
355/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000356 * [CPU_OR_S](https://man7.org/linux/man-pages/man3/CPU_OR_S.3.html) ors two
Elliott Hughes31fbc142021-02-19 14:32:41 -0800357 * dynamic CPU sets allocated by `CPU_ALLOC`.
358 */
Calin Juravleb7437902014-04-29 20:25:26 +0100359#define CPU_OR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, |)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800360
361/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000362 * [CPU_XOR](https://man7.org/linux/man-pages/man3/CPU_XOR.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800363 * exclusive-ors two static CPU sets.
364 */
365#define CPU_XOR(dst, set1, set2) __CPU_OP(dst, set1, set2, ^)
366/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000367 * [CPU_XOR_S](https://man7.org/linux/man-pages/man3/CPU_XOR_S.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800368 * exclusive-ors two dynamic CPU sets allocated by `CPU_ALLOC`.
369 */
Calin Juravleb7437902014-04-29 20:25:26 +0100370#define CPU_XOR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, ^)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100371
Elliott Hughes31fbc142021-02-19 14:32:41 -0800372#define __CPU_OP(dst, set1, set2, op) __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op)
373
Calin Juravleb7437902014-04-29 20:25:26 +0100374#define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \
375 do { \
376 cpu_set_t* __dst = (dstset); \
377 const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
378 const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
379 size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \
380 for (; __nn < __nn_max; __nn++) \
381 (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
382 } while (0)
383
Elliott Hughes31fbc142021-02-19 14:32:41 -0800384/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000385 * [CPU_ALLOC_SIZE](https://man7.org/linux/man-pages/man3/CPU_ALLOC_SIZE.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800386 * returns the size of a CPU set large enough for CPUs in the range 0..count-1.
387 */
388#define CPU_ALLOC_SIZE(count) \
389 __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE)
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100390
Elliott Hughes31fbc142021-02-19 14:32:41 -0800391/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000392 * [CPU_ALLOC](https://man7.org/linux/man-pages/man3/CPU_ALLOC.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800393 * allocates a CPU set large enough for CPUs in the range 0..count-1.
394 */
395#define CPU_ALLOC(count) __sched_cpualloc((count))
zijunzhao21463032023-03-18 00:38:14 +0000396cpu_set_t* _Nullable __sched_cpualloc(size_t __count);
Elliott Hughes31fbc142021-02-19 14:32:41 -0800397
398/**
Elliott Hughes7fb8d582025-01-06 15:57:38 +0000399 * [CPU_FREE](https://man7.org/linux/man-pages/man3/CPU_FREE.3.html)
Elliott Hughes31fbc142021-02-19 14:32:41 -0800400 * deallocates a CPU set allocated by `CPU_ALLOC`.
401 */
402#define CPU_FREE(set) __sched_cpufree((set))
zijunzhao21463032023-03-18 00:38:14 +0000403void __sched_cpufree(cpu_set_t* _Nonnull __set);
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100404
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700405#endif /* __USE_GNU */
David 'Digit' Turner72e6fd42010-12-03 18:04:01 +0100406
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407__END_DECLS