Update copyright notices with scripts/update-copyrights
[jlayton/glibc.git] / nptl / sysdeps / unix / sysv / linux / 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
25
26 int
27 __lll_robust_lock_wait (int *futex, int private)
28 {
29   int oldval = *futex;
30   int tid = THREAD_GETMEM (THREAD_SELF, tid);
31
32   /* If the futex changed meanwhile try locking again.  */
33   if (oldval == 0)
34     goto try;
35
36   do
37     {
38       if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
39         return oldval;
40
41       int newval = oldval | FUTEX_WAITERS;
42       if (oldval != newval
43           && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
44         continue;
45
46       lll_futex_wait (futex, newval, private);
47
48     try:
49       ;
50     }
51   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
52                                                         tid | FUTEX_WAITERS,
53                                                         0)) != 0);
54   return 0;
55 }
56
57
58 int
59 __lll_robust_timedlock_wait (int *futex, const struct timespec *abstime,
60                              int private)
61 {
62   /* Reject invalid timeouts.  */
63   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
64     return EINVAL;
65
66   int tid = THREAD_GETMEM (THREAD_SELF, tid);
67   int oldval = *futex;
68
69   /* If the futex changed meanwhile try locking again.  */
70   if (oldval == 0)
71     goto try;
72
73   /* Work around the fact that the kernel rejects negative timeout values
74      despite them being valid.  */
75   if (__builtin_expect (abstime->tv_sec < 0, 0))
76     return ETIMEDOUT;
77
78   do
79     {
80 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
81      || !defined lll_futex_timed_wait_bitset)
82       struct timeval tv;
83       struct timespec rt;
84
85       /* Get the current time.  */
86       (void) __gettimeofday (&tv, NULL);
87
88       /* Compute relative timeout.  */
89       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
90       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
91       if (rt.tv_nsec < 0)
92         {
93           rt.tv_nsec += 1000000000;
94           --rt.tv_sec;
95         }
96
97       /* Already timed out?  */
98       if (rt.tv_sec < 0)
99         return ETIMEDOUT;
100 #endif
101
102       /* Wait.  */
103       if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
104         return oldval;
105
106       int newval = oldval | FUTEX_WAITERS;
107       if (oldval != newval
108           && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
109         continue;
110
111 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
112      || !defined lll_futex_timed_wait_bitset)
113       lll_futex_timed_wait (futex, newval, &rt, private);
114 #else
115       lll_futex_timed_wait_bitset (futex, newval, abstime,
116                                    FUTEX_CLOCK_REALTIME, private);
117 #endif
118
119     try:
120       ;
121     }
122   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
123                                                         tid | FUTEX_WAITERS,
124                                                         0)) != 0);
125
126   return 0;
127 }