Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / arm / nptl / lowlevellock.c
blob9603d7b32888c08292ea86a363e5f99fa65a8a72
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/>. */
19 #include <errno.h>
20 #include <sysdep.h>
21 #include <lowlevellock.h>
22 #include <sys/time.h>
24 void
25 __lll_lock_wait_private (int *futex)
29 int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
30 if (oldval != 0)
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
39 void
40 __lll_lock_wait (int *futex, int private)
44 int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
45 if (oldval != 0)
46 lll_futex_wait (futex, 2, private);
48 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
52 int
53 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
55 struct timespec rt;
57 /* Reject invalid timeouts. */
58 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
59 return EINVAL;
61 /* Upgrade the lock. */
62 if (atomic_exchange_acq (futex, 2) == 0)
63 return 0;
67 struct timeval tv;
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;
75 if (rt.tv_nsec < 0)
77 rt.tv_nsec += 1000000000;
78 --rt.tv_sec;
81 /* Already timed out? */
82 if (rt.tv_sec < 0)
83 return ETIMEDOUT;
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);
90 return 0;
94 int
95 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
97 int tid;
99 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
100 return EINVAL;
102 /* Repeat until thread terminated. */
103 while ((tid = *tidp) != 0)
105 struct timeval tv;
106 struct timespec rt;
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;
114 if (rt.tv_nsec < 0)
116 rt.tv_nsec += 1000000000;
117 --rt.tv_sec;
120 /* Already timed out? */
121 if (rt.tv_sec < 0)
122 return ETIMEDOUT;
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)
127 return ETIMEDOUT;
130 return 0;
132 #endif