]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libthr/thread/thr_umtx.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libthr / thread / thr_umtx.c
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29
30 #include "thr_private.h"
31 #include "thr_umtx.h"
32
33 #ifndef HAS__UMTX_OP_ERR
34 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
35 {
36         if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
37                 return (errno);
38         return (0);
39 }
40 #endif
41
42 void
43 _thr_umutex_init(struct umutex *mtx)
44 {
45         static struct umutex default_mtx = DEFAULT_UMUTEX;
46
47         *mtx = default_mtx;
48 }
49
50 void
51 _thr_urwlock_init(struct urwlock *rwl)
52 {
53         static struct urwlock default_rwl = DEFAULT_URWLOCK;
54         *rwl = default_rwl;
55 }
56
57 int
58 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
59 {
60         uint32_t owner;
61
62         if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
63                 for (;;) {
64                         /* wait in kernel */
65                         _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
66
67                         owner = mtx->m_owner;
68                         if ((owner & ~UMUTEX_CONTESTED) == 0 &&
69                              atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
70                                 return (0);
71                 }
72         }
73
74         return  _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
75 }
76
77 #define SPINLOOPS 1000
78
79 int
80 __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id)
81 {
82         uint32_t owner;
83
84         if (!_thr_is_smp)
85                 return __thr_umutex_lock(mtx, id);
86
87         if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
88                 for (;;) {
89                         int count = SPINLOOPS;
90                         while (count--) {
91                                 owner = mtx->m_owner;
92                                 if ((owner & ~UMUTEX_CONTESTED) == 0) {
93                                         if (atomic_cmpset_acq_32(
94                                             &mtx->m_owner,
95                                             owner, id|owner)) {
96                                                 return (0);
97                                         }
98                                 }
99                                 CPU_SPINWAIT;
100                         }
101
102                         /* wait in kernel */
103                         _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
104                 }
105         }
106
107         return  _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
108 }
109
110 int
111 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
112         const struct timespec *ets)
113 {
114         struct timespec timo, cts;
115         uint32_t owner;
116         int ret;
117
118         clock_gettime(CLOCK_REALTIME, &cts);
119         TIMESPEC_SUB(&timo, ets, &cts);
120
121         if (timo.tv_sec < 0)
122                 return (ETIMEDOUT);
123
124         for (;;) {
125                 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
126
127                         /* wait in kernel */
128                         ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, &timo);
129
130                         /* now try to lock it */
131                         owner = mtx->m_owner;
132                         if ((owner & ~UMUTEX_CONTESTED) == 0 &&
133                              atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
134                                 return (0);
135                 } else {
136                         ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, &timo);
137                         if (ret == 0)
138                                 break;
139                 }
140                 if (ret == ETIMEDOUT)
141                         break;
142                 clock_gettime(CLOCK_REALTIME, &cts);
143                 TIMESPEC_SUB(&timo, ets, &cts);
144                 if (timo.tv_sec < 0 || (timo.tv_sec == 0 && timo.tv_nsec == 0)) {
145                         ret = ETIMEDOUT;
146                         break;
147                 }
148         }
149         return (ret);
150 }
151
152 int
153 __thr_umutex_unlock(struct umutex *mtx, uint32_t id)
154 {
155         static int wake2_avail = 0;
156
157         if (__predict_false(wake2_avail == 0)) {
158                 struct umutex test = DEFAULT_UMUTEX;
159
160                 if (_umtx_op(&test, UMTX_OP_MUTEX_WAKE2, test.m_flags, 0, 0) == -1)
161                         wake2_avail = -1;
162                 else 
163                         wake2_avail = 1;
164         }
165
166         if (wake2_avail != 1)
167                 goto unlock;
168
169         uint32_t flags = mtx->m_flags;
170
171         if ((flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
172                 uint32_t owner;
173                 do {
174                         owner = mtx->m_owner;
175                         if (__predict_false((owner & ~UMUTEX_CONTESTED) != id))
176                                 return (EPERM);
177                 } while (__predict_false(!atomic_cmpset_rel_32(&mtx->m_owner,
178                                          owner, UMUTEX_UNOWNED)));
179                 if ((owner & UMUTEX_CONTESTED))
180                         (void)_umtx_op_err(mtx, UMTX_OP_MUTEX_WAKE2, flags, 0, 0);
181                 return (0);
182         }
183 unlock:
184         return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0);
185 }
186
187 int
188 __thr_umutex_trylock(struct umutex *mtx)
189 {
190         return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0);
191 }
192
193 int
194 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
195         uint32_t *oldceiling)
196 {
197         return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0);
198 }
199
200 int
201 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
202 {
203         if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
204                 timeout->tv_nsec <= 0)))
205                 return (ETIMEDOUT);
206         return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
207                 __DECONST(void*, timeout));
208 }
209
210 int
211 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared)
212 {
213         if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
214                 timeout->tv_nsec <= 0)))
215                 return (ETIMEDOUT);
216         return _umtx_op_err(__DEVOLATILE(void *, mtx), 
217                         shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
218                         __DECONST(void*, timeout));
219 }
220
221 int
222 _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int id, int clockid,
223         const struct timespec *abstime, int shared)
224 {
225         struct timespec ts, ts2, *tsp;
226
227         if (abstime != NULL) {
228                 clock_gettime(clockid, &ts);
229                 TIMESPEC_SUB(&ts2, abstime, &ts);
230                 if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0))
231                         return (ETIMEDOUT);
232                 tsp = &ts2;
233         } else {
234                 tsp = NULL;
235         }
236         return _umtx_op_err(__DEVOLATILE(void *, mtx), 
237                 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, NULL,
238                         tsp);
239 }
240
241 int
242 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
243 {
244         return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE,
245                 nr_wakeup, 0, 0);
246 }
247
248 void
249 _thr_ucond_init(struct ucond *cv)
250 {
251         bzero(cv, sizeof(struct ucond));
252 }
253
254 int
255 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
256         const struct timespec *timeout, int flags)
257 {
258         if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
259             timeout->tv_nsec <= 0))) {
260                 struct pthread *curthread = _get_curthread();
261                 _thr_umutex_unlock(m, TID(curthread));
262                 return (ETIMEDOUT);
263         }
264         return _umtx_op_err(cv, UMTX_OP_CV_WAIT, flags,
265                      m, __DECONST(void*, timeout));
266 }
267  
268 int
269 _thr_ucond_signal(struct ucond *cv)
270 {
271         if (!cv->c_has_waiters)
272                 return (0);
273         return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL);
274 }
275
276 int
277 _thr_ucond_broadcast(struct ucond *cv)
278 {
279         if (!cv->c_has_waiters)
280                 return (0);
281         return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL);
282 }
283
284 int
285 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
286 {
287         return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp);
288 }
289
290 int
291 __thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
292 {
293         return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp);
294 }
295
296 int
297 __thr_rwlock_unlock(struct urwlock *rwlock)
298 {
299         return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL);
300 }
301
302 void
303 _thr_rwl_rdlock(struct urwlock *rwlock)
304 {
305         int ret;
306
307         for (;;) {
308                 if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0)
309                         return;
310                 ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL);
311                 if (ret == 0)
312                         return;
313                 if (ret != EINTR)
314                         PANIC("rdlock error");
315         }
316 }
317
318 void
319 _thr_rwl_wrlock(struct urwlock *rwlock)
320 {
321         int ret;
322
323         for (;;) {
324                 if (_thr_rwlock_trywrlock(rwlock) == 0)
325                         return;
326                 ret = __thr_rwlock_wrlock(rwlock, NULL);
327                 if (ret == 0)
328                         return;
329                 if (ret != EINTR)
330                         PANIC("wrlock error");
331         }
332 }
333
334 void
335 _thr_rwl_unlock(struct urwlock *rwlock)
336 {
337         if (_thr_rwlock_unlock(rwlock))
338                 PANIC("unlock error");
339 }