aboutsummaryrefslogtreecommitdiff
path: root/libc/include/sched.h
blob: 9f043b600ead7951b6500bf541cc84db73eb8c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#pragma once

/**
 * @file sched.h
 * @brief Thread execution scheduling.
 */

#include <bits/timespec.h>
#include <linux/sched.h>
#include <sys/cdefs.h>

__BEGIN_DECLS

/*
 * @def SCHED_NORMAL
 * The standard (as opposed to real-time) round-robin scheduling policy.
 *
 * (Linux's name for POSIX's SCHED_OTHER.)
 *
 * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html)
 */

/*
 * @def SCHED_FIFO
 * The real-time first-in/first-out scheduling policy.
 *
 * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html)
 */

/*
 * @def SCHED_RR
 * The real-time round-robin policy. (See also SCHED_NORMAL/SCHED_OTHER.)
 *
 * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html)
 */

/*
 * @def SCHED_BATCH
 * The batch scheduling policy.
 *
 * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html)
 */

/*
 * @def SCHED_IDLE
 * The low priority "only when otherwise idle" scheduling priority.
 *
 * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html)
 */

/*
 * @def SCHED_DEADLINE
 * The deadline scheduling policy.
 *
 * See [sched(7)](http://man7.org/linux/man-pages/man7/sched.7.html)
 */

/*
 * The standard (as opposed to real-time) round-robin scheduling policy.
 *
 * (POSIX's name for Linux's SCHED_NORMAL.)
 */
#define SCHED_OTHER SCHED_NORMAL

/**
 * See sched_getparam()/sched_setparam() and
 * sched_getscheduler()/sched_setscheduler().
 */
struct sched_param {
  int sched_priority;
};

/**
 * [sched_setscheduler(2)](https://man7.org/linux/man-pages/man2/sched_setscheduler.2.html)
 * sets the scheduling policy and associated parameters for the given thread.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_setscheduler(pid_t __pid, int __policy, const struct sched_param* _Nonnull __param);

/**
 * [sched_getscheduler(2)](https://man7.org/linux/man-pages/man2/sched_getscheduler.2)
 * gets the scheduling policy for the given thread.
 *
 * Returns a non-negative thread policy on success and returns -1 and sets
 * `errno` on failure.
 */
int sched_getscheduler(pid_t __pid);

/**
 * [sched_yield(2)](http://man7.org/linux/man-pages/man2/sched_yield.2.html)
 * voluntarily gives up using the CPU so that another thread can run.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_yield(void);

/**
 * [sched_get_priority_max(2)](http://man7.org/linux/man-pages/man2/sched_get_priority_max.2.html)
 * gets the maximum priority value allowed for the given scheduling policy.
 *
 * Returns a priority on success and returns -1 and sets `errno` on failure.
 */
int sched_get_priority_max(int __policy);

/**
 * [sched_get_priority_min(2)](http://man7.org/linux/man-pages/man2/sched_get_priority_min.2.html)
 * gets the minimum priority value allowed for the given scheduling policy.
 *
 * Returns a priority on success and returns -1 and sets `errno` on failure.
 */
int sched_get_priority_min(int __policy);

/**
 * [sched_setparam(2)](http://man7.org/linux/man-pages/man2/sched_setparam.2.html)
 * sets the scheduling parameters for the given thread.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_setparam(pid_t __pid, const struct sched_param* _Nonnull __param);

/**
 * [sched_getparam(2)](http://man7.org/linux/man-pages/man2/sched_getparam.2.html)
 * gets the scheduling parameters for the given thread.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_getparam(pid_t __pid, struct sched_param* _Nonnull __param);

/**
 * [sched_rr_get_interval(2)](http://man7.org/linux/man-pages/man2/sched_rr_get_interval.2.html)
 * queries the round-robin time quantum for the given thread.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_rr_get_interval(pid_t __pid, struct timespec* _Nonnull __quantum);

#if defined(__USE_GNU)

/**
 * [clone(2)](http://man7.org/linux/man-pages/man2/clone.2.html)
 * creates a new child process.
 *
 * Returns the pid of the child to the caller on success and
 * returns -1 and sets `errno` on failure.
 */
