]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/pthread_attr_copy.c
nptl: Add __pthread_attr_copy for copying pthread_attr_t objects
[thirdparty/glibc.git] / nptl / pthread_attr_copy.c
1 /* Deep copy of a pthread_attr_t object.
2 Copyright (C) 2020 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 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <pthreadP.h>
21 #include <stdlib.h>
22
23 int
24 __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source)
25 {
26 /* Avoid overwriting *TARGET until all allocations have
27 succeeded. */
28 union pthread_attr_transparent temp;
29 temp.external = *source;
30
31 /* Force new allocation. This function has full ownership of temp. */
32 temp.internal.cpuset = NULL;
33 temp.internal.cpusetsize = 0;
34
35 int ret = 0;
36
37 struct pthread_attr *isource = (struct pthread_attr *) source;
38
39 /* Propagate affinity mask information. */
40 if (isource->cpusetsize > 0)
41 ret = __pthread_attr_setaffinity_np (&temp.external,
42 isource->cpusetsize,
43 isource->cpuset);
44
45 if (ret != 0)
46 {
47 /* Deallocate because we have ownership. */
48 __pthread_attr_destroy (&temp.external);
49 return ret;
50 }
51
52 /* Transfer ownership. *target is not assumed to have been
53 initialized. */
54 *target = temp.external;
55 return 0;
56 }
57 libc_hidden_def (__pthread_attr_copy)