public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: libc-alpha@sourceware.org
Subject: [PATCH 07/19] nptl: Add __pthread_attr_copy for copying pthread_attr_t objects
Date: Tue, 19 May 2020 12:44:28 +0200	[thread overview]
Message-ID: <653128d1dee168e40595a2228e0100dcad3ac2fe.1589884403.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1589884403.git.fweimer@redhat.com>

Also add the private type union pthread_attr_transparent, to reduce
the amount of casting that is required.
---
 nptl/Makefile                |  1 +
 nptl/Versions                |  1 +
 nptl/pthreadP.h              |  6 ++++
 nptl/pthread_attr_copy.c     | 57 ++++++++++++++++++++++++++++++++++++
 sysdeps/nptl/internaltypes.h |  7 +++++
 5 files changed, 72 insertions(+)
 create mode 100644 nptl/pthread_attr_copy.c

diff --git a/nptl/Makefile b/nptl/Makefile
index 0dd8800f19..b4aaad0f20 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -39,6 +39,7 @@ routines = \
   old_pthread_cond_destroy \
   old_pthread_cond_init \
   pthread_atfork \
+  pthread_attr_copy \
   pthread_attr_destroy \
   pthread_attr_getdetachstate \
   pthread_attr_getinheritsched \
diff --git a/nptl/Versions b/nptl/Versions
index 65f0c077da..4c7b43354d 100644
--- a/nptl/Versions
+++ b/nptl/Versions
@@ -60,6 +60,7 @@ libc {
     __pthread_cond_destroy; __pthread_cond_init;
     __pthread_attr_setaffinity_np;
     __pthread_attr_init; __pthread_attr_destroy;
+    __pthread_attr_copy;
   }
 }
 
diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h
index f218b01574..41b693d034 100644
--- a/nptl/pthreadP.h
+++ b/nptl/pthreadP.h
@@ -580,6 +580,12 @@ extern void __wait_lookup_done (void) attribute_hidden;
 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
 #endif
 
+/* Make a deep copy of the attribute *SOURCE in *TARGET.  *TARGET is
+   not assumed to have been initialized.  Returns 0 on success, or a
+   positive error code otherwise.  */
+int __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source);
+libc_hidden_proto (__pthread_attr_copy)
+
 /* Returns 0 if POL is a valid scheduling policy.  */
 static inline int
 check_sched_policy_attr (int pol)
diff --git a/nptl/pthread_attr_copy.c b/nptl/pthread_attr_copy.c
new file mode 100644
index 0000000000..77a1a43eeb
--- /dev/null
+++ b/nptl/pthread_attr_copy.c
@@ -0,0 +1,57 @@
+/* Deep copy of a pthread_attr_t object.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <pthreadP.h>
+#include <stdlib.h>
+
+int
+__pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source)
+{
+  /* Avoid overwriting *TARGET until all allocations have
+     succeeded.  */
+  union pthread_attr_transparent temp;
+  temp.external = *source;
+
+  /* Force new allocation.  This function has full ownership of temp.  */
+  temp.internal.cpuset = NULL;
+  temp.internal.cpusetsize = 0;
+
+  int ret = 0;
+
+  struct pthread_attr *isource = (struct pthread_attr *) source;
+
+  /* Propagate affinity mask information.  */
+  if (isource->cpusetsize > 0)
+    ret = __pthread_attr_setaffinity_np (&temp.external,
+                                         isource->cpusetsize,
+                                         isource->cpuset);
+
+  if (ret != 0)
+    {
+      /* Deallocate because we have ownership.  */
+      __pthread_attr_destroy (&temp.external);
+      return ret;
+    }
+
+  /* Transfer ownership.  *target is not assumed to have been
+     initialized.  */
+  *target = temp.external;
+  return 0;
+}
+libc_hidden_def (__pthread_attr_copy)
diff --git a/sysdeps/nptl/internaltypes.h b/sysdeps/nptl/internaltypes.h
index 71f09b4624..6d06a76baf 100644
--- a/sysdeps/nptl/internaltypes.h
+++ b/sysdeps/nptl/internaltypes.h
@@ -49,6 +49,13 @@ struct pthread_attr
 #define ATTR_FLAG_SCHED_SET		0x0020
 #define ATTR_FLAG_POLICY_SET		0x0040
 