int clone(int (* __BIONIC_COMPLICATED_NULLNESS __fn)(void* __BIONIC_COMPLICATED_NULLNESS ), void* __BIONIC_COMPLICATED_NULLNESS __child_stack, int __flags, void* _Nullable __arg, ...);

/**
 * [unshare(2)](http://man7.org/linux/man-pages/man2/unshare.2.html)
 * disassociates part of the caller's execution context.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int unshare(int __flags);

/**
 * [setns(2)](http://man7.org/linux/man-pages/man2/setns.2.html)
 * reassociates a thread with a different namespace.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int setns(int __fd, int __ns_type);

/**
 * [sched_getcpu(3)](http://man7.org/linux/man-pages/man3/sched_getcpu.3.html)
 * reports which CPU the caller is running on.
 *
 * Returns a non-negative CPU number on success and returns -1 and sets
 * `errno` on failure.
 */
int sched_getcpu(void);

#ifdef __LP64__
#define CPU_SETSIZE 1024
#else
#define CPU_SETSIZE 32
#endif

#define __CPU_BITTYPE  unsigned long int  /* mandated by the kernel  */
#define __CPU_BITS     (8 * sizeof(__CPU_BITTYPE))
#define __CPU_ELT(x)   ((x) / __CPU_BITS)
#define __CPU_MASK(x)  ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1)))

/**
 * [cpu_set_t](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) is a
 * statically-sized CPU set. See `CPU_ALLOC` for dynamically-sized CPU sets.
 */
typedef struct {
  __CPU_BITTYPE  __bits[ CPU_SETSIZE / __CPU_BITS ];
} cpu_set_t;

/**
 * [sched_setaffinity(2)](http://man7.org/linux/man-pages/man2/sched_setaffinity.2.html)
 * sets the CPU affinity mask for the given thread.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_setaffinity(pid_t __pid, size_t __set_size, const cpu_set_t* _Nonnull __set);

/**
 * [sched_getaffinity(2)](http://man7.org/linux/man-pages/man2/sched_getaffinity.2.html)
 * gets the CPU affinity mask for the given thread.
 *
 * Returns 0 on success and returns -1 and sets `errno` on failure.
 */
int sched_getaffinity(pid_t __pid, size_t __set_size, cpu_set_t* _Nonnull __set);

/**
 * [CPU_ZERO](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears all
 * bits in a static CPU set.
 */
#define CPU_ZERO(set)          CPU_ZERO_S(sizeof(cpu_set_t), set)
/**
 * [CPU_ZERO_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears all
 * bits in a dynamic CPU set allocated by `CPU_ALLOC`.
 */
#define CPU_ZERO_S(setsize, set)  __builtin_memset(set, 0, setsize)

/**
 * [CPU_SET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one
 * bit in a static CPU set.
 */
#define CPU_SET(cpu, set)      CPU_SET_S(cpu, sizeof(cpu_set_t), set)
/**
 * [CPU_SET_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) sets one
 * bit in a dynamic CPU set allocated by `CPU_ALLOC`.
 */
