Blame nptl/pthread_spin_trylock.c

Packit 6c4009
/* pthread_spin_trylock -- trylock a spin lock.  Generic version.
Packit 6c4009
   Copyright (C) 2012-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 <atomic.h>
Packit 6c4009
#include "pthreadP.h"
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
pthread_spin_trylock (pthread_spinlock_t *lock)
Packit 6c4009
{
Packit 6c4009
  /* For the spin try lock, we have the following possibilities:
Packit 6c4009
Packit 6c4009
     1) If we assume that trylock will most likely succeed in practice:
Packit 6c4009
     * We just do an exchange.
Packit 6c4009
Packit 6c4009
     2) If we want to bias towards cases where trylock succeeds, but don't
Packit 6c4009
     rule out contention:
Packit 6c4009
     * If exchange is not implemented by a CAS loop, and exchange is faster
Packit 6c4009
     than CAS, do an exchange.
Packit 6c4009
     * If exchange is implemented by a CAS loop, use a weak CAS and not an
Packit 6c4009
     exchange so we bail out after the first failed attempt to change the state.
Packit 6c4009
Packit 6c4009
     3) If we expect contention to be likely:
Packit 6c4009
     * If CAS always brings the cache line into an exclusive state even if the
Packit 6c4009
     spinlock is already acquired, then load the value first with
Packit 6c4009
     atomic_load_relaxed and test if lock is not acquired. Then do 2).
Packit 6c4009
Packit 6c4009
     We assume that 2) is the common case, and that this won't be slower than
Packit 6c4009
     1) in the common case.
Packit 6c4009
Packit 6c4009
     We use acquire MO to synchronize-with the release MO store in
Packit 6c4009
     pthread_spin_unlock, and thus ensure that prior critical sections
Packit 6c4009
     happen-before this critical section.  */
Packit 6c4009
#if ! ATOMIC_EXCHANGE_USES_CAS
Packit 6c4009
  /* Try to acquire the lock with an exchange instruction as this architecture
Packit 6c4009
     has such an instruction and we assume it is faster than a CAS.
Packit 6c4009
     The acquisition succeeds if the lock is not in an acquired state.  */
Packit 6c4009
  if (atomic_exchange_acquire (lock, 1) == 0)
Packit 6c4009
    return 0;
Packit 6c4009
#else
Packit 6c4009
  /* Try to acquire the lock with a CAS instruction as this architecture
Packit 6c4009
     has no exchange instruction.  The acquisition succeeds if the lock is not
Packit 6c4009
     acquired.  */
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      int val = 0;
Packit 6c4009
      if (atomic_compare_exchange_weak_acquire (lock, &val, 1))
Packit 6c4009
	return 0;
Packit 6c4009
    }
Packit 6c4009
  /* atomic_compare_exchange_weak_acquire can fail spuriously.  Whereas
Packit 6c4009
     C++11 and C11 make it clear that trylock operations can fail spuriously,
Packit 6c4009
     POSIX does not explicitly specify this; it only specifies that failing
Packit 6c4009
     synchronization operations do not need to have synchronization effects
Packit 6c4009
     themselves, but a spurious failure is something that could contradict a
Packit 6c4009
     happens-before established earlier (e.g., that we need to observe that
Packit 6c4009
     the lock is acquired).  Therefore, we emulate a strong CAS by simply
Packit 6c4009
     checking with a relaxed MO load that the lock is really acquired before
Packit 6c4009
     returning EBUSY; the additional overhead this may cause is on the slow
Packit 6c4009
     path.  */
Packit 6c4009
  while (atomic_load_relaxed (lock) == 0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  return EBUSY;
Packit 6c4009
}