]>
Commit | Line | Data |
---|---|---|
8d2c0a59 | 1 | /* elision-lock.c: Elided pthread mutex lock. |
f7a9f785 | 2 | Copyright (C) 2015-2016 Free Software Foundation, Inc. |
8d2c0a59 AZ |
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 <stdio.h> | |
20 | #include <pthread.h> | |
21 | #include <pthreadP.h> | |
22 | #include <lowlevellock.h> | |
23 | #include <elision-conf.h> | |
24 | #include "htm.h" | |
25 | ||
8d2c0a59 AZ |
26 | #if !defined(LLL_LOCK) && !defined(EXTRAARG) |
27 | /* Make sure the configuration code is always linked in for static | |
28 | libraries. */ | |
29 | #include "elision-conf.c" | |
30 | #endif | |
31 | ||
32 | #ifndef EXTRAARG | |
33 | # define EXTRAARG | |
34 | #endif | |
35 | #ifndef LLL_LOCK | |
36 | # define LLL_LOCK(a,b) lll_lock(a,b), 0 | |
37 | #endif | |
38 | ||
39 | #define aconf __elision_aconf | |
40 | ||
41 | /* Adaptive lock using transactions. | |
42 | By default the lock region is run as a transaction, and when it | |
43 | aborts or the lock is busy the lock adapts itself. */ | |
44 | ||
45 | int | |
46 | __lll_lock_elision (int *lock, short *adapt_count, EXTRAARG int pshared) | |
47 | { | |
48 | if (*adapt_count > 0) | |
49 | { | |
8d2c0a59 AZ |
50 | goto use_lock; |
51 | } | |
52 | ||
72f1463d | 53 | for (int i = aconf.try_tbegin; i > 0; i--) |
8d2c0a59 | 54 | { |
42bf1c89 | 55 | if (__libc_tbegin (0)) |
8d2c0a59 AZ |
56 | { |
57 | if (*lock == 0) | |
58 | return 0; | |
59 | /* Lock was busy. Fall back to normal locking. */ | |
42bf1c89 | 60 | __libc_tabort (_ABORT_LOCK_BUSY); |
8d2c0a59 AZ |
61 | } |
62 | else | |
63 | { | |
64 | /* A persistent failure indicates that a retry will probably | |
65 | result in another failure. Use normal locking now and | |
66 | for the next couple of calls. */ | |
72f1463d | 67 | if (_TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ())) |
8d2c0a59 AZ |
68 | { |
69 | if (aconf.skip_lock_internal_abort > 0) | |
70 | *adapt_count = aconf.skip_lock_internal_abort; | |
71 | goto use_lock; | |
72 | } | |
8d2c0a59 AZ |
73 | } |
74 | } | |
75 | ||
72f1463d PM |
76 | /* Fall back to locks for a bit if retries have been exhausted */ |
77 | if (aconf.try_tbegin > 0 && aconf.skip_lock_out_of_tbegin_retries > 0) | |
78 | *adapt_count = aconf.skip_lock_out_of_tbegin_retries; | |
79 | ||
8d2c0a59 AZ |
80 | use_lock: |
81 | return LLL_LOCK ((*lock), pshared); | |
82 | } |