1 | /* elide.h: Generic lock elision support. |
2 | Copyright (C) 2014-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 | #ifndef ELIDE_H |
19 | #define ELIDE_H 1 |
20 | |
21 | #include <hle.h> |
22 | #include <elision-conf.h> |
23 | |
24 | #define ACCESS_ONCE(x) (* (volatile typeof(x) *) &(x)) |
25 | |
26 | /* Adapt elision with ADAPT_COUNT and STATUS and decide retries. */ |
27 | |
28 | static inline bool |
29 | elision_adapt(signed char *adapt_count, unsigned int status) |
30 | { |
31 | if (status & _XABORT_RETRY) |
32 | return false; |
33 | if ((status & _XABORT_EXPLICIT) |
34 | && _XABORT_CODE (status) == _ABORT_LOCK_BUSY) |
35 | { |
36 | /* Right now we skip here. Better would be to wait a bit |
37 | and retry. This likely needs some spinning. Be careful |
38 | to avoid writing the lock. */ |
39 | if (*adapt_count != __elision_aconf.skip_lock_busy) |
40 | ACCESS_ONCE (*adapt_count) = __elision_aconf.skip_lock_busy; |
41 | } |
42 | /* Internal abort. There is no chance for retry. |
43 | Use the normal locking and next time use lock. |
44 | Be careful to avoid writing to the lock. */ |
45 | else if (*adapt_count != __elision_aconf.skip_lock_internal_abort) |
46 | ACCESS_ONCE (*adapt_count) = __elision_aconf.skip_lock_internal_abort; |
47 | return true; |
48 | } |
49 | |
50 | /* is_lock_free must be executed inside the transaction */ |
51 | |
52 | /* Returns true if lock defined by IS_LOCK_FREE was elided. |
53 | ADAPT_COUNT is a pointer to per-lock state variable. */ |
54 | |
55 | #define ELIDE_LOCK(adapt_count, is_lock_free) \ |
56 | ({ \ |
57 | int ret = 0; \ |
58 | \ |
59 | if ((adapt_count) <= 0) \ |
60 | { \ |
61 | for (int i = __elision_aconf.retry_try_xbegin; i > 0; i--) \ |
62 | { \ |
63 | unsigned int status; \ |
64 | if ((status = _xbegin ()) == _XBEGIN_STARTED) \ |
65 | { \ |
66 | if (is_lock_free) \ |
67 | { \ |
68 | ret = 1; \ |
69 | break; \ |
70 | } \ |
71 | _xabort (_ABORT_LOCK_BUSY); \ |
72 | } \ |
73 | if (!elision_adapt (&(adapt_count), status)) \ |
74 | break; \ |
75 | } \ |
76 | } \ |
77 | else \ |
78 | (adapt_count)--; /* missing updates ok */ \ |
79 | ret; \ |
80 | }) |
81 | |
82 | /* Returns true if lock defined by IS_LOCK_FREE was try-elided. |
83 | ADAPT_COUNT is a pointer to per-lock state variable. */ |
84 | |
85 | #define ELIDE_TRYLOCK(adapt_count, is_lock_free, write) ({ \ |
86 | int ret = 0; \ |
87 | if (__elision_aconf.retry_try_xbegin > 0) \ |
88 | { \ |
89 | if (write) \ |
90 | _xabort (_ABORT_NESTED_TRYLOCK); \ |
91 | ret = ELIDE_LOCK (adapt_count, is_lock_free); \ |
92 | } \ |
93 | ret; \ |
94 | }) |
95 | |
96 | /* Returns true if lock defined by IS_LOCK_FREE was elided. The call |
97 | to _xend crashes if the application incorrectly tries to unlock a |
98 | lock which has not been locked. */ |
99 | |
100 | #define ELIDE_UNLOCK(is_lock_free) \ |
101 | ({ \ |
102 | int ret = 0; \ |
103 | if (is_lock_free) \ |
104 | { \ |
105 | _xend (); \ |
106 | ret = 1; \ |
107 | } \ |
108 | ret; \ |
109 | }) |
110 | |
111 | #endif |
112 | |