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 09/19] nptl: Use __pthread_attr_copy in pthread_setattr_default_np
Date: Tue, 19 May 2020 12:44:34 +0200	[thread overview]
Message-ID: <74f2d4c0a6cd892c70d693fff4bd75a65c4c2e27.1589884403.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1589884403.git.fweimer@redhat.com>

---
 nptl/pthread_setattr_default_np.c | 53 +++++++++----------------------
 1 file changed, 15 insertions(+), 38 deletions(-)

diff --git a/nptl/pthread_setattr_default_np.c b/nptl/pthread_setattr_default_np.c
index d6a46624b5..eb5d24d3bd 100644
--- a/nptl/pthread_setattr_default_np.c
+++ b/nptl/pthread_setattr_default_np.c
@@ -26,7 +26,6 @@ int
 pthread_setattr_default_np (const pthread_attr_t *in)
 {
   const struct pthread_attr *real_in;
-  struct pthread_attr attrs;
   int ret;
 
   real_in = (struct pthread_attr *) in;
@@ -58,49 +57,27 @@ pthread_setattr_default_np (const pthread_attr_t *in)
   if (real_in->flags & ATTR_FLAG_STACKADDR)
     return EINVAL;
 
-  attrs = *real_in;
+  union pthread_attr_transparent temp;
+  ret = __pthread_attr_copy (&temp.external, in);
+  if (ret != 0)
+    return ret;
 
-  /* Now take the lock because we start writing into
+  /* Now take the lock because we start accessing
      __default_pthread_attr.  */
   lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
 
-  /* Free the cpuset if the input is 0.  Otherwise copy in the cpuset
-     contents.  */
-  size_t cpusetsize = attrs.cpusetsize;
-  if (cpusetsize == 0)
-    {
-      free (__default_pthread_attr.cpuset);
-      __default_pthread_attr.cpuset = NULL;
-    }
-  else if (cpusetsize == __default_pthread_attr.cpusetsize)
-    {
-      attrs.cpuset = __default_pthread_attr.cpuset;
-      memcpy (attrs.cpuset, real_in->cpuset, cpusetsize);
-    }
-  else
-    {
-      /* This may look wrong at first sight, but it isn't.  We're freeing
-	 __default_pthread_attr.cpuset and allocating to attrs.cpuset because
-	 we'll copy over all of attr to __default_pthread_attr later.  */
-      cpu_set_t *newp = realloc (__default_pthread_attr.cpuset,
-				 cpusetsize);
-
-      if (newp == NULL)
-	{
-	  ret = ENOMEM;
-	  goto out;
-	}
-
-      attrs.cpuset = newp;
-      memcpy (attrs.cpuset, real_in->cpuset, cpusetsize);
-    }
+  /* Preserve the previous stack size (see above).  */
+  if (temp.internal.stacksize == 0)
+    temp.internal.stacksize = __default_pthread_attr.stacksize;
+
+  /* Destroy the old attribute structure because it will be
+     overwritten.  */
+  __pthread_attr_destroy ((pthread_attr_t *) &__default_pthread_attr);
 
-  /* We don't want to accidentally set the default stacksize to zero.  */
-  if (attrs.stacksize == 0)
-    attrs.stacksize = __default_pthread_attr.stacksize;
-  __default_pthread_attr = attrs;
+  /* __default_pthread_attr takes ownership, so do not free
+     attrs.internal after this point.  */
+  __default_pthread_attr = temp.internal;
 
- out:
   lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
   return ret;
 }
-- 
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 ` [PATCH 07/19] nptl: Add __pthread_attr_copy for copying pthread_attr_t objects Florian Weimer
2020-05-20 14:10   ` 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 ` Florian Weimer [this message]
2020-05-20 14:48   ` [PATCH 09/19] nptl: Use __pthread_attr_copy in pthread_setattr_default_np 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=74f2d4c0a6cd892c70d693fff4bd75a65c4c2e27.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).