1 #pragma once
2 
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #include <features.h>
8 
9 #define __NEED_struct_timespec
10 #define __NEED_pid_t
11 #define __NEED_time_t
12 
13 #ifdef _GNU_SOURCE
14 #define __NEED_size_t
15 #endif
16 
17 #include <bits/alltypes.h>
18 
19 struct sched_param {
20     int sched_priority;
21     int sched_ss_low_priority;
22     struct timespec sched_ss_repl_period;
23     struct timespec sched_ss_init_budget;
24     int sched_ss_max_repl;
25 };
26 
27 int sched_get_priority_max(int);
28 int sched_get_priority_min(int);
29 int sched_getparam(pid_t, struct sched_param*);
30 int sched_getscheduler(pid_t);
31 int sched_rr_get_interval(pid_t, struct timespec*);
32 int sched_setparam(pid_t, const struct sched_param*);
33 int sched_setscheduler(pid_t, int, const struct sched_param*);
34 int sched_yield(void);
35 
36 #define SCHED_OTHER 0
37 #define SCHED_FIFO 1
38 #define SCHED_RR 2
39 #define SCHED_BATCH 3
40 #define SCHED_IDLE 5
41 #define SCHED_DEADLINE 6
42 #define SCHED_RESET_ON_FORK 0x40000000
43 
44 #ifdef _GNU_SOURCE
45 void* memcpy(void* __restrict, const void* __restrict, size_t);
46 int memcmp(const void*, const void*, size_t);
47 void* calloc(size_t, size_t);
48 void free(void*);
49 
50 typedef struct cpu_set_t { unsigned long __bits[128 / sizeof(long)]; } cpu_set_t;
51 int __sched_cpucount(size_t, const cpu_set_t*);
52 int sched_getcpu(void);
53 int sched_getaffinity(pid_t, size_t, cpu_set_t*);
54 int sched_setaffinity(pid_t, size_t, const cpu_set_t*);
55 
56 #define __CPU_op_S(i, size, set, op) \
57     ((i) / 8U >= (size)              \
58          ? 0                         \
59          : ((set)->__bits[(i) / 8 / sizeof(long)] op(1UL << ((i) % (8 * sizeof(long))))))
60 
61 #define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=)
62 #define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &= ~)
63 #define CPU_ISSET_S(i, size, set) __CPU_op_S(i, size, set, &)
64 
65 #define __CPU_op_func_S(func, op)                                                             \
66     static __inline void __CPU_##func##_S(size_t __size, cpu_set_t* __dest,                   \
67                                           const cpu_set_t* __src1, const cpu_set_t* __src2) { \
68         size_t __i;                                                                           \
69         for (__i = 0; __i < __size / sizeof(long); __i++)                                     \
70             __dest->__bits[__i] = __src1->__bits[__i] op __src2->__bits[__i];                 \
71     }
72 
73 __CPU_op_func_S(AND, &) __CPU_op_func_S(OR, |) __CPU_op_func_S(XOR, ^)
74 
75 #define CPU_AND_S(a, b, c, d) __CPU_AND_S(a, b, c, d)
76 #define CPU_OR_S(a, b, c, d) __CPU_OR_S(a, b, c, d)
77 #define CPU_XOR_S(a, b, c, d) __CPU_XOR_S(a, b, c, d)
78 
79 #define CPU_COUNT_S(size, set) __sched_cpucount(size, set)
80 #define CPU_ZERO_S(size, set) memset(set, 0, size)
81 #define CPU_EQUAL_S(size, set1, set2) (!memcmp(set1, set2, size))
82 
83 #define CPU_ALLOC_SIZE(n)                       \
84     (sizeof(long) * ((n) / (8 * sizeof(long)) + \
85                      ((n) % (8 * sizeof(long)) + 8 * sizeof(long) - 1) / (8 * sizeof(long))))
86 #define CPU_ALLOC(n) ((cpu_set_t*)calloc(1, CPU_ALLOC_SIZE(n)))
87 #define CPU_FREE(set) free(set)
88 
89 #define CPU_SETSIZE 128
90 
91 #define CPU_SET(i, set) CPU_SET_S(i, sizeof(cpu_set_t), set)
92 #define CPU_CLR(i, set) CPU_CLR_S(i, sizeof(cpu_set_t), set)
93 #define CPU_ISSET(i, set) CPU_ISSET_S(i, sizeof(cpu_set_t), set)
94 #define CPU_AND(d, s1, s2) CPU_AND_S(sizeof(cpu_set_t), d, s1, s2)
95 #define CPU_OR(d, s1, s2) CPU_OR_S(sizeof(cpu_set_t), d, s1, s2)
96 #define CPU_XOR(d, s1, s2) CPU_XOR_S(sizeof(cpu_set_t), d, s1, s2)
97 #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
98 #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
99 #define CPU_EQUAL(s1, s2) CPU_EQUAL_S(sizeof(cpu_set_t), s1, s2)
100 
101 #endif
102 
103 #ifdef __cplusplus
104 }
105 #endif
106