thr_umtx.c revision 162061
161822Sgreen/*
255992Swpaul * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
355992Swpaul * All rights reserved.
455992Swpaul *
555992Swpaul * Redistribution and use in source and binary forms, with or without
655992Swpaul * modification, are permitted provided that the following conditions
755992Swpaul * are met:
855992Swpaul * 1. Redistributions of source code must retain the above copyright
955992Swpaul *    notice unmodified, this list of conditions, and the following
1055992Swpaul *    disclaimer.
1155992Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1255992Swpaul *    notice, this list of conditions and the following disclaimer in the
1355992Swpaul *    documentation and/or other materials provided with the distribution.
1455992Swpaul *
1555992Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1655992Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1755992Swpaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1855992Swpaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1955992Swpaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2055992Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2155992Swpaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2255992Swpaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2355992Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2455992Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2555992Swpaul *
2655992Swpaul * $FreeBSD: head/lib/libthr/thread/thr_umtx.c 162061 2006-09-06 04:04:10Z davidxu $
2755992Swpaul *
2855992Swpaul */
2955992Swpaul
3055992Swpaul#include "thr_private.h"
3155992Swpaul#include "thr_umtx.h"
3255992Swpaul
33114601Sobrienint
3481586Sru__thr_umutex_lock(struct umutex *mtx, uint32_t id)
3581586Sru{
3681586Sru	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0) == 0)
3781586Sru		return 0;
38114601Sobrien	return (errno);
39114601Sobrien}
40114601Sobrien
4181586Sruint
4255992Swpaul__thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
4355992Swpaul	const struct timespec *timeout)
4455992Swpaul{
4555992Swpaul	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
4690868Smike		timeout->tv_nsec <= 0)))
4790868Smike		return (ETIMEDOUT);
4855992Swpaul	if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0,
4955992Swpaul		__DECONST(void *, timeout)) == 0)
5055992Swpaul		return (0);
5155992Swpaul	return (errno);
5255992Swpaul}
5355992Swpaul
5455992Swpaulint
5555992Swpaul__thr_umutex_unlock(struct umutex *mtx, uint32_t id)
5655992Swpaul{
5755992Swpaul	if (_umtx_op(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0) == 0)
5855992Swpaul		return (0);
5988748Sambrisko	return (errno);
60108401Sambrisko}
6155992Swpaul
62108401Sambriskoint
6399800Salfred__thr_umutex_kern_trylock(struct umutex *mtx)
64132860Snjl{
65132860Snjl	if (_umtx_op(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0) == 0)
6699800Salfred		return (0);
67132860Snjl	return (errno);
6899800Salfred}
6999800Salfred
7099800Salfredint
7199800Salfred__thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
7299800Salfred	uint32_t *oldceiling)
7399800Salfred{
7499800Salfred	if (_umtx_op(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0) == 0)
7599800Salfred		return (0);
7699800Salfred	return (errno);
7799800Salfred}
7899800Salfred
7999800Salfredint
8055992Swpaul_thr_umtx_wait(volatile umtx_t *mtx, long id, const struct timespec *timeout)
8199800Salfred{
8299800Salfred	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
8355992Swpaul		timeout->tv_nsec <= 0)))
8499800Salfred		return (ETIMEDOUT);
85132860Snjl	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_WAIT, id, 0,
86132860Snjl		__DECONST(void*, timeout)) == 0)
87132860Snjl		return (0);
88132860Snjl	return (errno);
89108401Sambrisko}
90132860Snjl
9155992Swpaulint
9255992Swpaul_thr_umtx_wake(volatile umtx_t *mtx, int nr_wakeup)
9355992Swpaul{
9455992Swpaul	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_WAKE,
9555992Swpaul		nr_wakeup, 0, 0) == 0)
9655992Swpaul		return (0);
9755992Swpaul	return (errno);
9855992Swpaul}
9955992Swpaul