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