2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / pthread_attr_setaffinity.c
blob355e695ec29b349388568f398cba376c36cba18d
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.
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.
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.
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. */
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>
29 /* Defined in pthread_setaffinity.c. */
30 extern size_t __kernel_cpumask_size attribute_hidden;
31 extern int __determine_cpumask_size (pid_t tid);
34 int
35 __pthread_attr_setaffinity_new (pthread_attr_t *attr, size_t cpusetsize,
36 const cpu_set_t *cpuset)
38 struct pthread_attr *iattr;
40 assert (sizeof (*attr) >= sizeof (struct pthread_attr));
41 iattr = (struct pthread_attr *) attr;
43 if (cpuset == NULL || cpusetsize == 0)
45 free (iattr->cpuset);
46 iattr->cpuset = NULL;
47 iattr->cpusetsize = 0;
49 else
51 if (__kernel_cpumask_size == 0)
53 int res = __determine_cpumask_size (THREAD_SELF->tid);
54 if (res != 0)
55 /* Some serious problem. */
56 return res;
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;
67 if (iattr->cpusetsize != cpusetsize)
69 void *newp = (cpu_set_t *) realloc (iattr->cpuset, cpusetsize);
70 if (newp == NULL)
71 return ENOMEM;
73 iattr->cpuset = newp;
74 iattr->cpusetsize = cpusetsize;
77 memcpy (iattr->cpuset, cpuset, cpusetsize);
80 return 0;
82 versioned_symbol (libpthread, __pthread_attr_setaffinity_new,
83 pthread_attr_setaffinity_np, GLIBC_2_3_4);
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)
90 /* The old interface by default assumed a 1024 processor bitmap. */
91 return __pthread_attr_setaffinity_new (attr, 128, cpuset);
93 compat_symbol (libpthread, __pthread_attr_setaffinity_old,
94 pthread_attr_setaffinity_np, GLIBC_2_3_3);
95 #endif