tile: add no-op fe*() routines for libc internal use
[platform/upstream/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 the owner died, return the present value of the futex.  */
40       if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
41         return oldval;
42
43       /* Try to put the lock into state 'acquired, possibly with waiters'.  */
44       int newval = oldval | FUTEX_WAITERS;
45       if (oldval != newval
46           && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
47         continue;
48
49       /* If *futex == 2, wait until woken.  */
50       lll_futex_wait (futex, newval, private);
51
52     try:
53       ;
54     }
55   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
56                                                         tid | FUTEX_WAITERS,
57                                                         0)) != 0);
58   return 0;
59 }
60
61
62 int
63 __lll_robust_timedlock_wait (int *futex, const struct timespec *abstime,
64                              int private)
65 {
66   /* Reject invalid timeouts.  */
67   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
68     return EINVAL;
69
70   int tid = THREAD_GETMEM (THREAD_SELF, tid);
71   int oldval = *futex;
72
73   /* If the futex changed meanwhile, try locking again.  */
74   if (oldval == 0)
75     goto try;
76
77   /* Work around the fact that the kernel rejects negative timeout values
78      despite them being valid.  */
79   if (__glibc_unlikely (abstime->tv_sec < 0))
80     return ETIMEDOUT;
81
82   do
83     {
84 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
85      || !defined lll_futex_timed_wait_bitset)
86       struct timeval tv;
87       struct timespec rt;
88
89       /* Get the current time.  */
90       (void) __gettimeofday (&tv, NULL);
91
92       /* Compute relative timeout.  */
93       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
94       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
95       if (rt.tv_nsec < 0)
96         {
97           rt.tv_nsec += 1000000000;
98           --rt.tv_sec;
99         }
100
101       /* Already timed out?  */
102       if (rt.tv_sec < 0)
103         return ETIMEDOUT;
104 #endif
105
106       /* If the owner died, return the present value of the futex.  */
107       if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
108         return oldval;
109
110       /* Try to put the lock into state 'acquired, possibly with waiters'.  */
111       int newval = oldval | FUTEX_WAITERS;
112       if (oldval != newval
113           && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
114         continue;
115
116       /* If *futex == 2, wait until woken or timeout.  */
117 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
118      || !defined lll_futex_timed_wait_bitset)
119       lll_futex_timed_wait (futex, newval, &rt, private);
120 #else
121       lll_futex_timed_wait_bitset (futex, newval, abstime,
122                                    FUTEX_CLOCK_REALTIME, private);
123 #endif
124
125     try:
126       ;
127     }
128   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
129                                                         tid | FUTEX_WAITERS,
130                                                         0)) != 0);
131
132   return 0;
133 }