OSDN Git Service

a25fbe356c4f5eb8a337ed32800ad943b0b7815f
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / sched_setaffinity.c
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <features.h>
20 #ifdef __USE_GNU
21
22 #include <sched.h>
23 #include <sys/types.h>
24 #include <sys/syscall.h>
25
26 #ifdef INTERNAL_SYSCALL /* remove this when all archs has this #defined */
27
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <alloca.h>
33
34 libc_hidden_proto(getpid)
35
36 #define __NR___syscall_sched_setaffinity __NR_sched_setaffinity
37 static inline _syscall3(int, __syscall_sched_setaffinity, __kernel_pid_t, pid,
38                         size_t, cpusetsize, cpu_set_t *, cpuset);
39
40 static size_t __kernel_cpumask_size;
41
42 int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *cpuset)
43 {
44         size_t cnt;
45         if (unlikely (__kernel_cpumask_size == 0)) {
46                 INTERNAL_SYSCALL_DECL (err);
47                 int res;
48                 size_t psize = 128;
49                 void *p = alloca (psize);
50
51                 while (res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, getpid (),
52                                                psize, p),
53                        INTERNAL_SYSCALL_ERROR_P (res, err)
54                        && INTERNAL_SYSCALL_ERRNO (res, err) == EINVAL)
55                         p = extend_alloca (p, psize, 2 * psize);
56
57                 if (res == 0 || INTERNAL_SYSCALL_ERROR_P (res, err)) {
58                         __set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
59                         return -1;
60                 }
61
62                 __kernel_cpumask_size = res;
63         }
64
65         /* We now know the size of the kernel cpumask_t.  Make sure the user
66            does not request to set a bit beyond that.  */
67         for (cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
68                 if (((char *) cpuset)[cnt] != '\0') {
69                         /* Found a nonzero byte.  This means the user request cannot be
70                            fulfilled.  */
71                         __set_errno (EINVAL);
72                         return -1;
73                 }
74
75         return INLINE_SYSCALL (sched_setaffinity, 3, pid, cpusetsize, cpuset);
76 }
77 #endif
78 #endif