thr_umtx.c revision 177850
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: head/lib/libthr/thread/thr_umtx.c 177850 2008-04-02 04:32:31Z davidxu $
27 *
28 */
29
30#include "thr_private.h"
31#include "thr_umtx.h"
32
33void
34_thr_umutex_init(struct umutex *mtx)
35{
36	static struct umutex default_mtx = DEFAULT_UMUTEX;
37
38	*mtx = default_mtx;
39}
40
41int
42__thr_umutex_lock(struct umutex *mtx)
43{
44	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0) != -1)
45		return 0;
46	return (errno);
47}
48
49int
50__thr_umutex_timedlock(struct umutex *mtx,
51	const struct timespec *timeout)
52{
53	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
54		timeout->tv_nsec <= 0)))
55		return (ETIMEDOUT);
56	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0,
57		__DECONST(void *, timeout)) != -1)
58		return (0);
59	return (errno);
60}
61
62int
63__thr_umutex_unlock(struct umutex *mtx)
64{
65	if (_umtx_op(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0) != -1)
66		return (0);
67	return (errno);
68}
69
70int
71__thr_umutex_trylock(struct umutex *mtx)
72{
73	if (_umtx_op(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0) != -1)
74		return (0);
75	return (errno);
76}
77
78int
79__thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
80	uint32_t *oldceiling)
81{
82	if (_umtx_op(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0) != -1)
83		return (0);
84	return (errno);
85}
86
87int
88_thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
89{
90	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
91		timeout->tv_nsec <= 0)))
92		return (ETIMEDOUT);
93	if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
94		__DECONST(void*, timeout)) != -1)
95		return (0);
96	return (errno);
97}
98
99int
100_thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout)
101{
102	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
103		timeout->tv_nsec <= 0)))
104		return (ETIMEDOUT);
105	if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT_UINT, id, 0,
106		__DECONST(void*, timeout)) != -1)
107		return (0);
108	return (errno);
109}
110
111int
112_thr_umtx_wake(volatile void *mtx, int nr_wakeup)
113{
114	if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAKE,
115		nr_wakeup, 0, 0) != -1)
116		return (0);
117	return (errno);
118}
119
120void
121_thr_ucond_init(struct ucond *cv)
122{
123	bzero(cv, sizeof(struct ucond));
124}
125
126int
127_thr_ucond_wait(struct ucond *cv, struct umutex *m,
128	const struct timespec *timeout, int check_unparking)
129{
130	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
131	    timeout->tv_nsec <= 0))) {
132		__thr_umutex_unlock(m);
133                return (ETIMEDOUT);
134	}
135	if (_umtx_op(cv, UMTX_OP_CV_WAIT,
136		     check_unparking ? UMTX_CHECK_UNPARKING : 0,
137		     m, __DECONST(void*, timeout)) != -1) {
138		return (0);
139	}
140	return (errno);
141}
142
143int
144_thr_ucond_signal(struct ucond *cv)
145{
146	if (!cv->c_has_waiters)
147		return (0);
148	if (_umtx_op(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL) != -1)
149		return (0);
150	return (errno);
151}
152
153int
154_thr_ucond_broadcast(struct ucond *cv)
155{
156	if (!cv->c_has_waiters)
157		return (0);
158	if (_umtx_op(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL) != -1)
159		return (0);
160	return (errno);
161}
162
163int
164__thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
165{
166	if (_umtx_op(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp) != -1)
167		return (0);
168	return (errno);
169}
170
171int
172__thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
173{
174	if (_umtx_op(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp) != -1)
175		return (0);
176	return (errno);
177}
178
179int
180__thr_rwlock_unlock(struct urwlock *rwlock)
181{
182	if (_umtx_op(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL) != -1)
183		return (0);
184	return (errno);
185}
186