Blame nptl/sem_wait.c

Packit 6c4009
/* sem_wait -- wait on a semaphore.  Generic futex-using version.
Packit 6c4009
   Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
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 <lowlevellock.h>	/* lll_futex* used by the old code.  */
Packit 6c4009
#include "sem_waitcommon.c"
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__new_sem_wait (sem_t *sem)
Packit 6c4009
{
Packit 6c4009
  /* We need to check whether we need to act upon a cancellation request here
Packit 6c4009
     because POSIX specifies that cancellation points "shall occur" in
Packit 6c4009
     sem_wait and sem_timedwait, which also means that they need to check
Packit 6c4009
     this regardless whether they block or not (unlike "may occur"
Packit 6c4009
     functions).  See the POSIX Rationale for this requirement: Section
Packit 6c4009
     "Thread Cancellation Overview" [1] and austin group issue #1076 [2]
Packit 6c4009
     for thoughs on why this may be a suboptimal design.
Packit 6c4009
Packit 6c4009
     [1] http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html
Packit 6c4009
     [2] http://austingroupbugs.net/view.php?id=1076 for thoughts on why this
Packit 6c4009
   */
Packit 6c4009
  __pthread_testcancel ();
Packit 6c4009
Packit 6c4009
  if (__new_sem_wait_fast ((struct new_sem *) sem, 0) == 0)
Packit 6c4009
    return 0;
Packit 6c4009
  else
Packit 6c4009
    return __new_sem_wait_slow((struct new_sem *) sem, NULL);
Packit 6c4009
}
Packit 6c4009
versioned_symbol (libpthread, __new_sem_wait, sem_wait, GLIBC_2_1);
Packit 6c4009
Packit 6c4009
#if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
Packit 6c4009
int
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
__old_sem_wait (sem_t *sem)
Packit 6c4009
{
Packit 6c4009
  int *futex = (int *) sem;
Packit 6c4009
  int err;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      if (atomic_decrement_if_positive (futex) > 0)
Packit 6c4009
	return 0;
Packit 6c4009
Packit 6c4009
      /* Enable asynchronous cancellation.  Required by the standard.  */
Packit 6c4009
      int oldtype = __pthread_enable_asynccancel ();
Packit 6c4009
Packit 6c4009
      /* Always assume the semaphore is shared.  */
Packit 6c4009
      err = lll_futex_wait (futex, 0, LLL_SHARED);
Packit 6c4009
Packit 6c4009
      /* Disable asynchronous cancellation.  */
Packit 6c4009
      __pthread_disable_asynccancel (oldtype);
Packit 6c4009
    }
Packit 6c4009
  while (err == 0 || err == -EWOULDBLOCK);
Packit 6c4009
Packit 6c4009
  __set_errno (-err);
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__new_sem_trywait (sem_t *sem)
Packit 6c4009
{
Packit 6c4009
  /* We must not fail spuriously, so require a definitive result even if this
Packit 6c4009
     may lead to a long execution time.  */
Packit 6c4009
  if (__new_sem_wait_fast ((struct new_sem *) sem, 1) == 0)
Packit 6c4009
    return 0;
Packit 6c4009
  __set_errno (EAGAIN);
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
versioned_symbol (libpthread, __new_sem_trywait, sem_trywait, GLIBC_2_1);
Packit 6c4009
#if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
Packit 6c4009
int
Packit 6c4009
__old_sem_trywait (sem_t *sem)
Packit 6c4009
{
Packit 6c4009
  int *futex = (int *) sem;
Packit 6c4009
  int val;
Packit 6c4009
Packit 6c4009
  if (*futex > 0)
Packit 6c4009
    {
Packit 6c4009
      val = atomic_decrement_if_positive (futex);
Packit 6c4009
      if (val > 0)
Packit 6c4009
	return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __set_errno (EAGAIN);
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0);
Packit 6c4009
#endif