Blame sysdeps/unix/sysv/linux/s390/elision-unlock.c

Packit 6c4009
/* Commit an elided pthread lock.
Packit 6c4009
   Copyright (C) 2014-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 <pthreadP.h>
Packit 6c4009
#include <lowlevellock.h>
Packit 6c4009
#include <htm.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__lll_unlock_elision(int *futex, short *adapt_count, int private)
Packit 6c4009
{
Packit 6c4009
  /* If the lock is free, we elided the lock earlier.  This does not
Packit 6c4009
     necessarily mean that we are in a transaction, because the user code may
Packit 6c4009
     have closed the transaction, but that is impossible to detect reliably.
Packit 6c4009
     Relaxed MO access to futex is sufficient because a correct program
Packit 6c4009
     will only release a lock it has acquired; therefore, it must either
Packit 6c4009
     changed the futex word's value to something !=0 or it must have used
Packit 6c4009
     elision; these are actions by the same thread, so these actions are
Packit 6c4009
     sequenced-before the relaxed load (and thus also happens-before the
Packit 6c4009
     relaxed load).  Therefore, relaxed MO is sufficient.  */
Packit 6c4009
  if (atomic_load_relaxed (futex) == 0)
Packit 6c4009
    {
Packit 6c4009
      __libc_tend ();
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Update the adapt_count while unlocking before completing the critical
Packit 6c4009
	 section.  adapt_count is accessed concurrently outside of a
Packit 6c4009
	 transaction or a critical section (e.g. in elision-lock.c). So we need
Packit 6c4009
	 to use atomic accesses.  However, the value of adapt_count is just a
Packit 6c4009
	 hint, so relaxed MO accesses are sufficient.
Packit 6c4009
	 If adapt_count would be decremented while locking, multiple
Packit 6c4009
	 CPUs, trying to lock the acquired mutex, will decrement adapt_count to
Packit 6c4009
	 zero and another CPU will try to start a transaction, which will be
Packit 6c4009
	 immediately aborted as the mutex is locked.
Packit 6c4009
	 The update of adapt_count is done before releasing the lock as POSIX'
Packit 6c4009
	 mutex destruction requirements disallow accesses to the mutex after it
Packit 6c4009
	 has been released and thus could have been acquired or destroyed by
Packit 6c4009
	 another thread.  */
Packit 6c4009
      short adapt_count_val = atomic_load_relaxed (adapt_count);
Packit 6c4009
      if (adapt_count_val > 0)
Packit 6c4009
	atomic_store_relaxed (adapt_count, adapt_count_val - 1);
Packit 6c4009
Packit 6c4009
      lll_unlock ((*futex), private);
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}