1/* elision-lock.c: Elided pthread mutex lock.
2 Copyright (C) 2011-2016 Free Software Foundation, Inc.
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
17 <http://www.gnu.org/licenses/>. */
18
19#include <pthread.h>
20#include "pthreadP.h"
21#include "lowlevellock.h"
22#include "hle.h"
23#include <elision-conf.h>
24
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
30
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
37
38#define aconf __elision_aconf
39
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. */
43
44int
45__lll_lock_elision (int *futex, short *adapt_count, EXTRAARG int private)
46{
47 if (*adapt_count <= 0)
48 {
49 unsigned status;
50 int try_xbegin;
51
52 for (try_xbegin = aconf.retry_try_xbegin;
53 try_xbegin > 0;
54 try_xbegin--)
55 {
56 if ((status = _xbegin()) == _XBEGIN_STARTED)
57 {
58 if (*futex == 0)
59 return 0;
60
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);
65 }
66
67 if (!(status & _XABORT_RETRY))
68 {
69 if ((status & _XABORT_EXPLICIT)
70 && _XABORT_CODE (status) == _ABORT_LOCK_BUSY)
71 {
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;
76 }
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;
83 }
84 }
85 }
86 else
87 {
88 /* Use a normal lock until the threshold counter runs out.
89 Lost updates possible. */
90 (*adapt_count)--;
91 }
92
93 /* Use a normal lock here. */
94 return LLL_LOCK ((*futex), private);
95}
96