8a984d
nptl: Add __pthread_attr_copy for copying pthread_attr_t objects
8a984d
8a984d
Also add the private type union pthread_attr_transparent, to reduce
8a984d
the amount of casting that is required.
8a984d
8a984d
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
8a984d
Tested-by: Carlos O'Donell <carlos@redhat.com>
8a984d
(cherry picked from commit 331c6e8a184167dd21a9f0b3fc165aeefea6eeca)
8a984d
8a984d
Difference from upstream:
8a984d
Unlike upstream, __pthread_attr_copy is in libpthread.so.
8a984d
8a984d
# Conflicts:
8a984d
#	nptl/Makefile
8a984d
#	nptl/Versions
8a984d
8a984d
diff --git a/nptl/Makefile b/nptl/Makefile
8a984d
index d6b37b6efd3b7d78..b14de3ffb330c10b 100644
8a984d
--- a/nptl/Makefile
8a984d
+++ b/nptl/Makefile
8a984d
@@ -54,7 +54,8 @@ libpthread-routines = nptl-init nptlfreeres vars events version pt-interp \
8a984d
 		      pthread_getconcurrency pthread_setconcurrency \
8a984d
 		      pthread_getschedparam pthread_setschedparam \
8a984d
 		      pthread_setschedprio \
8a984d
-		      pthread_attr_init pthread_attr_destroy \
8a984d
+		      pthread_attr_init pthread_attr_copy \
8a984d
+		      pthread_attr_destroy \
8a984d
 		      pthread_attr_getdetachstate pthread_attr_setdetachstate \
8a984d
 		      pthread_attr_getguardsize pthread_attr_setguardsize \
8a984d
 		      pthread_attr_getschedparam pthread_attr_setschedparam \
8a984d
diff --git a/nptl/Versions b/nptl/Versions
8a984d
index 6007fd03e7ed117c..e38272aa187fbe78 100644
8a984d
--- a/nptl/Versions
8a984d
+++ b/nptl/Versions
8a984d
@@ -283,5 +283,6 @@ libpthread {
8a984d
     __pthread_barrier_init; __pthread_barrier_wait;
8a984d
     __shm_directory;
8a984d
     __libpthread_freeres;
8a984d
+    __pthread_attr_copy;
8a984d
   }
8a984d
 }
8a984d
diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h
8a984d
index 00be8f92793e8710..a2d48b2015cd385c 100644
8a984d
--- a/nptl/pthreadP.h
8a984d
+++ b/nptl/pthreadP.h
8a984d
@@ -464,6 +464,9 @@ extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
8a984d
 				    size_t *__restrict __stacksize);
8a984d
 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
8a984d
 				    size_t __stacksize);
8a984d
+extern int __pthread_attr_setaffinity_np (pthread_attr_t *attr,
8a984d
+					  size_t cpusetsize,
8a984d
+					  const cpu_set_t *cpuset);
8a984d
 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
8a984d
 				  const pthread_rwlockattr_t *__restrict
8a984d
 				  __attr);
8a984d
@@ -605,6 +608,11 @@ extern void __wait_lookup_done (void) attribute_hidden;
8a984d
 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
8a984d
 #endif
8a984d
 
8a984d
+/* Make a deep copy of the attribute *SOURCE in *TARGET.  *TARGET is
8a984d
+   not assumed to have been initialized.  Returns 0 on success, or a
8a984d
+   positive error code otherwise.  */
8a984d
+int __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source);
8a984d
+
8a984d
 /* Returns 0 if POL is a valid scheduling policy.  */
8a984d
 static inline int
8a984d
 check_sched_policy_attr (int pol)
