2ce75b20b8a75b6effd385811e58318d613ad355
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / powerpc / elision-lock.c
1 /* elision-lock.c: Elided pthread mutex lock.
2    Copyright (C) 2014 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 <stdio.h>
20 #include <pthread.h>
21 #include <pthreadP.h>
22 #include <lowlevellock.h>
23 #include <elision-conf.h>
24 #include "htm.h"
25
26 /* PowerISA 2.0.7 Section B.5.5 defines isync to be insufficient as a
27    barrier in acquire mechanism for HTM operations, a strong 'sync' is
28    required.  */
29 #undef __arch_compare_and_exchange_val_32_acq
30 #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval)           \
31   ({                                                                          \
32       __typeof (*(mem)) __tmp;                                                \
33       __typeof (mem)  __memp = (mem);                                         \
34       __asm __volatile (                                                      \
35                         "1:     lwarx   %0,0,%1" MUTEX_HINT_ACQ "\n"          \
36                         "       cmpw    %0,%2\n"                              \
37                         "       bne     2f\n"                                 \
38                         "       stwcx.  %3,0,%1\n"                            \
39                         "       bne-    1b\n"                                 \
40                         "2:     sync"                                         \
41                         : "=&r" (__tmp)                                       \
42                         : "b" (__memp), "r" (oldval), "r" (newval)            \
43                         : "cr0", "memory");                                   \
44       __tmp;                                                                  \
45   })
46
47 #if !defined(LLL_LOCK) && !defined(EXTRAARG)
48 /* Make sure the configuration code is always linked in for static
49    libraries.  */
50 #include "elision-conf.c"
51 #endif
52
53 #ifndef EXTRAARG
54 # define EXTRAARG
55 #endif
56 #ifndef LLL_LOCK
57 # define LLL_LOCK(a,b) lll_lock(a,b), 0
58 #endif
59
60 #define aconf __elision_aconf
61
62 /* Adaptive lock using transactions.
63    By default the lock region is run as a transaction, and when it
64    aborts or the lock is busy the lock adapts itself.  */
65
66 int
67 __lll_lock_elision (int *lock, short *adapt_count, EXTRAARG int pshared)
68 {
69   if (*adapt_count > 0)
70     {
71       (*adapt_count)--;
72       goto use_lock;
73     }
74
75   int try_begin = aconf.try_tbegin;
76   while (1)
77     {
78       if (__builtin_tbegin (0))
79         {
80           if (*lock == 0)
81             return 0;
82           /* Lock was busy.  Fall back to normal locking.  */
83           __builtin_tabort (_ABORT_LOCK_BUSY);
84         }
85       else
86         {
87           /* A persistent failure indicates that a retry will probably
88              result in another failure.  Use normal locking now and
89              for the next couple of calls.  */
90           if (try_begin-- <= 0
91               || _TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
92             {
93               if (aconf.skip_lock_internal_abort > 0)
94                 *adapt_count = aconf.skip_lock_internal_abort;
95               goto use_lock;
96             }
97           /* Same logic as above, but for for a number of temporary failures
98              in a row.  */
99           else if (aconf.skip_lock_out_of_tbegin_retries > 0
100                    && aconf.try_tbegin > 0)
101             *adapt_count = aconf.skip_lock_out_of_tbegin_retries;
102         }
103      }
104
105 use_lock:
106   return LLL_LOCK ((*lock), pshared);
107 }