Blame nptl/pthread_setcancelstate.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_setcancelstate (int state, int *oldstate)
Packit 6c4009
{
Packit 6c4009
  volatile struct pthread *self;
Packit 6c4009
Packit 6c4009
  if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
Packit 6c4009
    return EINVAL;
Packit 6c4009
Packit 6c4009
  self = THREAD_SELF;
Packit 6c4009
Packit 6c4009
  int oldval = THREAD_GETMEM (self, cancelhandling);
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      int newval = (state == PTHREAD_CANCEL_DISABLE
Packit 6c4009
		    ? oldval | CANCELSTATE_BITMASK
Packit 6c4009
		    : oldval & ~CANCELSTATE_BITMASK);
Packit 6c4009
Packit 6c4009
      /* Store the old value.  */
Packit 6c4009
      if (oldstate != NULL)
Packit 6c4009
	*oldstate = ((oldval & CANCELSTATE_BITMASK)
Packit 6c4009
		     ? PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE);
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
	    __do_cancel ();
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_setcancelstate, pthread_setcancelstate)
Packit 6c4009
hidden_def (__pthread_setcancelstate)