Remove TLS configure tests.
[platform/upstream/linaro-glibc.git] / nptl / lowlevelrobustlock.c
1 /* Copyright (C) 2006-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
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 <errno.h>
20 #include <sysdep.h>
21 #include <lowlevellock.h>
22 #include <sys/time.h>
23 #include <pthreadP.h>
24 #include <kernel-features.h>
25
26
27 int
28 __lll_robust_lock_wait (int *futex, int private)
29 {
30   int oldval = *futex;
31   int tid = THREAD_GETMEM (THREAD_SELF, tid);
32
33   /* If the futex changed meanwhile try locking again.  */
34   if (oldval == 0)
35     goto try;
36
37   do
38     {
39       if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
40         return oldval;
41
42       int newval = oldval | FUTEX_WAITERS;
43       if (oldval != newval
44           && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
45         continue;
46
47       lll_futex_wait (futex, newval, private);
48
49     try:
50       ;
51     }
52   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
53                                                         tid | FUTEX_WAITERS,
54                                                         0)) != 0);
55   return 0;
56 }
57
58
59 int
60 __lll_robust_timedlock_wait (int *futex, const struct timespec *abstime,
61                              int private)
62 {
63   /* Reject invalid timeouts.  */
64   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
65     return EINVAL;
66
67   int tid = THREAD_GETMEM (THREAD_SELF, tid);
68   int oldval = *futex;
69
70   /* If the futex changed meanwhile try locking again.  */
71   if (oldval == 0)
72     goto try;
73
74   /* Work around the fact that the kernel rejects negative timeout values
75      despite them being valid.  */
76   if (__glibc_unlikely (abstime->tv_sec < 0))
77     return ETIMEDOUT;
78
79   do
80     {
81 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
82      || !defined lll_futex_timed_wait_bitset)
83       struct timeval tv;
84       struct timespec rt;
85
86       /* Get the current time.  */
87       (void) __gettimeofday (&tv, NULL);
88
89       /* Compute relative timeout.  */
90       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
91       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
92       if (rt.tv_nsec < 0)
93         {
94           rt.tv_nsec += 1000000000;
95           --rt.tv_sec;
96         }
97
98       /* Already timed out?  */
99       if (rt.tv_sec < 0)
100         return ETIMEDOUT;
101 #endif
102
103       /* Wait.  */
104       if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
105         return oldval;
106
107       int newval = oldval | FUTEX_WAITERS;
108       if (oldval != newval
109           && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
110         continue;
111
112 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
113      || !defined lll_futex_timed_wait_bitset)
114       lll_futex_timed_wait (futex, newval, &rt, private);
115 #else
116       lll_futex_timed_wait_bitset (futex, newval, abstime,
117                                    FUTEX_CLOCK_REALTIME, private);
118 #endif
119
120     try:
121       ;
122     }
123   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
124                                                         tid | FUTEX_WAITERS,
125                                                         0)) != 0);
126
127   return 0;
128 }