+/* Used to allocate a pthread_attr_t object which is also accessed
+   internally.  */
+union pthread_attr_transparent
+{
+  pthread_attr_t external;
+  struct pthread_attr internal;
+};
 
 /* Mutex attribute data structure.  */
 struct pthread_mutexattr
-- 
2.25.4



  parent reply	other threads:[~2020-05-19 10:44 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-19 10:43 [PATCH 00/19] Signal mask for timer helper thread Florian Weimer
2020-05-19 10:44 ` [PATCH 01/19] manual: Add missing section and node for clockid_t wait functions Florian Weimer
2020-05-20 13:12   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 02/19] nptl: Replace some stubs with the Linux implementation Florian Weimer
2020-05-20 13:27   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 03/19] nptl: Move pthread_attr_setaffinity_np into libc Florian Weimer
2020-05-20 13:31   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 04/19] nptl: Move pthread_getaffinity_np " Florian Weimer
2020-05-20 13:52   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 05/19] nptl: Move pthread_gettattr_np " Florian Weimer
2020-05-20 13:57   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 06/19] nptl: Make __pthread_attr_init, __pthread_attr_destroy available internally Florian Weimer
2020-05-20 13:59   ` Carlos O'Donell
2020-05-19 10:44 ` Florian Weimer [this message]
2020-05-20 14:10   ` [PATCH 07/19] nptl: Add __pthread_attr_copy for copying pthread_attr_t objects Carlos O'Donell
2020-05-19 10:44 ` [PATCH 08/19] nptl: Use __pthread_attr_copy in pthread_getattr_default_np (bug 25999) Florian Weimer
2020-05-20 14:42   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 09/19] nptl: Use __pthread_attr_copy in pthread_setattr_default_np Florian Weimer
2020-05-20 14:48   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 10/19] <libc-symbols.h>: Add libpthread hidden alias support Florian Weimer
2020-05-20 14:51   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 11/19] nptl: Add internal alias __pthread_getattr_default_np Florian Weimer
2020-06-02  3:28   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 12/19] nptl: Use __pthread_getattr_default_np in pthread_create Florian Weimer
2020-06-02  3:34   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 13/19] nptl: Use __pthread_attr_setaffinity_np in pthread_getattr_np Florian Weimer
2020-06-02  3:36   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 14/19] nptl: Change type of __default_pthread_attr Florian Weimer
2020-06-02  3:39   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 15/19] nptl: Destroy the default thread attribute as part of freeres Florian Weimer
2020-06-02  3:41   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 16/19] nptl: Make pthread_attr_t dynamically extensible Florian Weimer
2020-06-02  3:47   ` Carlos O'Donell
2020-05-19 10:44 ` [PATCH 17/19] nptl: Add pthread_attr_setsigmask_np, pthread_attr_getsigmask_np Florian Weimer
2020-06-02  4:01   ` Carlos O'Donell
2020-05-19 10:45 ` [PATCH 18/19] manual: " Florian Weimer
2020-05-20  7:39   ` Michael Kerrisk
2020-06-03  9:26     ` Florian Weimer
2020-06-02  4:05   ` Carlos O'Donell
2020-05-19 10:45 ` [PATCH 19/19] Linux: Use __pthread_attr_setsigmask_internal for timer helper thread Florian Weimer
2020-06-02  4:07   ` Carlos O'Donell
2020-05-20 13:11 ` [PATCH 00/19] Signal mask " Carlos O'Donell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=653128d1dee168e40595a2228e0100dcad3ac2fe.1589884403.git.fweimer@redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).