(root)/
glibc-2.38/
nptl/
pthread_attr_copy.c
       1  /* Deep copy of a pthread_attr_t object.
       2     Copyright (C) 2020-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <errno.h>
      20  #include <pthreadP.h>
      21  #include <stdlib.h>
      22  
      23  int
      24  __pthread_attr_copy (pthread_attr_t *target, const pthread_attr_t *source)
      25  {
      26    /* Avoid overwriting *TARGET until all allocations have
      27       succeeded.  */
      28    union pthread_attr_transparent temp;
      29    temp.external = *source;
      30  
      31    /* Force new allocation.  This function has full ownership of temp.  */
      32    temp.internal.extension = NULL;
      33  
      34    int ret = 0;
      35  
      36    struct pthread_attr *isource = (struct pthread_attr *) source;
      37  
      38    if (isource->extension != NULL)
      39      {
      40        /* Propagate affinity mask information.  */
      41        if (isource->extension->cpusetsize > 0)
      42          ret = __pthread_attr_setaffinity_np (&temp.external,
      43                                               isource->extension->cpusetsize,
      44                                               isource->extension->cpuset);
      45  
      46        /* Propagate the signal mask information.  */
      47        if (ret == 0 && isource->extension->sigmask_set)
      48          ret = __pthread_attr_setsigmask_internal ((pthread_attr_t *) &temp,
      49                                                    &isource->extension->sigmask);
      50      }
      51  
      52    if (ret != 0)
      53      {
      54        /* Deallocate because we have ownership.  */
      55        __pthread_attr_destroy (&temp.external);
      56        return ret;
      57      }
      58  
      59    /* Transfer ownership.  *target is not assumed to have been
      60       initialized.  */
      61    *target = temp.external;
      62    return 0;
      63  }
      64  libc_hidden_def (__pthread_attr_copy)