]>
Commit | Line | Data |
---|---|---|
a334319f | 1 | /* Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
80f536db UD |
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> | |
439ff07b | 22 | #include <limits.h> |
80f536db UD |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
25 | #include <pthreadP.h> | |
439ff07b UD |
26 | #include <shlib-compat.h> |
27 | ||
28 | ||
29 | /* Defined in pthread_setaffinity.c. */ | |
a334319f | 30 | extern size_t __kernel_cpumask_size; |
439ff07b | 31 | extern int __determine_cpumask_size (pid_t tid); |
80f536db UD |
32 | |
33 | ||
34 | int | |
439ff07b UD |
35 | __pthread_attr_setaffinity_new (pthread_attr_t *attr, size_t cpusetsize, |
36 | const cpu_set_t *cpuset) | |
80f536db UD |
37 | { |
38 | struct pthread_attr *iattr; | |
39 | ||
40 | assert (sizeof (*attr) >= sizeof (struct pthread_attr)); | |
41 | iattr = (struct pthread_attr *) attr; | |
42 | ||
439ff07b | 43 | if (cpuset == NULL || cpusetsize == 0) |
80f536db | 44 | { |
9ba96eda UD |
45 | free (iattr->cpuset); |
46 | iattr->cpuset = NULL; | |
439ff07b | 47 | iattr->cpusetsize = 0; |
80f536db | 48 | } |
9ba96eda UD |
49 | else |
50 | { | |
439ff07b UD |
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) | |
9ba96eda | 68 | { |
439ff07b UD |
69 | void *newp = (cpu_set_t *) realloc (iattr->cpuset, cpusetsize); |
70 | if (newp == NULL) | |
9ba96eda | 71 | return ENOMEM; |
439ff07b UD |
72 | |
73 | iattr->cpuset = newp; | |
74 | iattr->cpusetsize = cpusetsize; | |
9ba96eda | 75 | } |
80f536db | 76 | |
439ff07b | 77 | memcpy (iattr->cpuset, cpuset, cpusetsize); |
9ba96eda | 78 | } |
80f536db UD |
79 | |
80 | return 0; | |
81 | } | |
439ff07b UD |
82 | versioned_symbol (libpthread, __pthread_attr_setaffinity_new, |
83 | pthread_attr_setaffinity_np, GLIBC_2_3_4); | |
84 | ||
85 | ||
8e32efa6 | 86 | #if SHLIB_COMPAT (libpthread, GLIBC_2_3_3, GLIBC_2_3_4) |
439ff07b | 87 | int |
63f78a3a | 88 | __pthread_attr_setaffinity_old (pthread_attr_t *attr, cpu_set_t *cpuset) |
439ff07b UD |
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 |