4a310d59450c61352f0e487b7c3a7e7ab2382b20
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / powerpc / elision-trylock.c
1 /* elision-trylock.c: Lock eliding trylock for pthreads.
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 <pthread.h>
20 #include <pthreadP.h>
21 #include <lowlevellock.h>
22 #include <elision-conf.h>
23 #include "htm.h"
24
25 #define aconf __elision_aconf
26
27 /* Try to elide a futex trylock.  FUTEX is the futex variable.  ADAPT_COUNT is
28    the adaptation counter in the mutex.  */
29
30 int
31 __lll_trylock_elision (int *futex, short *adapt_count)
32 {
33   /* Implement POSIX semantics by forbiding nesting elided trylocks.  */
34   __builtin_tabort (_ABORT_NESTED_TRYLOCK);
35
36   /* Only try a transaction if it's worth it.  */
37   if (*adapt_count > 0)
38     {
39       (*adapt_count)--;
40       goto use_lock;
41     }
42
43   if (__builtin_tbegin (0))
44     {
45       if (*futex == 0)
46         return 0;
47
48       /* Lock was busy.  Fall back to normal locking.  */
49       __builtin_tabort (_ABORT_LOCK_BUSY);
50     }
51   else
52     {
53       if (_TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
54         {
55           /* A persistent failure indicates that a retry will probably
56              result in another failure.  Use normal locking now and
57              for the next couple of calls.  */
58           if (aconf.skip_trylock_internal_abort > 0)
59             *adapt_count = aconf.skip_trylock_internal_abort;
60         }
61
62         if (aconf.skip_lock_busy > 0)
63           *adapt_count = aconf.skip_lock_busy;
64     }
65
66 use_lock:
67   return lll_trylock (*futex);
68 }