#define CPU_SET_S(cpu, setsize, set) \
  do { \
    size_t __cpu = (cpu); \
    if (__cpu < 8 * (setsize)) \
      (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
  } while (0)

/**
 * [CPU_CLR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears one
 * bit in a static CPU set.
 */
#define CPU_CLR(cpu, set)      CPU_CLR_S(cpu, sizeof(cpu_set_t), set)
/**
 * [CPU_CLR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) clears one
 * bit in a dynamic CPU set allocated by `CPU_ALLOC`.
 */
#define CPU_CLR_S(cpu, setsize, set) \
  do { \
    size_t __cpu = (cpu); \
    if (__cpu < 8 * (setsize)) \
      (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
  } while (0)

/**
 * [CPU_ISSET](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
 * whether the given bit is set in a static CPU set.
 */
#define CPU_ISSET(cpu, set)    CPU_ISSET_S(cpu, sizeof(cpu_set_t), set)
/**
 * [CPU_ISSET_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
 * whether the given bit is set in a dynamic CPU set allocated by `CPU_ALLOC`.
 */
#define CPU_ISSET_S(cpu, setsize, set) \
  (__extension__ ({ \
    size_t __cpu = (cpu); \
    (__cpu < 8 * (setsize)) \
      ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
      : 0; \
  }))

/**
 * [CPU_COUNT](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) counts
 * how many bits are set in a static CPU set.
 */
#define CPU_COUNT(set)         CPU_COUNT_S(sizeof(cpu_set_t), set)
/**
 * [CPU_COUNT_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) counts
 * how many bits are set in a dynamic CPU set allocated by `CPU_ALLOC`.
 */
#define CPU_COUNT_S(setsize, set)  __sched_cpucount((setsize), (set))
int __sched_cpucount(size_t __set_size, const cpu_set_t* _Nonnull __set);

/**
 * [CPU_EQUAL](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
 * whether two static CPU sets have the same bits set and cleared as each other.
 */
#define CPU_EQUAL(set1, set2)  CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2)
/**
 * [CPU_EQUAL_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) tests
 * whether two dynamic CPU sets allocated by `CPU_ALLOC` have the same bits
 * set and cleared as each other.
 */
#define CPU_EQUAL_S(setsize, set1, set2)  (__builtin_memcmp(set1, set2, setsize) == 0)

/**
 * [CPU_AND](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ands two
 * static CPU sets.
 */
#define CPU_AND(dst, set1, set2)  __CPU_OP(dst, set1, set2, &)
/**
 * [CPU_AND_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ands two
 * dynamic CPU sets allocated by `CPU_ALLOC`.
 */
#define CPU_AND_S(setsize, dst, set1, set2)  __CPU_OP_S(setsize, dst, set1, set2, &)

/**
 * [CPU_OR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ors two
 * static CPU sets.
 */
#define CPU_OR(dst, set1, set2)   __CPU_OP(dst, set1, set2, |)
/**
 * [CPU_OR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html) ors two
 * dynamic CPU sets allocated by `CPU_ALLOC`.
 */
#define CPU_OR_S(setsize, dst, set1, set2)   __CPU_OP_S(setsize, dst, set1, set2, |)

/**
 * [CPU_XOR](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
 * exclusive-ors two static CPU sets.
 */
#define CPU_XOR(dst, set1, set2)  __CPU_OP(dst, set1, set2, ^)
/**
 * [CPU_XOR_S](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
 * exclusive-ors two dynamic CPU sets allocated by `CPU_ALLOC`.
 */
#define CPU_XOR_S(setsize, dst, set1, set2)  __CPU_OP_S(setsize, dst, set1, set2, ^)

#define __CPU_OP(dst, set1, set2, op)  __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op)

#define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \
  do { \
    cpu_set_t* __dst = (dstset); \
    const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
    const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
    size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \
    for (; __nn < __nn_max; __nn++) \
      (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
  } while (0)

/**
 * [CPU_ALLOC_SIZE](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
 * returns the size of a CPU set large enough for CPUs in the range 0..count-1.
 */
#define CPU_ALLOC_SIZE(count) \
  __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE)

/**
 * [CPU_ALLOC](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
 * allocates a CPU set large enough for CPUs in the range 0..count-1.
 */
#define CPU_ALLOC(count)  __sched_cpualloc((count))
cpu_set_t* _Nullable __sched_cpualloc(size_t __count);

/**
 * [CPU_FREE](https://man7.org/linux/man-pages/man3/CPU_SET.3.html)
 * deallocates a CPU set allocated by `CPU_ALLOC`.
 */
#define CPU_FREE(set)     __sched_cpufree((set))
void __sched_cpufree(cpu_set_t* _Nonnull __set);

#endif /* __USE_GNU */

__END_DECLS