1 /* low level locking for pthread library. Generic futex-using version.
2 Copyright (C) 2003, 2005, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <lowlevellock.h>
27 #ifndef IS_IN_libpthread
30 __lll_lock_wait_private (int *futex)
34 int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
36 lll_futex_wait (futex, 2, LLL_PRIVATE);
38 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
42 /* These functions don't get included in libc.so */
43 #ifdef IS_IN_libpthread
45 __lll_lock_wait (int *futex, int private)
49 int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
51 lll_futex_wait (futex, 2, private);
53 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
58 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
62 /* Reject invalid timeouts. */
63 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
66 /* Upgrade the lock. */
67 if (atomic_exchange_acq (futex, 2) == 0)
74 /* Get the current time. */
75 (void) gettimeofday (&tv, NULL);
77 /* Compute relative timeout. */
78 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
79 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
82 rt.tv_nsec += 1000000000;
86 /* Already timed out? */
90 // XYZ: Lost the lock to check whether it was private.
91 lll_futex_timed_wait (futex, 2, &rt, private);
93 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
100 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
104 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
107 /* Repeat until thread terminated. */
108 while ((tid = *tidp) != 0)
113 /* Get the current time. */
114 (void) gettimeofday (&tv, NULL);
116 /* Compute relative timeout. */
117 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
118 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
121 rt.tv_nsec += 1000000000;
125 /* Already timed out? */
129 /* Wait until thread terminates. */
130 // XYZ: Lost the lock to check whether it was private.
131 if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)