Blame nptl/pthread_setcanceltype.c

Packit 6c4009
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 "pthreadP.h"
Packit 6c4009
#include <atomic.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__pthread_setcanceltype (int type, int *oldtype)
Packit 6c4009
{
Packit 6c4009
  if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS)
Packit 6c4009
    return EINVAL;
Packit 6c4009
Packit 6c4009
#ifndef SIGCANCEL
Packit 6c4009
  if (type == PTHREAD_CANCEL_ASYNCHRONOUS)
Packit 6c4009
    return ENOTSUP;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  volatile struct pthread *self = THREAD_SELF;
Packit 6c4009
Packit 6c4009
  int oldval = THREAD_GETMEM (self, cancelhandling);
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      int newval = (type == PTHREAD_CANCEL_ASYNCHRONOUS
Packit 6c4009
		    ? oldval | CANCELTYPE_BITMASK
Packit 6c4009
		    : oldval & ~CANCELTYPE_BITMASK);
Packit 6c4009
Packit 6c4009
      /* Store the old value.  */
Packit 6c4009
      if (oldtype != NULL)
Packit 6c4009
	*oldtype = ((oldval & CANCELTYPE_BITMASK)
Packit 6c4009
		    ? PTHREAD_CANCEL_ASYNCHRONOUS : PTHREAD_CANCEL_DEFERRED);
Packit 6c4009
Packit 6c4009
      /* Avoid doing unnecessary work.  The atomic operation can
Packit 6c4009
	 potentially be expensive if the memory has to be locked and
Packit 6c4009
	 remote cache lines have to be invalidated.  */
Packit 6c4009
      if (oldval == newval)
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      /* Update the cancel handling word.  This has to be done
Packit 6c4009
	 atomically since other bits could be modified as well.  */
Packit 6c4009
      int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
Packit 6c4009
					      oldval);
Packit 6c4009
      if (__glibc_likely (curval == oldval))
Packit 6c4009
	{
Packit 6c4009
	  if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
Packit 6c4009
	    {
Packit 6c4009
	      THREAD_SETMEM (self, result, PTHREAD_CANCELED);
Packit 6c4009
	      __do_cancel ();
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Prepare for the next round.  */
Packit 6c4009
      oldval = curval;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
strong_alias (__pthread_setcanceltype, pthread_setcanceltype)