The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 2950f13 | 2017-08-01 23:02:48 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file sched.h |
| 33 | * @brief Thread execution scheduling. |
| 34 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | 414dd2d | 2024-10-16 14:48:30 +0000 | [diff] [blame] | 36 | #include <sys/cdefs.h> |
| 37 | |
Elliott Hughes | 5704c42 | 2016-01-25 18:06:24 -0800 | [diff] [blame] | 38 | #include <bits/timespec.h> |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 39 | #include <linux/sched.h> |
| 40 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | __BEGIN_DECLS |
| 42 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 43 | /* |
| 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 Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 49 | * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | /* |
| 53 | * @def SCHED_FIFO |
| 54 | * The real-time first-in/first-out scheduling policy. |
| 55 | * |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 56 | * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 57 | */ |
| 58 | |
| 59 | /* |
| 60 | * @def SCHED_RR |
| 61 | * The real-time round-robin policy. (See also SCHED_NORMAL/SCHED_OTHER.) |
| 62 | * |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 63 | * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 64 | */ |
| 65 | |
| 66 | /* |
| 67 | * @def SCHED_BATCH |
| 68 | * The batch scheduling policy. |
| 69 | * |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 70 | * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | /* |
| 74 | * @def SCHED_IDLE |
| 75 | * The low priority "only when otherwise idle" scheduling priority. |
| 76 | * |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 77 | * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 78 | */ |
| 79 | |
| 80 | /* |
| 81 | * @def SCHED_DEADLINE |
| 82 | * The deadline scheduling policy. |
| 83 | * |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 84 | * See [sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 85 | */ |
| 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 Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 92 | #define SCHED_OTHER SCHED_NORMAL |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 94 | /** |
| 95 | * See sched_getparam()/sched_setparam() and |
| 96 | * sched_getscheduler()/sched_setscheduler(). |
| 97 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | struct sched_param { |
Dan Albert | f664034 | 2014-11-21 10:22:09 -0800 | [diff] [blame] | 99 | int sched_priority; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 102 | /** |
Florian Mayer | 7d098bb | 2024-04-16 21:12:40 +0000 | [diff] [blame] | 103 | * [sched_setscheduler(2)](https://man7.org/linux/man-pages/man2/sched_setscheduler.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 104 | * 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 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 108 | int sched_setscheduler(pid_t __pid, int __policy, const struct sched_param* _Nonnull __param); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
Florian Mayer | 7d098bb | 2024-04-16 21:12:40 +0000 | [diff] [blame] | 111 | * [sched_getscheduler(2)](https://man7.org/linux/man-pages/man2/sched_getscheduler.2) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 112 | * 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 Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 117 | int sched_getscheduler(pid_t __pid); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 120 | * [sched_yield(2)](https://man7.org/linux/man-pages/man2/sched_yield.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 121 | * 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 Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 125 | int sched_yield(void); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 128 | * [sched_get_priority_max(2)](https://man7.org/linux/man-pages/man2/sched_get_priority_max.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 129 | * 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 Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 133 | int sched_get_priority_max(int __policy); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 134 | |
| 135 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 136 | * [sched_get_priority_min(2)](https://man7.org/linux/man-pages/man2/sched_get_priority_min.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 137 | * 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 Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 141 | int sched_get_priority_min(int __policy); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 144 | * [sched_setparam(2)](https://man7.org/linux/man-pages/man2/sched_setparam.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 145 | * sets the scheduling parameters for the given thread. |
| 146 | * |
| 147 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 148 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 149 | int sched_setparam(pid_t __pid, const struct sched_param* _Nonnull __param); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 152 | * [sched_getparam(2)](https://man7.org/linux/man-pages/man2/sched_getparam.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 153 | * gets the scheduling parameters for the given thread. |
| 154 | * |
| 155 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 156 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 157 | int sched_getparam(pid_t __pid, struct sched_param* _Nonnull __param); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 160 | * [sched_rr_get_interval(2)](https://man7.org/linux/man-pages/man2/sched_rr_get_interval.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 161 | * 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 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 165 | int sched_rr_get_interval(pid_t __pid, struct timespec* _Nonnull __quantum); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 166 | |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 167 | #if defined(__USE_GNU) |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 168 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 169 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 170 | * [clone(2)](https://man7.org/linux/man-pages/man2/clone.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 171 | * 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 Hughes | db36e08 | 2023-05-26 12:58:14 -0700 | [diff] [blame] | 176 | int clone(int (* __BIONIC_COMPLICATED_NULLNESS __fn)(void* __BIONIC_COMPLICATED_NULLNESS ), void* __BIONIC_COMPLICATED_NULLNESS __child_stack, int __flags, void* _Nullable __arg, ...); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 177 | |
| 178 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 179 | * [unshare(2)](https://man7.org/linux/man-pages/man2/unshare.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 180 | * disassociates part of the caller's execution context. |
| 181 | * |
| 182 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 183 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 184 | int unshare(int __flags); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 187 | * [setns(2)](https://man7.org/linux/man-pages/man2/setns.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 188 | * reassociates a thread with a different namespace. |
| 189 | * |
| 190 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 191 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 192 | int setns(int __fd, int __ns_type); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 193 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 194 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 195 | * [sched_getcpu(3)](https://man7.org/linux/man-pages/man3/sched_getcpu.3.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 196 | * 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 | */ |
| 201 | int sched_getcpu(void); |
| 202 | |
Calin Juravle | dd09699 | 2014-05-13 16:01:43 +0100 | [diff] [blame] | 203 | #ifdef __LP64__ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 204 | #define CPU_SETSIZE 1024 |
Calin Juravle | dd09699 | 2014-05-13 16:01:43 +0100 | [diff] [blame] | 205 | #else |
| 206 | #define CPU_SETSIZE 32 |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 207 | #endif |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 208 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 209 | #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' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 213 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 214 | /** |
| 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' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 218 | typedef struct { |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 219 | __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ]; |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 220 | } cpu_set_t; |
| 221 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 222 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 223 | * [sched_setaffinity(2)](https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 224 | * sets the CPU affinity mask for the given thread. |
| 225 | * |
| 226 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 227 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 228 | int sched_setaffinity(pid_t __pid, size_t __set_size, const cpu_set_t* _Nonnull __set); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 229 | |
| 230 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 231 | * [sched_getaffinity(2)](https://man7.org/linux/man-pages/man2/sched_getaffinity.2.html) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 232 | * gets the CPU affinity mask for the given thread. |
| 233 | * |
| 234 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 235 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 236 | int sched_getaffinity(pid_t __pid, size_t __set_size, cpu_set_t* _Nonnull __set); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 237 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 238 | /** |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 242 | #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 243 | /** |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 247 | #define CPU_ZERO_S(setsize, set) __builtin_memset(set, 0, setsize) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 248 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 249 | /** |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 258 | #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' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 264 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 265 | /** |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 274 | #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' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 280 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 281 | /** |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 290 | #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' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 297 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 298 | /** |
| 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)) |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 308 | int __sched_cpucount(size_t __set_size, const cpu_set_t* _Nonnull __set); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 309 | |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 320 | #define CPU_EQUAL_S(setsize, set1, set2) (__builtin_memcmp(set1, set2, setsize) == 0) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 321 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 322 | /** |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 331 | #define CPU_AND_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, &) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 332 | |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 342 | #define CPU_OR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, |) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 343 | |
| 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 Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 353 | #define CPU_XOR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, ^) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 354 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 355 | #define __CPU_OP(dst, set1, set2, op) __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op) |
| 356 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 357 | #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 Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 367 | /** |
| 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' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 373 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 374 | /** |
| 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)) |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 379 | cpu_set_t* _Nullable __sched_cpualloc(size_t __count); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 380 | |
| 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)) |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame] | 386 | void __sched_cpufree(cpu_set_t* _Nonnull __set); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 387 | |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 388 | #endif /* __USE_GNU */ |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 389 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 390 | __END_DECLS |