Blame nptl/pthread_rwlock_trywrlock.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
/* See pthread_rwlock_common.c for an overview.  */
Packit 6c4009
int
Packit 6c4009
__pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
Packit 6c4009
{
Packit 6c4009
  /* When in a trywrlock, we can acquire the write lock if it is in states
Packit 6c4009
     #1 (idle and read phase) and #5 (idle and write phase), and also in #6
Packit 6c4009
     (readers waiting, write phase) if we prefer writers.
Packit 6c4009
     If we observe any other state, we are allowed to fail and do not need to
Packit 6c4009
     "synchronize memory" as specified by POSIX (hence relaxed MO is
Packit 6c4009
     sufficient for the first load and the CAS failure path).
Packit 6c4009
     We face a similar issue as in tryrdlock in that we need to both avoid
Packit 6c4009
     live-locks / starvation and must not fail spuriously (see there for
Packit 6c4009
     further comments) -- and thus must loop until we get a definitive
Packit 6c4009
     observation or state change.  */
Packit 6c4009
  unsigned int r = atomic_load_relaxed (&rwlock->__data.__readers);
Packit 6c4009
  bool prefer_writer =
Packit 6c4009
      (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP);
Packit 6c4009
  while (((r & PTHREAD_RWLOCK_WRLOCKED) == 0)
Packit 6c4009
      && (((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0)
Packit 6c4009
	  || (prefer_writer && ((r & PTHREAD_RWLOCK_WRPHASE) != 0))))
Packit 6c4009
    {
Packit 6c4009
      /* Try to transition to states #7 or #8 (i.e., acquire the lock).  */
Packit 6c4009
      if (atomic_compare_exchange_weak_acquire (
Packit 6c4009
	  &rwlock->__data.__readers, &r,
Packit 6c4009
	  r | PTHREAD_RWLOCK_WRPHASE | PTHREAD_RWLOCK_WRLOCKED))
Packit 6c4009
	{
Packit 6c4009
	  atomic_store_relaxed (&rwlock->__data.__writers_futex, 1);
Packit Bot 0c2104
	  atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1);
Packit 6c4009
	  atomic_store_relaxed (&rwlock->__data.__cur_writer,
Packit 6c4009
	      THREAD_GETMEM (THREAD_SELF, tid));
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
      /* TODO Back-off.  */
Packit 6c4009
      /* See above.  */
Packit 6c4009
    }
Packit 6c4009
  return EBUSY;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
strong_alias (__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock)