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