]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/unix/sysv/linux/lowlevelrobustlock.c
[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[thirdparty/glibc.git] / nptl / sysdeps / unix / sysv / linux / lowlevelrobustlock.c
CommitLineData
11bf311e 1/* Copyright (C) 2006 Free Software Foundation, Inc.
f1740bc4
UD
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <sysdep.h>
22#include <lowlevellock.h>
23#include <sys/time.h>
24#include <pthreadP.h>
25
26
27int
28__lll_robust_lock_wait (int *futex)
29{
30 int oldval = *futex;
31 int tid = THREAD_GETMEM (THREAD_SELF, tid);
32
33 do
34 {
35 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
36 return oldval;
37
38 int newval = oldval | FUTEX_WAITERS;
39 if (oldval != newval
40 && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
41 continue;
42
43 lll_futex_wait (futex, newval);
44 }
42b6ddfc
UD
45 while ((oldval = atomic_compare_and_exchange_val_acq (futex,
46 tid | FUTEX_WAITERS,
47 0)) != 0);
f1740bc4
UD
48 return 0;
49}
50
51
52int
53__lll_robust_timedlock_wait (int *futex, const struct timespec *abstime)
54{
55 /* Reject invalid timeouts. */
56 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
57 return EINVAL;
58
59 int tid = THREAD_GETMEM (THREAD_SELF, tid);
60
61 do
62 {
63 struct timeval tv;
64 struct timespec rt;
65
66 /* Get the current time. */
67 (void) __gettimeofday (&tv, NULL);
68
69 /* Compute relative timeout. */
70 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
71 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
72 if (rt.tv_nsec < 0)
73 {
74 rt.tv_nsec += 1000000000;
75 --rt.tv_sec;
76 }
77
78 /* Already timed out? */
79 if (rt.tv_sec < 0)
80 return ETIMEDOUT;
81
82 /* Wait. */
11bf311e 83 int oldval = *futex;
f1740bc4
UD
84 if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
85 return oldval;
86
87 int newval = oldval | FUTEX_WAITERS;
88 if (oldval != newval
89 && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
90 continue;
91
92 lll_futex_timed_wait (futex, newval, &rt);
93 }
11bf311e 94 while (atomic_compare_and_exchange_bool_acq (futex, tid | FUTEX_WAITERS, 0));
f1740bc4
UD
95
96 return 0;
97}