thr_umtx.c revision 144518
1144518Sdavidxu/*
2144518Sdavidxu * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3144518Sdavidxu * All rights reserved.
4144518Sdavidxu *
5144518Sdavidxu * Redistribution and use in source and binary forms, with or without
6144518Sdavidxu * modification, are permitted provided that the following conditions
7144518Sdavidxu * are met:
8144518Sdavidxu * 1. Redistributions of source code must retain the above copyright
9144518Sdavidxu *    notice unmodified, this list of conditions, and the following
10144518Sdavidxu *    disclaimer.
11144518Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
12144518Sdavidxu *    notice, this list of conditions and the following disclaimer in the
13144518Sdavidxu *    documentation and/or other materials provided with the distribution.
14144518Sdavidxu *
15144518Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16144518Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17144518Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18144518Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19144518Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20144518Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21144518Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22144518Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23144518Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24144518Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25144518Sdavidxu *
26144518Sdavidxu * $FreeBSD: head/lib/libthr/thread/thr_umtx.c 144518 2005-04-02 01:20:00Z davidxu $
27144518Sdavidxu *
28144518Sdavidxu */
29144518Sdavidxu
30144518Sdavidxu#include "thr_private.h"
31144518Sdavidxu#include "thr_umtx.h"
32144518Sdavidxu
33144518Sdavidxuint
34144518Sdavidxu__thr_umtx_lock(volatile umtx_t *mtx, long id)
35144518Sdavidxu{
36144518Sdavidxu	while (_umtx_op((struct umtx *)mtx, UMTX_OP_LOCK, id, 0, 0))
37144518Sdavidxu		;
38144518Sdavidxu	return (0);
39144518Sdavidxu}
40144518Sdavidxu
41144518Sdavidxuint
42144518Sdavidxu__thr_umtx_timedlock(volatile umtx_t *mtx, long id,
43144518Sdavidxu	const struct timespec *timeout)
44144518Sdavidxu{
45144518Sdavidxu	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
46144518Sdavidxu		timeout->tv_nsec <= 0)))
47144518Sdavidxu		return (ETIMEDOUT);
48144518Sdavidxu	if (_umtx_op((struct umtx *)mtx, UMTX_OP_LOCK, id, 0,
49144518Sdavidxu		(void *)timeout) == 0)
50144518Sdavidxu		return (0);
51144518Sdavidxu	return (errno);
52144518Sdavidxu}
53144518Sdavidxu
54144518Sdavidxuint
55144518Sdavidxu__thr_umtx_unlock(volatile umtx_t *mtx, long id)
56144518Sdavidxu{
57144518Sdavidxu	if (_umtx_op((struct umtx *)mtx, UMTX_OP_UNLOCK, id, 0, 0) == 0)
58144518Sdavidxu		return (0);
59144518Sdavidxu	return (errno);
60144518Sdavidxu}
61144518Sdavidxu
62144518Sdavidxuint
63144518Sdavidxu_thr_umtx_wait(volatile umtx_t *mtx, long id, const struct timespec *timeout)
64144518Sdavidxu{
65144518Sdavidxu	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
66144518Sdavidxu		timeout->tv_nsec <= 0)))
67144518Sdavidxu		return (ETIMEDOUT);
68144518Sdavidxu	if (_umtx_op((struct umtx *)mtx, UMTX_OP_WAIT, id, 0,
69144518Sdavidxu		(void*) timeout) == 0)
70144518Sdavidxu		return (0);
71144518Sdavidxu	return (errno);
72144518Sdavidxu}
73144518Sdavidxu
74144518Sdavidxuint
75144518Sdavidxu_thr_umtx_wake(volatile umtx_t *mtx, int nr_wakeup)
76144518Sdavidxu{
77144518Sdavidxu	if (_umtx_op((struct umtx *)mtx, UMTX_OP_WAKE, nr_wakeup, 0, 0) == 0)
78144518Sdavidxu		return (0);
79144518Sdavidxu	return (errno);
80144518Sdavidxu}
81