thr_umtx.c revision 161680
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 161680 2006-08-28 04:47:27Z davidxu $
27 *
28 */
29
30#include "thr_private.h"
31#include "thr_umtx.h"
32
33int
34__thr_umtx_lock(volatile umtx_t *mtx, long id)
35{
36	 while (_umtx_op(__DEVOLATILE(struct umtx *, mtx),
37		UMTX_OP_LOCK, id, 0, 0))
38		;
39	return (0);
40}
41
42int
43__thr_umtx_timedlock(volatile umtx_t *mtx, long id,
44	const struct timespec *timeout)
45{
46	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
47		timeout->tv_nsec <= 0)))
48		return (ETIMEDOUT);
49	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_LOCK, id, 0,
50		__DECONST(void *, timeout)) == 0)
51		return (0);
52	return (errno);
53}
54
55int
56__thr_umtx_unlock(volatile umtx_t *mtx, long id)
57{
58	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_UNLOCK,
59		id, 0, 0) == 0)
60		return (0);
61	return (errno);
62}
63
64int
65__thr_umutex_lock(struct umutex *mtx, uint32_t id)
66{
67	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0) == 0)
68		return 0;
69	return (errno);
70}
71
72int
73__thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
74	const struct timespec *timeout)
75{
76	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
77		timeout->tv_nsec <= 0)))
78		return (ETIMEDOUT);
79	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0,
80		__DECONST(void *, timeout)) == 0)
81		return (0);
82	return (errno);
83}
84
85int
86__thr_umutex_unlock(struct umutex *mtx, uint32_t id)
87{
88	if (_umtx_op(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0) == 0)
89		return (0);
90	return (errno);
91}
92
93int
94__thr_umutex_kern_trylock(struct umutex *mtx)
95{
96	if (_umtx_op(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0) == 0)
97		return (0);
98	return (errno);
99}
100
101int
102__thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
103	uint32_t *oldceiling)
104{
105	if (_umtx_op(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0) == 0)
106		return (0);
107	return (errno);
108}
109
110int
111_thr_umtx_wait(volatile umtx_t *mtx, long id, const struct timespec *timeout)
112{
113	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
114		timeout->tv_nsec <= 0)))
115		return (ETIMEDOUT);
116	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_WAIT, id, 0,
117		__DECONST(void*, timeout)) == 0)
118		return (0);
119	return (errno);
120}
121
122int
123_thr_umtx_wake(volatile umtx_t *mtx, int nr_wakeup)
124{
125	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_WAKE,
126		nr_wakeup, 0, 0) == 0)
127		return (0);
128	return (errno);
129}
130