]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/x86/elision-trylock.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / x86 / elision-trylock.c
CommitLineData
1cdbe579 1/* elision-trylock.c: Lock eliding trylock for pthreads.
04277e02 2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
1cdbe579
AK
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
1cdbe579
AK
18
19#include <pthread.h>
20#include <pthreadP.h>
21#include <lowlevellock.h>
22#include "hle.h"
23#include <elision-conf.h>
24
25#define aconf __elision_aconf
26
075b9322
DV
27/* Try to elide a futex trylock. FUTEX is the futex variable. ADAPT_COUNT is
28 the adaptation counter in the mutex. */
1cdbe579
AK
29
30int
31__lll_trylock_elision (int *futex, short *adapt_count)
32{
33 /* Implement POSIX semantics by forbiding nesting
075b9322 34 trylock. Sorry. After the abort the code is re-executed
1cdbe579
AK
35 non transactional and if the lock was already locked
36 return an error. */
37 _xabort (_ABORT_NESTED_TRYLOCK);
38
ca6e601a
TR
39 /* Only try a transaction if it's worth it. See __lll_lock_elision for
40 why we need atomic accesses. Relaxed MO is sufficient because this is
41 just a hint. */
42 if (atomic_load_relaxed (adapt_count) <= 0)
1cdbe579
AK
43 {
44 unsigned status;
45
46 if ((status = _xbegin()) == _XBEGIN_STARTED)
47 {
48 if (*futex == 0)
49 return 0;
50
075b9322 51 /* Lock was busy. Fall back to normal locking.
1cdbe579
AK
52 Could also _xend here but xabort with 0xff code
53 is more visible in the profiler. */
54 _xabort (_ABORT_LOCK_BUSY);
55 }
56
57 if (!(status & _XABORT_RETRY))
58 {
075b9322 59 /* Internal abort. No chance for retry. For future
ca6e601a
TR
60 locks don't try speculation for some time. See above for MO. */
61 if (atomic_load_relaxed (adapt_count)
62 != aconf.skip_lock_internal_abort)
63 atomic_store_relaxed (adapt_count, aconf.skip_lock_internal_abort);
1cdbe579 64 }
075b9322 65 /* Could do some retries here. */
1cdbe579
AK
66 }
67 else
68 {
ca6e601a
TR
69 /* Lost updates are possible but harmless (see above). */
70 atomic_store_relaxed (adapt_count,
71 atomic_load_relaxed (adapt_count) - 1);
1cdbe579
AK
72 }
73
74 return lll_trylock (*futex);
75}