chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nptl / sysdeps / unix / sysv / linux / pthread_attr_setaffinity.c
1 /* Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <pthreadP.h>
26 #include <shlib-compat.h>
27
28
29 /* Defined in pthread_setaffinity.c.  */
30 extern size_t __kernel_cpumask_size attribute_hidden;
31 extern int __determine_cpumask_size (pid_t tid);
32
33
34 int
35 __pthread_attr_setaffinity_new (pthread_attr_t *attr, size_t cpusetsize,
36                                 const cpu_set_t *cpuset)
37 {
38   struct pthread_attr *iattr;
39
40   assert (sizeof (*attr) >= sizeof (struct pthread_attr));
41   iattr = (struct pthread_attr *) attr;
42
43   if (cpuset == NULL || cpusetsize == 0)
44     {
45       free (iattr->cpuset);
46       iattr->cpuset = NULL;
47       iattr->cpusetsize = 0;
48     }
49   else
50     {
51       if (__kernel_cpumask_size == 0)
52         {
53           int res = __determine_cpumask_size (THREAD_SELF->tid);
54           if (res != 0)
55             /* Some serious problem.  */
56             return res;
57         }
58
59       /* Check whether the new bitmask has any bit set beyond the
60          last one the kernel accepts.  */
61       for (size_t cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt)
62         if (((char *) cpuset)[cnt] != '\0')
63           /* Found a nonzero byte.  This means the user request cannot be
64              fulfilled.  */
65           return EINVAL;
66
67       if (iattr->cpusetsize != cpusetsize)
68         {
69           void *newp = (cpu_set_t *) realloc (iattr->cpuset, cpusetsize);
70           if (newp == NULL)
71             return ENOMEM;
72
73           iattr->cpuset = newp;
74           iattr->cpusetsize = cpusetsize;
75         }
76
77       memcpy (iattr->cpuset, cpuset, cpusetsize);
78     }
79
80   return 0;
81 }
82 versioned_symbol (libpthread, __pthread_attr_setaffinity_new,
83                   pthread_attr_setaffinity_np, GLIBC_2_3_4);
84
85
86 #if SHLIB_COMPAT (libpthread, GLIBC_2_3_3, GLIBC_2_3_4)
87 int
88 __pthread_attr_setaffinity_old (pthread_attr_t *attr, cpu_set_t *cpuset)
89 {
90   /* The old interface by default assumed a 1024 processor bitmap.  */
91   return __pthread_attr_setaffinity_new (attr, 128, cpuset);
92 }
93 compat_symbol (libpthread, __pthread_attr_setaffinity_old,
94                pthread_attr_setaffinity_np, GLIBC_2_3_3);
95 #endif