1 /* Elided pthread mutex lock.
2    Copyright (C) 2014-2021 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    <https://www.gnu.org/licenses/>.  */
18 
19 #include <pthread.h>
20 #include <pthreadP.h>
21 #include <lowlevellock.h>
22 #include <htm.h>
23 #include <elision-conf.h>
24 #include <stdint.h>
25 
26 #ifndef EXTRAARG
27 #define EXTRAARG
28 #endif
29 #ifndef LLL_LOCK
30 #define LLL_LOCK(a,b) lll_lock(a,b), 0
31 #endif
32 
33 #define aconf __elision_aconf
34 
35 /* Adaptive lock using transactions.
36    By default the lock region is run as a transaction, and when it
37    aborts or the lock is busy the lock adapts itself.  */
38 
39 int
__lll_lock_elision(int * futex,short * adapt_count,EXTRAARG int private)40 __lll_lock_elision (int *futex, short *adapt_count, EXTRAARG int private)
41 {
42   /* adapt_count can be accessed concurrently; these accesses can be both
43      inside of transactions (if critical sections are nested and the outer
44      critical section uses lock elision) and outside of transactions.  Thus,
45      we need to use atomic accesses to avoid data races.  However, the
46      value of adapt_count is just a hint, so relaxed MO accesses are
47      sufficient.  */
48     if (atomic_load_relaxed (adapt_count) <= 0 && aconf.try_tbegin > 0)
49     {
50       /* Start a transaction and retry it automatically if it aborts with
51 	 _HTM_TBEGIN_TRANSIENT.  This macro calls tbegin at most retry_cnt
52 	 + 1 times.  The second argument is considered as retry_cnt.  */
53       int status = __libc_tbegin_retry ((void *) 0, aconf.try_tbegin - 1);
54       if (__glibc_likely (status == _HTM_TBEGIN_STARTED))
55 	{
56 	  /* Check the futex to make sure nobody has touched it in the
57 	     mean time.  This forces the futex into the cache and makes
58 	     sure the transaction aborts if another thread acquires the lock
59 	     concurrently.  */
60 	  if (__glibc_likely (atomic_load_relaxed (futex) == 0))
61 	    /* Lock was free.  Return to user code in a transaction.  */
62 	    return 0;
63 
64 	  /* Lock was busy.  Fall back to normal locking.
65 	     This can be the case if e.g. adapt_count was decremented to zero
66 	     by a former release and another thread has been waken up and
67 	     acquired it.  */
68 	  if (__glibc_likely (__libc_tx_nesting_depth () <= 1))
69 	    {
70 	      /* In a non-nested transaction there is no need to abort,
71 		 which is expensive.  Simply end the started transaction.  */
72 	      __libc_tend ();
73 	      /* Don't try to use transactions for the next couple of times.
74 		 See above for why relaxed MO is sufficient.  */
75 	      if (aconf.skip_lock_busy > 0)
76 		atomic_store_relaxed (adapt_count, aconf.skip_lock_busy);
77 	    }
78 	  else /* nesting depth is > 1 */
79 	    {
80 	      /* A nested transaction will abort eventually because it
81 		 cannot make any progress before *futex changes back to 0.
82 		 So we may as well abort immediately.
83 		 This persistently aborts the outer transaction to force
84 		 the outer mutex use the default lock instead of retrying
85 		 with transactions until the try_tbegin of the outer mutex
86 		 is zero.
87 		 The adapt_count of this inner mutex is not changed,
88 		 because using the default lock with the inner mutex
89 		 would abort the outer transaction.  */
90 	      __libc_tabort (_HTM_FIRST_USER_ABORT_CODE | 1);
91 	      __builtin_unreachable ();
92 	    }
93 	}
94       else if (status != _HTM_TBEGIN_TRANSIENT)
95 	{
96 	  /* A persistent abort (cc 1 or 3) indicates that a retry is
97 	     probably futile.  Use the normal locking now and for the
98 	     next couple of calls.
99 	     Be careful to avoid writing to the lock.  See above for why
100 	     relaxed MO is sufficient.  */
101 	  if (aconf.skip_lock_internal_abort > 0)
102 	    atomic_store_relaxed (adapt_count,
103 				  aconf.skip_lock_internal_abort);
104 	}
105       else
106 	{
107 	  /* The transaction failed for some retries with
108 	     _HTM_TBEGIN_TRANSIENT.  Use the normal locking now and for the
109 	     next couple of calls.  */
110 	  if (aconf.skip_lock_out_of_tbegin_retries > 0)
111 	    atomic_store_relaxed (adapt_count,
112 				  aconf.skip_lock_out_of_tbegin_retries);
113 	}
114     }
115 
116   /* Use normal locking as fallback path if the transaction does not
117      succeed.  */
118   return LLL_LOCK ((*futex), private);
119 }
120 libc_hidden_def (__lll_lock_elision)
121