Blame sysdeps/unix/sysv/linux/x86/elision-trylock.c

Packit Service 82fcde
/* elision-trylock.c: Lock eliding trylock for pthreads.
Packit Service 82fcde
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <pthread.h>
Packit Service 82fcde
#include <pthreadP.h>
Packit Service 82fcde
#include <lowlevellock.h>
Packit Service 82fcde
#include "hle.h"
Packit Service 82fcde
#include <elision-conf.h>
Packit Service 82fcde
Packit Service 82fcde
#define aconf __elision_aconf
Packit Service 82fcde
Packit Service 82fcde
/* Try to elide a futex trylock.  FUTEX is the futex variable.  ADAPT_COUNT is
Packit Service 82fcde
   the adaptation counter in the mutex.  */
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
__lll_trylock_elision (int *futex, short *adapt_count)
Packit Service 82fcde
{
Packit Service 82fcde
  /* Implement POSIX semantics by forbiding nesting
Packit Service 82fcde
     trylock.  Sorry.  After the abort the code is re-executed
Packit Service 82fcde
     non transactional and if the lock was already locked
Packit Service 82fcde
     return an error.  */
Packit Service 82fcde
  _xabort (_ABORT_NESTED_TRYLOCK);
Packit Service 82fcde
Packit Service 82fcde
  /* Only try a transaction if it's worth it.  See __lll_lock_elision for
Packit Service 82fcde
     why we need atomic accesses.  Relaxed MO is sufficient because this is
Packit Service 82fcde
     just a hint.  */
Packit Service 82fcde
  if (atomic_load_relaxed (adapt_count) <= 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      unsigned status;
Packit Service 82fcde
Packit Service 82fcde
      if ((status = _xbegin()) == _XBEGIN_STARTED)
Packit Service 82fcde
	{
Packit Service 82fcde
	  if (*futex == 0)
Packit Service 82fcde
	    return 0;
Packit Service 82fcde
Packit Service 82fcde
	  /* Lock was busy.  Fall back to normal locking.
Packit Service 82fcde
	     Could also _xend here but xabort with 0xff code
Packit Service 82fcde
	     is more visible in the profiler.  */
Packit Service 82fcde
	  _xabort (_ABORT_LOCK_BUSY);
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      if (!(status & _XABORT_RETRY))
Packit Service 82fcde
        {
Packit Service 82fcde
          /* Internal abort.  No chance for retry.  For future
Packit Service 82fcde
             locks don't try speculation for some time.  See above for MO.  */
Packit Service 82fcde
          if (atomic_load_relaxed (adapt_count)
Packit Service 82fcde
              != aconf.skip_lock_internal_abort)
Packit Service 82fcde
            atomic_store_relaxed (adapt_count, aconf.skip_lock_internal_abort);
Packit Service 82fcde
        }
Packit Service 82fcde
      /* Could do some retries here.  */
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Lost updates are possible but harmless (see above).  */
Packit Service 82fcde
      atomic_store_relaxed (adapt_count,
Packit Service 82fcde
	  atomic_load_relaxed (adapt_count) - 1);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  return lll_trylock (*futex);
Packit Service 82fcde
}