11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions in binary form must reproduce the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    the documentation and/or other materials provided with the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef _SCHED_H_
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _SCHED_H_
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
31887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughes#include <linux/sched.h>
32db4990373232182a897e10663344c429f5d1b2d5Yabin Cui#include <machine/timespec.h>
33db4990373232182a897e10663344c429f5d1b2d5Yabin Cui#include <sys/cdefs.h>
34887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughes
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project__BEGIN_DECLS
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
37887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughes/* This name is used by glibc, but not by the kernel. */
38887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughes#define SCHED_OTHER SCHED_NORMAL
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstruct sched_param {
41f6640348876cd1d4d43cc95cf63306fedb83d8b9Dan Albert  int sched_priority;
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project};
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
44887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int sched_setscheduler(pid_t, int, const struct sched_param*);
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectextern int sched_getscheduler(pid_t);
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectextern int sched_yield(void);
47887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int sched_get_priority_max(int);
48887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int sched_get_priority_min(int);
49887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int sched_setparam(pid_t, const struct sched_param*);
50887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int sched_getparam(pid_t, struct sched_param*);
51887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int sched_rr_get_interval(pid_t, struct timespec*);
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
535f5cc45cf0e027f6ca503dc229a4890fc7164b66Elliott Hughes#if defined(__USE_GNU)
5472e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
55887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int clone(int (*)(void*), void*, int, void*, ...);
56887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int unshare(int);
5772e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnerextern int sched_getcpu(void);
58887e1140fe7596fa1239a0bc802d2cdb51c57d3cElliott Hughesextern int setns(int, int);
5972e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
60dd09699fae94e1d9d326c78cc6aa769701219bc7Calin Juravle#ifdef __LP64__
61b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_SETSIZE 1024
62dd09699fae94e1d9d326c78cc6aa769701219bc7Calin Juravle#else
63dd09699fae94e1d9d326c78cc6aa769701219bc7Calin Juravle#define CPU_SETSIZE 32
64b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#endif
6572e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
66b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define __CPU_BITTYPE  unsigned long int  /* mandated by the kernel  */
67b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define __CPU_BITS     (8 * sizeof(__CPU_BITTYPE))
68b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define __CPU_ELT(x)   ((x) / __CPU_BITS)
69b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define __CPU_MASK(x)  ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1)))
7072e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
7172e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnertypedef struct {
72b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  __CPU_BITTYPE  __bits[ CPU_SETSIZE / __CPU_BITS ];
7372e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner} cpu_set_t;
7472e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
7572e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnerextern int sched_setaffinity(pid_t pid, size_t setsize, const cpu_set_t* set);
7672e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
7772e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnerextern int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t* set);
7872e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
79b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_ZERO(set)          CPU_ZERO_S(sizeof(cpu_set_t), set)
80b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_SET(cpu, set)      CPU_SET_S(cpu, sizeof(cpu_set_t), set)
81b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_CLR(cpu, set)      CPU_CLR_S(cpu, sizeof(cpu_set_t), set)
82b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_ISSET(cpu, set)    CPU_ISSET_S(cpu, sizeof(cpu_set_t), set)
83b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_COUNT(set)         CPU_COUNT_S(sizeof(cpu_set_t), set)
84b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_EQUAL(set1, set2)  CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2)
8572e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
86b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_AND(dst, set1, set2)  __CPU_OP(dst, set1, set2, &)
87b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_OR(dst, set1, set2)   __CPU_OP(dst, set1, set2, |)
88b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_XOR(dst, set1, set2)  __CPU_OP(dst, set1, set2, ^)
8972e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
90b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define __CPU_OP(dst, set1, set2, op)  __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op)
9172e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
9272e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner/* Support for dynamically-allocated cpu_set_t */
9372e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
9472e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner#define CPU_ALLOC_SIZE(count) \
95b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE)
9672e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
97b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_ALLOC(count)  __sched_cpualloc((count))
98b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_FREE(set)     __sched_cpufree((set))
9972e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
10072e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnerextern cpu_set_t* __sched_cpualloc(size_t count);
10172e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnerextern void       __sched_cpufree(cpu_set_t* set);
10272e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
103b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_ZERO_S(setsize, set)  __builtin_memset(set, 0, setsize)
104b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
105b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_SET_S(cpu, setsize, set) \
106b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  do { \
107b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    size_t __cpu = (cpu); \
108b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    if (__cpu < 8 * (setsize)) \
109b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle      (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
110b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  } while (0)
111b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
112b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_CLR_S(cpu, setsize, set) \
113b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  do { \
114b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    size_t __cpu = (cpu); \
115b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    if (__cpu < 8 * (setsize)) \
116b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle      (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
117b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  } while (0)
118b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
119b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_ISSET_S(cpu, setsize, set) \
120b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  (__extension__ ({ \
121b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    size_t __cpu = (cpu); \
122b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    (__cpu < 8 * (setsize)) \
123b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle      ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
124b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle      : 0; \
125b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }))
126b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
127b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_EQUAL_S(setsize, set1, set2)  (__builtin_memcmp(set1, set2, setsize) == 0)
128b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
129b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_AND_S(setsize, dst, set1, set2)  __CPU_OP_S(setsize, dst, set1, set2, &)
130b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_OR_S(setsize, dst, set1, set2)   __CPU_OP_S(setsize, dst, set1, set2, |)
131b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_XOR_S(setsize, dst, set1, set2)  __CPU_OP_S(setsize, dst, set1, set2, ^)
132b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
133b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \
134b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  do { \
135b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    cpu_set_t* __dst = (dstset); \
136b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
137b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
138b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \
139b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    for (; __nn < __nn_max; __nn++) \
140b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle      (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
141b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  } while (0)
142b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
143b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle#define CPU_COUNT_S(setsize, set)  __sched_cpucount((setsize), (set))
14472e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
14572e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turnerextern int __sched_cpucount(size_t setsize, cpu_set_t* set);
14672e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
1475f5cc45cf0e027f6ca503dc229a4890fc7164b66Elliott Hughes#endif /* __USE_GNU */
14872e6fd42421dca80fb2776a9185c186d4a04e5f7David 'Digit' Turner
1491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project__END_DECLS
1501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* _SCHED_H_ */
152