8a984d
diff --git a/nptl/pthread_attr_copy.c b/nptl/pthread_attr_copy.c
8a984d
new file mode 100644
8a984d
index 0000000000000000..67f272acf297100c
8a984d
--- /dev/null
8a984d
+++ b/nptl/pthread_attr_copy.c
8a984d
@@ -0,0 +1,56 @@
8a984d
+/* Deep copy of a pthread_attr_t object.
8a984d
+   Copyright (C) 2020 Free Software Foundation, Inc.
8a984d
+   This file is part of the GNU C Library.
8a984d
+
8a984d
+   The GNU C Library is free software; you can redistribute it and/or
8a984d
+   modify it under the terms of the GNU Lesser General Public
8a984d
+   License as published by the Free Software Foundation; either
8a984d
+   version 2.1 of the License, or (at your option) any later version.
8a984d
+
8a984d
+   The GNU C Library is distributed in the hope that it will be useful,
8a984d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8a984d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8a984d
+   Lesser General Public License for more details.
8a984d
+
8a984d
+   You should have received a copy of the GNU Lesser General Public
8a984d
+   License along with the GNU C Library; if not, see
8a984d
+   <https://www.gnu.org/licenses/>.  */
8a984d
+
8a984d
+#include <errno.h>
8a984d
+#include <pthreadP.h>
8a984d
+#include <stdlib.h>
8a984d
+
8a984d
+int
8a984d
+__pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source)
8a984d
+{
8a984d
+  /* Avoid overwriting *TARGET until all allocations have
8a984d
+     succeeded.  */
8a984d
+  union pthread_attr_transparent temp;
8a984d
+  temp.external = *source;
8a984d
+
8a984d
+  /* Force new allocation.  This function has full ownership of temp.  */
8a984d
+  temp.internal.cpuset = NULL;
8a984d
+  temp.internal.cpusetsize = 0;
8a984d
+
8a984d
+  int ret = 0;
8a984d
+
8a984d
+  struct pthread_attr *isource = (struct pthread_attr *) source;
8a984d
+
8a984d
+  /* Propagate affinity mask information.  */
8a984d
+  if (isource->cpusetsize > 0)
8a984d
+    ret = __pthread_attr_setaffinity_np (&temp.external,
8a984d
+					 isource->cpusetsize,
8a984d
+					 isource->cpuset);
8a984d
+
8a984d
+  if (ret != 0)
8a984d
+    {
8a984d
+      /* Deallocate because we have ownership.  */
8a984d
+      __pthread_attr_destroy (&temp.external);
8a984d
+      return ret;
8a984d
+    }
8a984d
+
8a984d
+  /* Transfer ownership.  *target is not assumed to have been
8a984d
+     initialized.  */
8a984d
+  *target = temp.external;
8a984d
+  return 0;
8a984d
+}
8a984d
diff --git a/nptl/pthread_attr_setaffinity.c b/nptl/pthread_attr_setaffinity.c
8a984d
index 545b72c91e290216..914ebf6f9cbfd5ff 100644
8a984d
--- a/nptl/pthread_attr_setaffinity.c
8a984d
+++ b/nptl/pthread_attr_setaffinity.c
8a984d
@@ -55,6 +55,7 @@ __pthread_attr_setaffinity_new (pthread_attr_t *attr, size_t cpusetsize,
8a984d
 
8a984d
   return 0;
8a984d
 }
8a984d
+strong_alias (__pthread_attr_setaffinity_new, __pthread_attr_setaffinity_np)
8a984d
 versioned_symbol (libpthread, __pthread_attr_setaffinity_new,
8a984d
 		  pthread_attr_setaffinity_np, GLIBC_2_3_4);
8a984d
 
8a984d
diff --git a/sysdeps/nptl/internaltypes.h b/sysdeps/nptl/internaltypes.h
8a984d
index b78ad99a888b4e3b..d3dce1278de989e2 100644
8a984d
--- a/sysdeps/nptl/internaltypes.h
8a984d
+++ b/sysdeps/nptl/internaltypes.h
8a984d
@@ -49,6 +49,13 @@ struct pthread_attr
8a984d
 #define ATTR_FLAG_SCHED_SET		0x0020
8a984d
 #define ATTR_FLAG_POLICY_SET		0x0040
8a984d
 
8a984d
+/* Used to allocate a pthread_attr_t object which is also accessed
8a984d
+   internally.  */
8a984d
+union pthread_attr_transparent
8a984d
+{
8a984d
+  pthread_attr_t external;
8a984d
+  struct pthread_attr internal;
8a984d
+};
8a984d
 
8a984d
 /* Mutex attribute data structure.  */
8a984d
 struct pthread_mutexattr