thr_umtx.h revision 165206
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, this list of conditions and the following disclaimer. 10144518Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright 11144518Sdavidxu * notice, this list of conditions and the following disclaimer in the 12144518Sdavidxu * documentation and/or other materials provided with the distribution. 13144518Sdavidxu * 14144518Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15144518Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16144518Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17144518Sdavidxu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18144518Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19144518Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20144518Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21144518Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22144518Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23144518Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24144518Sdavidxu * SUCH DAMAGE. 25144518Sdavidxu * 26144518Sdavidxu * $FreeBSD: head/lib/libthr/thread/thr_umtx.h 165206 2006-12-14 13:22:02Z davidxu $ 27144518Sdavidxu */ 28144518Sdavidxu 29144518Sdavidxu#ifndef _THR_FBSD_UMTX_H_ 30144518Sdavidxu#define _THR_FBSD_UMTX_H_ 31144518Sdavidxu 32162061Sdavidxu#include <strings.h> 33144518Sdavidxu#include <sys/umtx.h> 34144518Sdavidxu 35162061Sdavidxu#define DEFAULT_UMUTEX {0, 0, {0, 0}, {0, 0, 0, 0}} 36162061Sdavidxu 37144518Sdavidxutypedef long umtx_t; 38144518Sdavidxu 39163334Sdavidxuint __thr_umutex_lock(struct umutex *mtx) __hidden; 40163334Sdavidxuint __thr_umutex_timedlock(struct umutex *mtx, 41161680Sdavidxu const struct timespec *timeout) __hidden; 42163334Sdavidxuint __thr_umutex_unlock(struct umutex *mtx) __hidden; 43163334Sdavidxuint __thr_umutex_trylock(struct umutex *mtx) __hidden; 44161680Sdavidxuint __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling, 45161680Sdavidxu uint32_t *oldceiling) __hidden; 46161680Sdavidxu 47163334Sdavidxuvoid _thr_umutex_init(struct umutex *mtx) __hidden; 48162061Sdavidxuint _thr_umtx_wait(volatile umtx_t *mtx, umtx_t exp, 49162061Sdavidxu const struct timespec *timeout) __hidden; 50162061Sdavidxuint _thr_umtx_wake(volatile umtx_t *mtx, int count) __hidden; 51164877Sdavidxuint _thr_ucond_wait(struct ucond *cv, struct umutex *m, 52164902Sdavidxu const struct timespec *timeout, int check_unpaking) __hidden; 53164902Sdavidxuvoid _thr_ucond_init(struct ucond *cv) __hidden; 54164902Sdavidxuint _thr_ucond_signal(struct ucond *cv) __hidden; 55164902Sdavidxuint _thr_ucond_broadcast(struct ucond *cv) __hidden; 56162061Sdavidxu 57144518Sdavidxustatic inline int 58161680Sdavidxu_thr_umutex_trylock(struct umutex *mtx, uint32_t id) 59161680Sdavidxu{ 60161680Sdavidxu if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id)) 61161680Sdavidxu return (0); 62161680Sdavidxu if ((mtx->m_flags & UMUTEX_PRIO_PROTECT) == 0) 63161680Sdavidxu return (EBUSY); 64163334Sdavidxu return (__thr_umutex_trylock(mtx)); 65161680Sdavidxu} 66161680Sdavidxu 67161680Sdavidxustatic inline int 68165206Sdavidxu_thr_umutex_trylock2(struct umutex *mtx, uint32_t id) 69165206Sdavidxu{ 70165206Sdavidxu if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id)) 71165206Sdavidxu return (0); 72165206Sdavidxu return (EBUSY); 73165206Sdavidxu} 74165206Sdavidxu 75165206Sdavidxustatic inline int 76161680Sdavidxu_thr_umutex_lock(struct umutex *mtx, uint32_t id) 77161680Sdavidxu{ 78161680Sdavidxu if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id)) 79161680Sdavidxu return (0); 80163334Sdavidxu return (__thr_umutex_lock(mtx)); 81161680Sdavidxu} 82161680Sdavidxu 83161680Sdavidxustatic inline int 84161680Sdavidxu_thr_umutex_timedlock(struct umutex *mtx, uint32_t id, 85161680Sdavidxu const struct timespec *timeout) 86161680Sdavidxu{ 87161680Sdavidxu if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id)) 88161680Sdavidxu return (0); 89163334Sdavidxu return (__thr_umutex_timedlock(mtx, timeout)); 90161680Sdavidxu} 91161680Sdavidxu 92161680Sdavidxustatic inline int 93161680Sdavidxu_thr_umutex_unlock(struct umutex *mtx, uint32_t id) 94161680Sdavidxu{ 95161680Sdavidxu if (atomic_cmpset_rel_32(&mtx->m_owner, id, UMUTEX_UNOWNED)) 96161680Sdavidxu return (0); 97163334Sdavidxu return (__thr_umutex_unlock(mtx)); 98161680Sdavidxu} 99161680Sdavidxu 100144518Sdavidxu#endif 101