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 | 5704c42 | 2016-01-25 18:06:24 -0800 | [diff] [blame] | 36 | #include <bits/timespec.h> |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 37 | #include <linux/sched.h> |
Yabin Cui | db49903 | 2014-12-09 20:15:48 -0800 | [diff] [blame] | 38 | #include <sys/cdefs.h> |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | __BEGIN_DECLS |
| 41 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 42 | /* |
| 43 | * @def SCHED_NORMAL |
| 44 | * The standard (as opposed to real-time) round-robin scheduling policy. |
| 45 | * |
| 46 | * (Linux's name for POSIX's SCHED_OTHER.) |
| 47 | * |
| 48 | * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html) |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * @def SCHED_FIFO |
| 53 | * The real-time first-in/first-out scheduling policy. |
| 54 | * |
| 55 | * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html) |
| 56 | */ |
| 57 | |
| 58 | /* |
| 59 | * @def SCHED_RR |
| 60 | * The real-time round-robin policy. (See also SCHED_NORMAL/SCHED_OTHER.) |
| 61 | * |
| 62 | * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html) |
| 63 | */ |
| 64 | |
| 65 | /* |
| 66 | * @def SCHED_BATCH |
| 67 | * The batch scheduling policy. |
| 68 | * |
| 69 | * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html) |
| 70 | */ |
| 71 | |
| 72 | /* |
| 73 | * @def SCHED_IDLE |
| 74 | * The low priority "only when otherwise idle" scheduling priority. |
| 75 | * |
| 76 | * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html) |
| 77 | */ |
| 78 | |
| 79 | /* |
| 80 | * @def SCHED_DEADLINE |
| 81 | * The deadline scheduling policy. |
| 82 | * |
| 83 | * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html) |
| 84 | */ |
| 85 | |
| 86 | /* |
| 87 | * The standard (as opposed to real-time) round-robin scheduling policy. |
| 88 | * |
| 89 | * (POSIX's name for Linux's SCHED_NORMAL.) |
| 90 | */ |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 91 | #define SCHED_OTHER SCHED_NORMAL |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 92 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 93 | /** |
| 94 | * See sched_getparam()/sched_setparam() and |
| 95 | * sched_getscheduler()/sched_setscheduler(). |
| 96 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | struct sched_param { |
Dan Albert | f664034 | 2014-11-21 10:22:09 -0800 | [diff] [blame] | 98 | int sched_priority; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 101 | /** |
| 102 | * [sched_setscheduler(2)](http://man7.org/linux/man-pages/man2/sched_getcpu.2.html) |
| 103 | * sets the scheduling policy and associated parameters for the given thread. |
| 104 | * |
| 105 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 106 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 107 | 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] | 108 | |
| 109 | /** |
| 110 | * [sched_getscheduler(2)](http://man7.org/linux/man-pages/man2/sched_getcpu.2.html) |
| 111 | * gets the scheduling policy for the given thread. |
| 112 | * |
| 113 | * Returns a non-negative thread policy on success and returns -1 and sets |
| 114 | * `errno` on failure. |
| 115 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 116 | int sched_getscheduler(pid_t __pid); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * [sched_yield(2)](http://man7.org/linux/man-pages/man2/sched_yield.2.html) |
| 120 | * voluntarily gives up using the CPU so that another thread can run. |
| 121 | * |
| 122 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 123 | */ |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 124 | int sched_yield(void); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * [sched_get_priority_max(2)](http://man7.org/linux/man-pages/man2/sched_get_priority_max.2.html) |
| 128 | * gets the maximum priority value allowed for the given scheduling policy. |
| 129 | * |
| 130 | * Returns a priority on success and returns -1 and sets `errno` on failure. |
| 131 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 132 | int sched_get_priority_max(int __policy); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * [sched_get_priority_min(2)](http://man7.org/linux/man-pages/man2/sched_get_priority_min.2.html) |
| 136 | * gets the minimum priority value allowed for the given scheduling policy. |
| 137 | * |
| 138 | * Returns a priority on success and returns -1 and sets `errno` on failure. |
| 139 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 140 | int sched_get_priority_min(int __policy); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * [sched_setparam(2)](http://man7.org/linux/man-pages/man2/sched_setparam.2.html) |
| 144 | * sets the scheduling parameters for the given thread. |
| 145 | * |
| 146 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 147 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 148 | int sched_setparam(pid_t __pid, const struct sched_param* _Nonnull __param); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * [sched_getparam(2)](http://man7.org/linux/man-pages/man2/sched_getparam.2.html) |
| 152 | * gets the scheduling parameters for the given thread. |
| 153 | * |
| 154 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 155 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 156 | int sched_getparam(pid_t __pid, struct sched_param* _Nonnull __param); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * [sched_rr_get_interval(2)](http://man7.org/linux/man-pages/man2/sched_rr_get_interval.2.html) |
| 160 | * queries the round-robin time quantum for the given thread. |
| 161 | * |
| 162 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 163 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 164 | 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] | 165 | |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 166 | #if defined(__USE_GNU) |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 167 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 168 | /** |
| 169 | * [clone(2)](http://man7.org/linux/man-pages/man2/clone.2.html) |
| 170 | * creates a new child process. |
| 171 | * |
| 172 | * Returns the pid of the child to the caller on success and |
| 173 | * returns -1 and sets `errno` on failure. |
| 174 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 175 | int clone(int (* __BIONIC_COMPLICATED_NULLNESS __fn)(void* __BIONIC_COMPLICATED_NULLNESS ), void* __BIONIC_COMPLICATED_NULLNESS __child_stack, int __flags, void* _Nullable __arg, ...) __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(17); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * [unshare(2)](http://man7.org/linux/man-pages/man2/unshare.2.html) |
| 179 | * disassociates part of the caller's execution context. |
| 180 | * |
| 181 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 182 | * |
| 183 | * Available since API level 17. |
| 184 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 185 | int unshare(int __flags) __INTRODUCED_IN(17); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * [setns(2)](http://man7.org/linux/man-pages/man2/setns.2.html) |
| 189 | * reassociates a thread with a different namespace. |
| 190 | * |
| 191 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 192 | * |
| 193 | * Available since API level 21. |
| 194 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 195 | int setns(int __fd, int __ns_type) __INTRODUCED_IN(21); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 196 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 197 | /** |
| 198 | * [sched_getcpu(3)](http://man7.org/linux/man-pages/man3/sched_getcpu.3.html) |
| 199 | * reports which CPU the caller is running on. |
| 200 | * |
| 201 | * Returns a non-negative CPU number on success and returns -1 and sets |
| 202 | * `errno` on failure. |
| 203 | */ |
| 204 | int sched_getcpu(void); |
| 205 | |
Calin Juravle | dd09699 | 2014-05-13 16:01:43 +0100 | [diff] [blame] | 206 | #ifdef __LP64__ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 207 | #define CPU_SETSIZE 1024 |
Calin Juravle | dd09699 | 2014-05-13 16:01:43 +0100 | [diff] [blame] | 208 | #else |
| 209 | #define CPU_SETSIZE 32 |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 210 | #endif |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 211 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 212 | #define __CPU_BITTYPE unsigned long int /* mandated by the kernel */ |
| 213 | #define __CPU_BITS (8 * sizeof(__CPU_BITTYPE)) |
| 214 | #define __CPU_ELT(x) ((x) / __CPU_BITS) |
| 215 | #define __CPU_MASK(x) ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1))) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 216 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 217 | /** |
| 218 | * [cpu_set_t](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) is a |
| 219 | * statically-sized CPU set. See `CPU_ALLOC` for dynamically-sized CPU sets. |
| 220 | */ |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 221 | typedef struct { |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 222 | __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ]; |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 223 | } cpu_set_t; |
| 224 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 225 | /** |
| 226 | * [sched_setaffinity(2)](http://man7.org/linux/man-pages/man2/sched_setaffinity.2.html) |
| 227 | * sets the CPU affinity mask for the given thread. |
| 228 | * |
| 229 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 230 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 231 | 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] | 232 | |
| 233 | /** |
| 234 | * [sched_getaffinity(2)](http://man7.org/linux/man-pages/man2/sched_getaffinity.2.html) |
| 235 | * gets the CPU affinity mask for the given thread. |
| 236 | * |
| 237 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 238 | */ |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 239 | 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] | 240 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 241 | /** |
| 242 | * [CPU_ZERO](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears all |
| 243 | * bits in a static CPU set. |
| 244 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 245 | #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set) |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 246 | /** |
| 247 | * [CPU_ZERO_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears all |
| 248 | * bits in a dynamic CPU set allocated by `CPU_ALLOC`. |
| 249 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 250 | #define CPU_ZERO_S(setsize, set) __builtin_memset(set, 0, setsize) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 251 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 252 | /** |
| 253 | * [CPU_SET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one |
| 254 | * bit in a static CPU set. |
| 255 | */ |
| 256 | #define CPU_SET(cpu, set) CPU_SET_S(cpu, sizeof(cpu_set_t), set) |
| 257 | /** |
| 258 | * [CPU_SET_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one |
| 259 | * bit in a dynamic CPU set allocated by `CPU_ALLOC`. |
| 260 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 261 | #define CPU_SET_S(cpu, setsize, set) \ |
| 262 | do { \ |
| 263 | size_t __cpu = (cpu); \ |
| 264 | if (__cpu < 8 * (setsize)) \ |
| 265 | (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \ |
| 266 | } while (0) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 267 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 268 | /** |
| 269 | * [CPU_CLR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears one |
| 270 | * bit in a static CPU set. |
| 271 | */ |
| 272 | #define CPU_CLR(cpu, set) CPU_CLR_S(cpu, sizeof(cpu_set_t), set) |
| 273 | /** |
| 274 | * [CPU_CLR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears one |
| 275 | * bit in a dynamic CPU set allocated by `CPU_ALLOC`. |
| 276 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 277 | #define CPU_CLR_S(cpu, setsize, set) \ |
| 278 | do { \ |
| 279 | size_t __cpu = (cpu); \ |
| 280 | if (__cpu < 8 * (setsize)) \ |
| 281 | (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \ |
| 282 | } while (0) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 283 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 284 | /** |
| 285 | * [CPU_ISSET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests |
| 286 | * whether the given bit is set in a static CPU set. |
| 287 | */ |
| 288 | #define CPU_ISSET(cpu, set) CPU_ISSET_S(cpu, sizeof(cpu_set_t), set) |
| 289 | /** |
| 290 | * [CPU_ISSET_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests |
| 291 | * whether the given bit is set in a dynamic CPU set allocated by `CPU_ALLOC`. |
| 292 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 293 | #define CPU_ISSET_S(cpu, setsize, set) \ |
| 294 | (__extension__ ({ \ |
| 295 | size_t __cpu = (cpu); \ |
| 296 | (__cpu < 8 * (setsize)) \ |
| 297 | ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \ |
| 298 | : 0; \ |
| 299 | })) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 300 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 301 | /** |
| 302 | * [CPU_COUNT](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) counts |
| 303 | * how many bits are set in a static CPU set. |
| 304 | */ |
| 305 | #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set) |
| 306 | /** |
| 307 | * [CPU_COUNT_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) counts |
| 308 | * how many bits are set in a dynamic CPU set allocated by `CPU_ALLOC`. |
| 309 | */ |
| 310 | #define CPU_COUNT_S(setsize, set) __sched_cpucount((setsize), (set)) |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 311 | 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] | 312 | |
| 313 | /** |
| 314 | * [CPU_EQUAL](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests |
| 315 | * whether two static CPU sets have the same bits set and cleared as each other. |
| 316 | */ |
| 317 | #define CPU_EQUAL(set1, set2) CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2) |
| 318 | /** |
| 319 | * [CPU_EQUAL_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests |
| 320 | * whether two dynamic CPU sets allocated by `CPU_ALLOC` have the same bits |
| 321 | * set and cleared as each other. |
| 322 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 323 | #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] | 324 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 325 | /** |
| 326 | * [CPU_AND](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ands two |
| 327 | * static CPU sets. |
| 328 | */ |
| 329 | #define CPU_AND(dst, set1, set2) __CPU_OP(dst, set1, set2, &) |
| 330 | /** |
| 331 | * [CPU_AND_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ands two |
| 332 | * dynamic CPU sets allocated by `CPU_ALLOC`. |
| 333 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 334 | #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] | 335 | |
| 336 | /** |
| 337 | * [CPU_OR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ors two |
| 338 | * static CPU sets. |
| 339 | */ |
| 340 | #define CPU_OR(dst, set1, set2) __CPU_OP(dst, set1, set2, |) |
| 341 | /** |
| 342 | * [CPU_OR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ors two |
| 343 | * dynamic CPU sets allocated by `CPU_ALLOC`. |
| 344 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 345 | #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] | 346 | |
| 347 | /** |
| 348 | * [CPU_XOR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) |
| 349 | * exclusive-ors two static CPU sets. |
| 350 | */ |
| 351 | #define CPU_XOR(dst, set1, set2) __CPU_OP(dst, set1, set2, ^) |
| 352 | /** |
| 353 | * [CPU_XOR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) |
| 354 | * exclusive-ors two dynamic CPU sets allocated by `CPU_ALLOC`. |
| 355 | */ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 356 | #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] | 357 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 358 | #define __CPU_OP(dst, set1, set2, op) __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op) |
| 359 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 360 | #define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \ |
| 361 | do { \ |
| 362 | cpu_set_t* __dst = (dstset); \ |
| 363 | const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \ |
| 364 | const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \ |
| 365 | size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \ |
| 366 | for (; __nn < __nn_max; __nn++) \ |
| 367 | (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \ |
| 368 | } while (0) |
| 369 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 370 | /** |
| 371 | * [CPU_ALLOC_SIZE](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) |
| 372 | * returns the size of a CPU set large enough for CPUs in the range 0..count-1. |
| 373 | */ |
| 374 | #define CPU_ALLOC_SIZE(count) \ |
| 375 | __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 376 | |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 377 | /** |
| 378 | * [CPU_ALLOC](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) |
| 379 | * allocates a CPU set large enough for CPUs in the range 0..count-1. |
| 380 | */ |
| 381 | #define CPU_ALLOC(count) __sched_cpualloc((count)) |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 382 | cpu_set_t* _Nullable __sched_cpualloc(size_t __count); |
Elliott Hughes | 31fbc14 | 2021-02-19 14:32:41 -0800 | [diff] [blame] | 383 | |
| 384 | /** |
| 385 | * [CPU_FREE](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) |
| 386 | * deallocates a CPU set allocated by `CPU_ALLOC`. |
| 387 | */ |
| 388 | #define CPU_FREE(set) __sched_cpufree((set)) |
zijunzhao | 2146303 | 2023-03-18 00:38:14 +0000 | [diff] [blame^] | 389 | void __sched_cpufree(cpu_set_t* _Nonnull __set); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 390 | |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 391 | #endif /* __USE_GNU */ |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 392 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 393 | __END_DECLS |