Blame nptl/pthread_setattr_default_np.c

Packit 6c4009
/* Set the default attributes to be used by pthread_create in the process.
Packit 6c4009
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <pthreadP.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
pthread_setattr_default_np (const pthread_attr_t *in)
Packit 6c4009
{
Packit 6c4009
  const struct pthread_attr *real_in;
Packit 6c4009
  struct pthread_attr attrs;
Packit 6c4009
  int ret;
Packit 6c4009
Packit 6c4009
  real_in = (struct pthread_attr *) in;
Packit 6c4009
Packit 6c4009
  /* Catch invalid values.  */
Packit 6c4009
  int policy = real_in->schedpolicy;
Packit 6c4009
  ret = check_sched_policy_attr (policy);
Packit 6c4009
  if (ret)
Packit 6c4009
    return ret;
Packit 6c4009
Packit 6c4009
  const struct sched_param *param = &real_in->schedparam;
Packit 6c4009
  if (param->sched_priority > 0)
Packit 6c4009
    {
Packit 6c4009
      ret = check_sched_priority_attr (param->sched_priority, policy);
Packit 6c4009
      if (ret)
Packit 6c4009
	return ret;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* stacksize == 0 is fine.  It means that we don't change the current
Packit 6c4009
     value.  */
Packit 6c4009
  if (real_in->stacksize != 0)
Packit 6c4009
    {
Packit 6c4009
      ret = check_stacksize_attr (real_in->stacksize);
Packit 6c4009
      if (ret)
Packit 6c4009
	return ret;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Having a default stack address is wrong.  */
Packit 6c4009
  if (real_in->flags & ATTR_FLAG_STACKADDR)
Packit 6c4009
    return EINVAL;
Packit 6c4009
Packit 6c4009
  attrs = *real_in;
Packit 6c4009
Packit 6c4009
  /* Now take the lock because we start writing into
Packit 6c4009
     __default_pthread_attr.  */
Packit 6c4009
  lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
Packit 6c4009
Packit 6c4009
  /* Free the cpuset if the input is 0.  Otherwise copy in the cpuset
Packit 6c4009
     contents.  */
Packit 6c4009
  size_t cpusetsize = attrs.cpusetsize;
Packit 6c4009
  if (cpusetsize == 0)
Packit 6c4009
    {
Packit 6c4009
      free (__default_pthread_attr.cpuset);
Packit 6c4009
      __default_pthread_attr.cpuset = NULL;
Packit 6c4009
    }
Packit 6c4009
  else if (cpusetsize == __default_pthread_attr.cpusetsize)
Packit 6c4009
    {
Packit 6c4009
      attrs.cpuset = __default_pthread_attr.cpuset;
Packit 6c4009
      memcpy (attrs.cpuset, real_in->cpuset, cpusetsize);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* This may look wrong at first sight, but it isn't.  We're freeing
Packit 6c4009
	 __default_pthread_attr.cpuset and allocating to attrs.cpuset because
Packit 6c4009
	 we'll copy over all of attr to __default_pthread_attr later.  */
Packit 6c4009
      cpu_set_t *newp = realloc (__default_pthread_attr.cpuset,
Packit 6c4009
				 cpusetsize);
Packit 6c4009
Packit 6c4009
      if (newp == NULL)
Packit 6c4009
	{
Packit 6c4009
	  ret = ENOMEM;
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      attrs.cpuset = newp;
Packit 6c4009
      memcpy (attrs.cpuset, real_in->cpuset, cpusetsize);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We don't want to accidentally set the default stacksize to zero.  */
Packit 6c4009
  if (attrs.stacksize == 0)
Packit 6c4009
    attrs.stacksize = __default_pthread_attr.stacksize;
Packit 6c4009
  __default_pthread_attr = attrs;
Packit 6c4009
Packit 6c4009
 out:
Packit 6c4009
  lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
Packit 6c4009
  return ret;
Packit 6c4009
}