Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86 / elision-trylock.c
bloba478ac18e3b3f11b01bbacbd121573cb15a203ae
1 /* elision-trylock.c: Lock eliding trylock for pthreads.
2 Copyright (C) 2013-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <pthread.h>
20 #include <pthreadP.h>
21 #include <lowlevellock.h>
22 #include "hle.h"
23 #include <elision-conf.h>
25 #define aconf __elision_aconf
27 /* Try to elide a futex trylock. FUTEX is the futex variable. ADAPT_COUNT is
28 the adaptation counter in the mutex. */
30 int
31 __lll_trylock_elision (int *futex, short *adapt_count)
33 /* Implement POSIX semantics by forbiding nesting
34 trylock. Sorry. After the abort the code is re-executed
35 non transactional and if the lock was already locked
36 return an error. */
37 _xabort (_ABORT_NESTED_TRYLOCK);
39 /* Only try a transaction if it's worth it. */
40 if (*adapt_count <= 0)
42 unsigned status;
44 if ((status = _xbegin()) == _XBEGIN_STARTED)
46 if (*futex == 0)
47 return 0;
49 /* Lock was busy. Fall back to normal locking.
50 Could also _xend here but xabort with 0xff code
51 is more visible in the profiler. */
52 _xabort (_ABORT_LOCK_BUSY);
55 if (!(status & _XABORT_RETRY))
57 /* Internal abort. No chance for retry. For future
58 locks don't try speculation for some time. */
59 if (*adapt_count != aconf.skip_trylock_internal_abort)
60 *adapt_count = aconf.skip_trylock_internal_abort;
62 /* Could do some retries here. */
64 else
66 /* Lost updates are possible, but harmless. */
67 (*adapt_count)--;
70 return lll_trylock (*futex);