1 /* low level locking for pthread library. Generic futex-using version.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <lowlevellock.h>
25 __lll_lock_wait_private (int *futex
)
29 int oldval
= atomic_compare_and_exchange_val_acq (futex
, 2, 1);
31 lll_futex_wait (futex
, 2, LLL_PRIVATE
);
33 while (atomic_compare_and_exchange_bool_acq (futex
, 2, 0) != 0);
37 /* These functions don't get included in libc.so */
38 #ifdef IS_IN_libpthread
40 __lll_lock_wait (int *futex
, int private)
44 int oldval
= atomic_compare_and_exchange_val_acq (futex
, 2, 1);
46 lll_futex_wait (futex
, 2, private);
48 while (atomic_compare_and_exchange_bool_acq (futex
, 2, 0) != 0);
53 __lll_timedlock_wait (int *futex
, const struct timespec
*abstime
, int private)
57 /* Reject invalid timeouts. */
58 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
61 /* Upgrade the lock. */
62 if (atomic_exchange_acq (futex
, 2) == 0)
69 /* Get the current time. */
70 (void) __gettimeofday (&tv
, NULL
);
72 /* Compute relative timeout. */
73 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
74 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
77 rt
.tv_nsec
+= 1000000000;
81 /* Already timed out? */
85 // XYZ: Lost the lock to check whether it was private.
86 lll_futex_timed_wait (futex
, 2, &rt
, private);
88 while (atomic_compare_and_exchange_bool_acq (futex
, 2, 0) != 0);
95 __lll_timedwait_tid (int *tidp
, const struct timespec
*abstime
)
99 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
102 /* Repeat until thread terminated. */
103 while ((tid
= *tidp
) != 0)
108 /* Get the current time. */
109 (void) __gettimeofday (&tv
, NULL
);
111 /* Compute relative timeout. */
112 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
113 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
116 rt
.tv_nsec
+= 1000000000;
120 /* Already timed out? */
124 /* Wait until thread terminates. */
125 // XYZ: Lost the lock to check whether it was private.
126 if (lll_futex_timed_wait (tidp
, tid
, &rt
, LLL_SHARED
) == -ETIMEDOUT
)