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 | */ |
| 28 | #ifndef _SCHED_H_ |
| 29 | #define _SCHED_H_ |
| 30 | |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 31 | #include <linux/sched.h> |
Yabin Cui | db49903 | 2014-12-09 20:15:48 -0800 | [diff] [blame] | 32 | #include <machine/timespec.h> |
| 33 | #include <sys/cdefs.h> |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 34 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | __BEGIN_DECLS |
| 36 | |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 37 | /* This name is used by glibc, but not by the kernel. */ |
| 38 | #define SCHED_OTHER SCHED_NORMAL |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | |
| 40 | struct sched_param { |
Dan Albert | f664034 | 2014-11-21 10:22:09 -0800 | [diff] [blame] | 41 | int sched_priority; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 44 | extern int sched_setscheduler(pid_t, int, const struct sched_param*); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | extern int sched_getscheduler(pid_t); |
| 46 | extern int sched_yield(void); |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 47 | extern int sched_get_priority_max(int); |
| 48 | extern int sched_get_priority_min(int); |
| 49 | extern int sched_setparam(pid_t, const struct sched_param*); |
| 50 | extern int sched_getparam(pid_t, struct sched_param*); |
| 51 | extern int sched_rr_get_interval(pid_t, struct timespec*); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 53 | #if defined(__USE_GNU) |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 54 | |
| 55 | extern int clone(int (*)(void*), void*, int, void*, ...); |
Jeff Sharkey | 82393de | 2012-08-11 14:12:26 -0700 | [diff] [blame] | 56 | extern int unshare(int); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 57 | extern int sched_getcpu(void); |
Elliott Hughes | 887e114 | 2014-01-02 12:05:50 -0800 | [diff] [blame] | 58 | extern int setns(int, int); |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 59 | |
Calin Juravle | dd09699 | 2014-05-13 16:01:43 +0100 | [diff] [blame] | 60 | #ifdef __LP64__ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 61 | #define CPU_SETSIZE 1024 |
Calin Juravle | dd09699 | 2014-05-13 16:01:43 +0100 | [diff] [blame] | 62 | #else |
| 63 | #define CPU_SETSIZE 32 |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 64 | #endif |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 65 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 66 | #define __CPU_BITTYPE unsigned long int /* mandated by the kernel */ |
| 67 | #define __CPU_BITS (8 * sizeof(__CPU_BITTYPE)) |
| 68 | #define __CPU_ELT(x) ((x) / __CPU_BITS) |
| 69 | #define __CPU_MASK(x) ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1))) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 70 | |
| 71 | typedef struct { |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 72 | __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ]; |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 73 | } cpu_set_t; |
| 74 | |
| 75 | extern int sched_setaffinity(pid_t pid, size_t setsize, const cpu_set_t* set); |
| 76 | |
| 77 | extern int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t* set); |
| 78 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 79 | #define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set) |
| 80 | #define CPU_SET(cpu, set) CPU_SET_S(cpu, sizeof(cpu_set_t), set) |
| 81 | #define CPU_CLR(cpu, set) CPU_CLR_S(cpu, sizeof(cpu_set_t), set) |
| 82 | #define CPU_ISSET(cpu, set) CPU_ISSET_S(cpu, sizeof(cpu_set_t), set) |
| 83 | #define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set) |
| 84 | #define CPU_EQUAL(set1, set2) CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 85 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 86 | #define CPU_AND(dst, set1, set2) __CPU_OP(dst, set1, set2, &) |
| 87 | #define CPU_OR(dst, set1, set2) __CPU_OP(dst, set1, set2, |) |
| 88 | #define CPU_XOR(dst, set1, set2) __CPU_OP(dst, set1, set2, ^) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 89 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 90 | #define __CPU_OP(dst, set1, set2, op) __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 91 | |
| 92 | /* Support for dynamically-allocated cpu_set_t */ |
| 93 | |
| 94 | #define CPU_ALLOC_SIZE(count) \ |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 95 | __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 96 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 97 | #define CPU_ALLOC(count) __sched_cpualloc((count)) |
| 98 | #define CPU_FREE(set) __sched_cpufree((set)) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 99 | |
| 100 | extern cpu_set_t* __sched_cpualloc(size_t count); |
| 101 | extern void __sched_cpufree(cpu_set_t* set); |
| 102 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 103 | #define CPU_ZERO_S(setsize, set) __builtin_memset(set, 0, setsize) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 104 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 105 | #define CPU_SET_S(cpu, setsize, set) \ |
| 106 | do { \ |
| 107 | size_t __cpu = (cpu); \ |
| 108 | if (__cpu < 8 * (setsize)) \ |
| 109 | (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \ |
| 110 | } while (0) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 111 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 112 | #define CPU_CLR_S(cpu, setsize, set) \ |
| 113 | do { \ |
| 114 | size_t __cpu = (cpu); \ |
| 115 | if (__cpu < 8 * (setsize)) \ |
| 116 | (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \ |
| 117 | } while (0) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 118 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 119 | #define CPU_ISSET_S(cpu, setsize, set) \ |
| 120 | (__extension__ ({ \ |
| 121 | size_t __cpu = (cpu); \ |
| 122 | (__cpu < 8 * (setsize)) \ |
| 123 | ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \ |
| 124 | : 0; \ |
| 125 | })) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 126 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 127 | #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] | 128 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 129 | #define CPU_AND_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, &) |
| 130 | #define CPU_OR_S(setsize, dst, set1, set2) __CPU_OP_S(setsize, dst, set1, set2, |) |
| 131 | #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] | 132 | |
Calin Juravle | b743790 | 2014-04-29 20:25:26 +0100 | [diff] [blame] | 133 | #define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \ |
| 134 | do { \ |
| 135 | cpu_set_t* __dst = (dstset); \ |
| 136 | const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \ |
| 137 | const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \ |
| 138 | size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \ |
| 139 | for (; __nn < __nn_max; __nn++) \ |
| 140 | (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \ |
| 141 | } while (0) |
| 142 | |
| 143 | #define CPU_COUNT_S(setsize, set) __sched_cpucount((setsize), (set)) |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 144 | |
| 145 | extern int __sched_cpucount(size_t setsize, cpu_set_t* set); |
| 146 | |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 147 | #endif /* __USE_GNU */ |
David 'Digit' Turner | 72e6fd4 | 2010-12-03 18:04:01 +0100 | [diff] [blame] | 148 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 149 | __END_DECLS |
| 150 | |
| 151 | #endif /* _SCHED_H_ */ |