]>
Commit | Line | Data |
---|---|---|
61dd6208 | 1 | /* Set the default attributes to be used by pthread_create in the process. |
bfff8b1b | 2 | Copyright (C) 2013-2017 Free Software Foundation, Inc. |
61dd6208 SP |
3 | This file is part of the GNU C Library. |
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, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <errno.h> | |
20 | #include <stdlib.h> | |
21 | #include <pthreadP.h> | |
22 | #include <assert.h> | |
e781d7c5 | 23 | #include <string.h> |
59ff172f | 24 | |
61dd6208 SP |
25 | |
26 | int | |
27 | pthread_setattr_default_np (const pthread_attr_t *in) | |
28 | { | |
29 | const struct pthread_attr *real_in; | |
30 | struct pthread_attr attrs; | |
31 | int ret; | |
32 | ||
33 | assert (sizeof (*in) >= sizeof (struct pthread_attr)); | |
34 | real_in = (struct pthread_attr *) in; | |
35 | ||
36 | /* Catch invalid values. */ | |
37 | int policy = real_in->schedpolicy; | |
38 | ret = check_sched_policy_attr (policy); | |
39 | if (ret) | |
40 | return ret; | |
41 | ||
42 | const struct sched_param *param = &real_in->schedparam; | |
43 | if (param->sched_priority > 0) | |
44 | { | |
45 | ret = check_sched_priority_attr (param->sched_priority, policy); | |
46 | if (ret) | |
47 | return ret; | |
48 | } | |
49 | ||
61dd6208 SP |
50 | /* stacksize == 0 is fine. It means that we don't change the current |
51 | value. */ | |
52 | if (real_in->stacksize != 0) | |
53 | { | |
54 | ret = check_stacksize_attr (real_in->stacksize); | |
55 | if (ret) | |
56 | return ret; | |
57 | } | |
58 | ||
59 | /* Having a default stack address is wrong. */ | |
60 | if (real_in->flags & ATTR_FLAG_STACKADDR) | |
61 | return EINVAL; | |
62 | ||
63 | attrs = *real_in; | |
64 | ||
65 | /* Now take the lock because we start writing into | |
66 | __default_pthread_attr. */ | |
67 | lll_lock (__default_pthread_attr_lock, LLL_PRIVATE); | |
68 | ||
69 | /* Free the cpuset if the input is 0. Otherwise copy in the cpuset | |
70 | contents. */ | |
71 | size_t cpusetsize = attrs.cpusetsize; | |
72 | if (cpusetsize == 0) | |
73 | { | |
74 | free (__default_pthread_attr.cpuset); | |
75 | __default_pthread_attr.cpuset = NULL; | |
76 | } | |
77 | else if (cpusetsize == __default_pthread_attr.cpusetsize) | |
78 | { | |
79 | attrs.cpuset = __default_pthread_attr.cpuset; | |
80 | memcpy (attrs.cpuset, real_in->cpuset, cpusetsize); | |
81 | } | |
82 | else | |
83 | { | |
84 | /* This may look wrong at first sight, but it isn't. We're freeing | |
85 | __default_pthread_attr.cpuset and allocating to attrs.cpuset because | |
86 | we'll copy over all of attr to __default_pthread_attr later. */ | |
87 | cpu_set_t *newp = realloc (__default_pthread_attr.cpuset, | |
88 | cpusetsize); | |
89 | ||
90 | if (newp == NULL) | |
91 | { | |
92 | ret = ENOMEM; | |
93 | goto out; | |
94 | } | |
95 | ||
96 | attrs.cpuset = newp; | |
97 | memcpy (attrs.cpuset, real_in->cpuset, cpusetsize); | |
98 | } | |
99 | ||
100 | /* We don't want to accidentally set the default stacksize to zero. */ | |
101 | if (attrs.stacksize == 0) | |
102 | attrs.stacksize = __default_pthread_attr.stacksize; | |
103 | __default_pthread_attr = attrs; | |
104 | ||
105 | out: | |
106 | lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE); | |
107 | return ret; | |
108 | } |