1 /* low level locking for pthread library. SPARC version.
2 Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <sys/time.h>
24 #include <tls.h>
25
26
27 void
28 #ifndef IS_IN_libpthread
29 weak_function
30 #endif
__lll_lock_wait_private(int * futex)31 __lll_lock_wait_private (int *futex)
32 {
33 do
34 {
35 int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1);
36 if (oldval != 0)
37 lll_futex_wait (futex, 2, LLL_PRIVATE);
38 }
39 while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0);
40 }
41
42
43 /* These functions don't get included in libc.so */
44 #ifdef IS_IN_libpthread
45 void
__lll_lock_wait(int * futex,int private)46 __lll_lock_wait (int *futex, int private)
47 {
48 do
49 {
50 int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1);
51 if (oldval != 0)
52 lll_futex_wait (futex, 2, private);
53 }
54 while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0);
55 }
56
57
58 int
__lll_timedlock_wait(int * futex,const struct timespec * abstime,int private)59 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
60 {
61 /* Reject invalid timeouts. */
62 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
63 return EINVAL;
64
65 do
66 {
67 struct timeval tv;
68 struct timespec rt;
69
70 /* Get the current time. */
71 (void) gettimeofday (&tv, NULL);
72
73 /* Compute relative timeout. */
74 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
75 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
76 if (rt.tv_nsec < 0)
77 {
78 rt.tv_nsec += 1000000000;
79 --rt.tv_sec;
80 }
81
82 /* Already timed out? */
83 if (rt.tv_sec < 0)
84 return ETIMEDOUT;
85
86 /* Wait. */
87 int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1);
88 if (oldval != 0)
89 lll_futex_timed_wait (futex, 2, &rt, private);
90 }
91 while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0);
92
93 return 0;
94 }
95
96
97 int
__lll_timedwait_tid(int * tidp,const struct timespec * abstime)98 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
99 {
100 int tid;
101
102 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
103 return EINVAL;
104
105 /* Repeat until thread terminated. */
106 while ((tid = *tidp) != 0)
107 {
108 struct timeval tv;
109 struct timespec rt;
110
111 /* Get the current time. */
112 (void) gettimeofday (&tv, NULL);
113
114 /* Compute relative timeout. */
115 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
116 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
117 if (rt.tv_nsec < 0)
118 {
119 rt.tv_nsec += 1000000000;
120 --rt.tv_sec;
121 }
122
123 /* Already timed out? */
124 if (rt.tv_sec < 0)
125 return ETIMEDOUT;
126
127 /* Wait until thread terminates. The kernel so far does not use
128 the private futex operations for this. */
129 if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
130 return ETIMEDOUT;
131 }
132
133 return 0;
134 }
135 #endif
136