initial commit
[glibc.git] / sysdeps / unix / bsd / bsd4.4 / kfreebsd / fbtl / lowlevellock.c
1 /* low level locking for pthread library. Generic futex-using version.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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
24 void
25 __lll_lock_wait_private (int *futex)
26 {
27 do
28 {
29 int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
30 if (oldval != 0)
31 lll_futex_wait (futex, 2, LLL_PRIVATE);
32 }
33 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
34 }
35
36
37 /* These functions don't get included in libc.so */
38 #if IS_IN (libpthread)
39 void
40 __lll_lock_wait_shared (int *futex)
41 {
42 do
43 {
44 int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
45 if (oldval != 0)
46 lll_futex_wait (futex, 2, LLL_SHARED);
47 }
48 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
49 }
50
51
52 int
53 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
54 {
55 struct timespec rt;
56
57 /* Reject invalid timeouts. */
58 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
59 return EINVAL;
60
61 /* Upgrade the lock. */
62 if (atomic_exchange_acq (futex, 2) == 0)
63 return 0;
64
65 do
66 {
67 struct timeval tv;
68
69 /* Get the current time. */
70 (void) __gettimeofday (&tv, NULL);
71
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)
76 {
77 rt.tv_nsec += 1000000000;
78 --rt.tv_sec;
79 }
80
81 /* Already timed out? */
82 if (rt.tv_sec < 0)
83 return ETIMEDOUT;
84
85 // XYZ: Lost the lock to check whether it was private.
86 lll_futex_timed_wait (futex, 2, &rt, private);
87 }
88 while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
89
90 return 0;
91 }
92
93 int
94 __lll_timedwait_tid (long *tidp, const struct timespec *abstime)
95 {
96 long tid;
97
98 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
99 return EINVAL;
100
101 /* Repeat until thread terminated. */
102 while ((tid = *tidp) != KTID_TERMINATED)
103 {
104 struct timeval tv;
105 struct timespec rt;
106
107 /* Get the current time. */
108 (void) __gettimeofday (&tv, NULL);
109
110 /* Compute relative timeout. */
111 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
112 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
113 if (rt.tv_nsec < 0)
114 {
115 rt.tv_nsec += 1000000000;
116 --rt.tv_sec;
117 }
118
119 /* Already timed out? */
120 if (rt.tv_sec < 0)
121 return ETIMEDOUT;
122
123 /* Wait until thread terminates. */
124 lll_umtx_long_wait_shared (tidp, tid, &rt);
125 }
126
127 return 0;
128 }
129 #endif