thr_umtx.c revision 164877
1164190Sjkoshy/*
2164190Sjkoshy * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice unmodified, this list of conditions, and the following
10164190Sjkoshy *    disclaimer.
11164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
12164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
13164190Sjkoshy *    documentation and/or other materials provided with the distribution.
14164190Sjkoshy *
15164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16164190Sjkoshy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17164190Sjkoshy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18164190Sjkoshy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19164190Sjkoshy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20164190Sjkoshy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21164190Sjkoshy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22164190Sjkoshy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23164190Sjkoshy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24164190Sjkoshy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25164190Sjkoshy *
26164190Sjkoshy * $FreeBSD: head/lib/libthr/thread/thr_umtx.c 164877 2006-12-04 14:20:41Z davidxu $
27164190Sjkoshy *
28164190Sjkoshy */
29164190Sjkoshy
30164190Sjkoshy#include "thr_private.h"
31164190Sjkoshy#include "thr_umtx.h"
32164190Sjkoshy
33164190Sjkoshyvoid
34164190Sjkoshy_thr_umutex_init(struct umutex *mtx)
35164190Sjkoshy{
36164190Sjkoshy	static struct umutex default_mtx = DEFAULT_UMUTEX;
37164190Sjkoshy
38164190Sjkoshy	*mtx = default_mtx;
39164190Sjkoshy}
40164190Sjkoshy
41164190Sjkoshyint
42164190Sjkoshy__thr_umutex_lock(struct umutex *mtx)
43164190Sjkoshy{
44164190Sjkoshy	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0) == 0)
45164190Sjkoshy		return 0;
46164190Sjkoshy	return (errno);
47164190Sjkoshy}
48164190Sjkoshy
49164190Sjkoshyint
50164190Sjkoshy__thr_umutex_timedlock(struct umutex *mtx,
51164190Sjkoshy	const struct timespec *timeout)
52164190Sjkoshy{
53164190Sjkoshy	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
54164190Sjkoshy		timeout->tv_nsec <= 0)))
55164190Sjkoshy		return (ETIMEDOUT);
56164190Sjkoshy	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0,
57164190Sjkoshy		__DECONST(void *, timeout)) == 0)
58164190Sjkoshy		return (0);
59164190Sjkoshy	return (errno);
60164190Sjkoshy}
61164190Sjkoshy
62164190Sjkoshyint
63164190Sjkoshy__thr_umutex_unlock(struct umutex *mtx)
64164190Sjkoshy{
65164190Sjkoshy	if (_umtx_op(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0) == 0)
66164190Sjkoshy		return (0);
67164190Sjkoshy	return (errno);
68164190Sjkoshy}
69164190Sjkoshy
70164190Sjkoshyint
71164190Sjkoshy__thr_umutex_trylock(struct umutex *mtx)
72164190Sjkoshy{
73164190Sjkoshy	if (_umtx_op(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0) == 0)
74164190Sjkoshy		return (0);
75164190Sjkoshy	return (errno);
76164190Sjkoshy}
77164190Sjkoshy
78164190Sjkoshyint
79164190Sjkoshy__thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
80164190Sjkoshy	uint32_t *oldceiling)
81164190Sjkoshy{
82164190Sjkoshy	if (_umtx_op(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0) == 0)
83164190Sjkoshy		return (0);
84164190Sjkoshy	return (errno);
85164190Sjkoshy}
86164190Sjkoshy
87164190Sjkoshyint
88164190Sjkoshy_thr_umtx_wait(volatile umtx_t *mtx, long id, const struct timespec *timeout)
89164190Sjkoshy{
90164190Sjkoshy	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
91164190Sjkoshy		timeout->tv_nsec <= 0)))
92164190Sjkoshy		return (ETIMEDOUT);
93164190Sjkoshy	if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
94164190Sjkoshy		__DECONST(void*, timeout)) == 0)
95164190Sjkoshy		return (0);
96164190Sjkoshy	return (errno);
97164190Sjkoshy}
98164190Sjkoshy
99164190Sjkoshyint
100164190Sjkoshy_thr_umtx_wake(volatile umtx_t *mtx, int nr_wakeup)
101164190Sjkoshy{
102164190Sjkoshy	if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAKE,
103164190Sjkoshy		nr_wakeup, 0, 0) == 0)
104164190Sjkoshy		return (0);
105164190Sjkoshy	return (errno);
106164190Sjkoshy}
107164190Sjkoshy
108164190Sjkoshyint
109164190Sjkoshy_thr_ucond_wait(struct ucond *cv, struct umutex *m,
110164190Sjkoshy	const struct timespec *timeout, int check_unparking)
111164190Sjkoshy{
112164190Sjkoshy	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
113164190Sjkoshy	    timeout->tv_nsec <= 0))) {
114164190Sjkoshy		__thr_umutex_unlock(m);
115164190Sjkoshy                return (ETIMEDOUT);
116164190Sjkoshy	}
117164190Sjkoshy	if (_umtx_op(cv, UMTX_OP_CV_WAIT,
118164190Sjkoshy		     check_unparking ? UMTX_CHECK_UNPAKING : 0,
119164190Sjkoshy		     m, __DECONST(void*, timeout)) == 0) {
120164190Sjkoshy		return (0);
121164190Sjkoshy	}
122164190Sjkoshy	return (errno);
123164190Sjkoshy}
124164190Sjkoshy
125164190Sjkoshyint
126164190Sjkoshy_thr_ucond_signal(struct ucond *cv)
127164190Sjkoshy{
128164190Sjkoshy	if (_umtx_op(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL) == 0)
129164190Sjkoshy		return (0);
130164190Sjkoshy	return (errno);
131164190Sjkoshy}
132164190Sjkoshy
133164190Sjkoshyint
134164190Sjkoshy_thr_ucond_broadcast(struct ucond *cv)
135164190Sjkoshy{
136164190Sjkoshy	if (_umtx_op(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL) == 0)
137164190Sjkoshy		return (0);
138164190Sjkoshy	return (errno);
139164190Sjkoshy}
140164190Sjkoshy