(root)/
glibc-2.38/
nptl/
pthread_attr_setaffinity.c
       1  /* Copyright (C) 2003-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <limits.h>
      20  #include <stdlib.h>
      21  #include <string.h>
      22  #include <pthreadP.h>
      23  #include <shlib-compat.h>
      24  
      25  
      26  int
      27  __pthread_attr_setaffinity_np (pthread_attr_t *attr, size_t cpusetsize,
      28  			       const cpu_set_t *cpuset)
      29  {
      30    struct pthread_attr *iattr;
      31  
      32    iattr = (struct pthread_attr *) attr;
      33  
      34    if (cpuset == NULL || cpusetsize == 0)
      35      {
      36        if (iattr->extension != NULL)
      37  	{
      38  	  free (iattr->extension->cpuset);
      39  	  iattr->extension->cpuset = NULL;
      40  	  iattr->extension->cpusetsize = 0;
      41  	}
      42      }
      43    else
      44      {
      45        int ret = __pthread_attr_extension (iattr);
      46        if (ret != 0)
      47  	return ret;
      48  
      49        if (iattr->extension->cpusetsize != cpusetsize)
      50  	{
      51  	  void *newp = realloc (iattr->extension->cpuset, cpusetsize);
      52  	  if (newp == NULL)
      53  	    return ENOMEM;
      54  
      55  	  iattr->extension->cpuset = newp;
      56  	  iattr->extension->cpusetsize = cpusetsize;
      57  	}
      58  
      59        memcpy (iattr->extension->cpuset, cpuset, cpusetsize);
      60      }
      61  
      62    return 0;
      63  }
      64  libc_hidden_def (__pthread_attr_setaffinity_np)
      65  versioned_symbol (libc, __pthread_attr_setaffinity_np,
      66  		  pthread_attr_setaffinity_np, GLIBC_2_32);
      67  
      68  
      69  #if SHLIB_COMPAT (libc, GLIBC_2_3_4, GLIBC_2_32)
      70  /* Compat symbol with the old libc version.  */
      71  strong_alias (__pthread_attr_setaffinity_np, __pthread_attr_setaffinity_alias)
      72  compat_symbol (libc, __pthread_attr_setaffinity_alias,
      73  	       pthread_attr_setaffinity_np, GLIBC_2_3_4);
      74  #endif
      75  
      76  #if SHLIB_COMPAT (libc, GLIBC_2_3_3, GLIBC_2_3_4)
      77  int
      78  __pthread_attr_setaffinity_old (pthread_attr_t *attr, cpu_set_t *cpuset)
      79  {
      80    /* The old interface by default assumed a 1024 processor bitmap.  */
      81    return __pthread_attr_setaffinity_np (attr, 128, cpuset);
      82  }
      83  compat_symbol (libc, __pthread_attr_setaffinity_old,
      84  	       pthread_attr_setaffinity_np, GLIBC_2_3_3);
      85  #endif