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