Home | History | Annotate | Download | only in include
      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 
     29 #ifndef _SCHED_H_
     30 #define _SCHED_H_
     31 
     32 #include <bits/timespec.h>
     33 #include <linux/sched.h>
     34 #include <sys/cdefs.h>
     35 
     36 __BEGIN_DECLS
     37 
     38 /* This name is used by glibc, but not by the kernel. */
     39 #define SCHED_OTHER SCHED_NORMAL
     40 
     41 struct sched_param {
     42   int sched_priority;
     43 };
     44 
     45 int sched_setscheduler(pid_t __pid, int __policy, const struct sched_param* __param);
     46 int sched_getscheduler(pid_t __pid);
     47 int sched_yield(void);
     48 int sched_get_priority_max(int __policy);
     49 int sched_get_priority_min(int __policy);
     50 int sched_setparam(pid_t __pid, const struct sched_param* __param);
     51 int sched_getparam(pid_t __pid, struct sched_param* __param);
     52 int sched_rr_get_interval(pid_t __pid, struct timespec* __quantum);
     53 
     54 #if defined(__USE_GNU)
     55 
     56 int clone(int (*__fn)(void*), void* __child_stack, int __flags, void* __arg, ...)
     57     __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_MIPS(12) __INTRODUCED_IN_X86(17);
     58 int unshare(int __flags) __INTRODUCED_IN(17);
     59 int sched_getcpu(void) __INTRODUCED_IN(12);
     60 int setns(int __fd, int __ns_type) __INTRODUCED_IN(21);
     61 
     62 #ifdef __LP64__
     63 #define CPU_SETSIZE 1024
     64 #else
     65 #define CPU_SETSIZE 32
     66 #endif
     67 
     68 #define __CPU_BITTYPE  unsigned long int  /* mandated by the kernel  */
     69 #define __CPU_BITS     (8 * sizeof(__CPU_BITTYPE))
     70 #define __CPU_ELT(x)   ((x) / __CPU_BITS)
     71 #define __CPU_MASK(x)  ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS - 1)))
     72 
     73 typedef struct {
     74   __CPU_BITTYPE  __bits[ CPU_SETSIZE / __CPU_BITS ];
     75 } cpu_set_t;
     76 
     77 int sched_setaffinity(pid_t __pid, size_t __set_size, const cpu_set_t* __set) __INTRODUCED_IN(12);
     78 int sched_getaffinity(pid_t __pid, size_t __set_size, cpu_set_t* __set) __INTRODUCED_IN(12);
     79 
     80 #define CPU_ZERO(set)          CPU_ZERO_S(sizeof(cpu_set_t), set)
     81 #define CPU_SET(cpu, set)      CPU_SET_S(cpu, sizeof(cpu_set_t), set)
     82 #define CPU_CLR(cpu, set)      CPU_CLR_S(cpu, sizeof(cpu_set_t), set)
     83 #define CPU_ISSET(cpu, set)    CPU_ISSET_S(cpu, sizeof(cpu_set_t), set)
     84 #define CPU_COUNT(set)         CPU_COUNT_S(sizeof(cpu_set_t), set)
     85 #define CPU_EQUAL(set1, set2)  CPU_EQUAL_S(sizeof(cpu_set_t), set1, set2)
     86 
     87 #define CPU_AND(dst, set1, set2)  __CPU_OP(dst, set1, set2, &)
     88 #define CPU_OR(dst, set1, set2)   __CPU_OP(dst, set1, set2, |)
     89 #define CPU_XOR(dst, set1, set2)  __CPU_OP(dst, set1, set2, ^)
     90 
     91 #define __CPU_OP(dst, set1, set2, op)  __CPU_OP_S(sizeof(cpu_set_t), dst, set1, set2, op)
     92 
     93 /* Support for dynamically-allocated cpu_set_t */
     94 
     95 #define CPU_ALLOC_SIZE(count) \
     96   __CPU_ELT((count) + (__CPU_BITS - 1)) * sizeof(__CPU_BITTYPE)
     97 
     98 #define CPU_ALLOC(count)  __sched_cpualloc((count))
     99 #define CPU_FREE(set)     __sched_cpufree((set))
    100 
    101 cpu_set_t* __sched_cpualloc(size_t __count) __INTRODUCED_IN(12);
    102 void __sched_cpufree(cpu_set_t* __set) __INTRODUCED_IN(12);
    103 
    104 #define CPU_ZERO_S(setsize, set)  __builtin_memset(set, 0, setsize)
    105 
    106 #define CPU_SET_S(cpu, setsize, set) \
    107   do { \
    108     size_t __cpu = (cpu); \
    109     if (__cpu < 8 * (setsize)) \
    110       (set)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
    111   } while (0)
    112 
    113 #define CPU_CLR_S(cpu, setsize, set) \
    114   do { \
    115     size_t __cpu = (cpu); \
    116     if (__cpu < 8 * (setsize)) \
    117       (set)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
    118   } while (0)
    119 
    120 #define CPU_ISSET_S(cpu, setsize, set) \
    121   (__extension__ ({ \
    122     size_t __cpu = (cpu); \
    123     (__cpu < 8 * (setsize)) \
    124       ? ((set)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
    125       : 0; \
    126   }))
    127 
    128 #define CPU_EQUAL_S(setsize, set1, set2)  (__builtin_memcmp(set1, set2, setsize) == 0)
    129 
    130 #define CPU_AND_S(setsize, dst, set1, set2)  __CPU_OP_S(setsize, dst, set1, set2, &)
    131 #define CPU_OR_S(setsize, dst, set1, set2)   __CPU_OP_S(setsize, dst, set1, set2, |)
    132 #define CPU_XOR_S(setsize, dst, set1, set2)  __CPU_OP_S(setsize, dst, set1, set2, ^)
    133 
    134 #define __CPU_OP_S(setsize, dstset, srcset1, srcset2, op) \
    135   do { \
    136     cpu_set_t* __dst = (dstset); \
    137     const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
    138     const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
    139     size_t __nn = 0, __nn_max = (setsize)/sizeof(__CPU_BITTYPE); \
    140     for (; __nn < __nn_max; __nn++) \
    141       (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
    142   } while (0)
    143 
    144 #define CPU_COUNT_S(setsize, set)  __sched_cpucount((setsize), (set))
    145 
    146 int __sched_cpucount(size_t __set_size, const cpu_set_t* __set) __INTRODUCED_IN(12);
    147 
    148 #endif /* __USE_GNU */
    149 
    150 __END_DECLS
    151 
    152 #endif /* _SCHED_H_ */
    153