Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86 / elision-lock.c
blob385ac31a52496212d4db95ec9ca5ee6115126259
1 /* elision-lock.c: Elided pthread mutex lock.
2 Copyright (C) 2011-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 #if !defined(LLL_LOCK) && !defined(EXTRAARG)
26 /* Make sure the configuration code is always linked in for static
27 libraries. */
28 #include "elision-conf.c"
29 #endif
31 #ifndef EXTRAARG
32 #define EXTRAARG
33 #endif
34 #ifndef LLL_LOCK
35 #define LLL_LOCK(a,b) lll_lock(a,b), 0
36 #endif
38 #define aconf __elision_aconf
40 /* Adaptive lock using transactions.
41 By default the lock region is run as a transaction, and when it
42 aborts or the lock is busy the lock adapts itself. */
44 int
45 __lll_lock_elision (int *futex, short *adapt_count, EXTRAARG int private)
47 if (*adapt_count <= 0)
49 unsigned status;
50 int try_xbegin;
52 for (try_xbegin = aconf.retry_try_xbegin;
53 try_xbegin > 0;
54 try_xbegin--)
56 if ((status = _xbegin()) == _XBEGIN_STARTED)
58 if (*futex == 0)
59 return 0;
61 /* Lock was busy. Fall back to normal locking.
62 Could also _xend here but xabort with 0xff code
63 is more visible in the profiler. */
64 _xabort (_ABORT_LOCK_BUSY);
67 if (!(status & _XABORT_RETRY))
69 if ((status & _XABORT_EXPLICIT)
70 && _XABORT_CODE (status) == _ABORT_LOCK_BUSY)
72 /* Right now we skip here. Better would be to wait a bit
73 and retry. This likely needs some spinning. */
74 if (*adapt_count != aconf.skip_lock_busy)
75 *adapt_count = aconf.skip_lock_busy;
77 /* Internal abort. There is no chance for retry.
78 Use the normal locking and next time use lock.
79 Be careful to avoid writing to the lock. */
80 else if (*adapt_count != aconf.skip_lock_internal_abort)
81 *adapt_count = aconf.skip_lock_internal_abort;
82 break;
86 else
88 /* Use a normal lock until the threshold counter runs out.
89 Lost updates possible. */
90 (*adapt_count)--;
93 /* Use a normal lock here. */
94 return LLL_LOCK ((*futex), private);