1a091d823SDavid Xu /*- 2a091d823SDavid Xu * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 3a091d823SDavid Xu * All rights reserved. 4a091d823SDavid Xu * 5a091d823SDavid Xu * Redistribution and use in source and binary forms, with or without 6a091d823SDavid Xu * modification, are permitted provided that the following conditions 7a091d823SDavid Xu * are met: 8a091d823SDavid Xu * 1. Redistributions of source code must retain the above copyright 9a091d823SDavid Xu * notice, this list of conditions and the following disclaimer. 10a091d823SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright 11a091d823SDavid Xu * notice, this list of conditions and the following disclaimer in the 12a091d823SDavid Xu * documentation and/or other materials provided with the distribution. 13a091d823SDavid Xu * 14a091d823SDavid Xu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15a091d823SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a091d823SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17a091d823SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18a091d823SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19a091d823SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20a091d823SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21a091d823SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22a091d823SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23a091d823SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24a091d823SDavid Xu * SUCH DAMAGE. 25a091d823SDavid Xu * 26a091d823SDavid Xu * $FreeBSD$ 27a091d823SDavid Xu */ 28a091d823SDavid Xu 29a091d823SDavid Xu #ifndef _THR_FBSD_UMTX_H_ 30a091d823SDavid Xu #define _THR_FBSD_UMTX_H_ 31a091d823SDavid Xu 32a091d823SDavid Xu #include <sys/umtx.h> 33a091d823SDavid Xu 34a091d823SDavid Xu typedef long umtx_t; 35a091d823SDavid Xu 368429e734SDavid Xu int __thr_umtx_lock(volatile umtx_t *mtx, long id) __hidden; 37a091d823SDavid Xu int __thr_umtx_timedlock(volatile umtx_t *mtx, long id, 388429e734SDavid Xu const struct timespec *timeout) __hidden; 398429e734SDavid Xu int __thr_umtx_unlock(volatile umtx_t *mtx, long id) __hidden; 40a091d823SDavid Xu 41a091d823SDavid Xu static inline void 42a091d823SDavid Xu _thr_umtx_init(volatile umtx_t *mtx) 43a091d823SDavid Xu { 44a091d823SDavid Xu *mtx = 0; 45a091d823SDavid Xu } 46a091d823SDavid Xu 47a091d823SDavid Xu static inline int 48a091d823SDavid Xu _thr_umtx_trylock(volatile umtx_t *mtx, long id) 49a091d823SDavid Xu { 502ff77b92SDavid Xu if (atomic_cmpset_acq_ptr((volatile uintptr_t *)mtx, 512ff77b92SDavid Xu (uintptr_t)UMTX_UNOWNED, (uintptr_t)id)) 522ff77b92SDavid Xu return (0); 532ff77b92SDavid Xu return (EBUSY); 54a091d823SDavid Xu } 55a091d823SDavid Xu 56a091d823SDavid Xu static inline int 57a091d823SDavid Xu _thr_umtx_lock(volatile umtx_t *mtx, long id) 58a091d823SDavid Xu { 592ff77b92SDavid Xu if (atomic_cmpset_acq_ptr((volatile uintptr_t *)mtx, 602ff77b92SDavid Xu (uintptr_t)UMTX_UNOWNED, (uintptr_t)id)) 61a091d823SDavid Xu return (0); 622ff77b92SDavid Xu return (__thr_umtx_lock(mtx, id)); 63a091d823SDavid Xu } 64a091d823SDavid Xu 65a091d823SDavid Xu static inline int 66a091d823SDavid Xu _thr_umtx_timedlock(volatile umtx_t *mtx, long id, 67a091d823SDavid Xu const struct timespec *timeout) 68a091d823SDavid Xu { 692ff77b92SDavid Xu if (atomic_cmpset_acq_ptr((volatile uintptr_t *)mtx, 702ff77b92SDavid Xu (uintptr_t)UMTX_UNOWNED, (uintptr_t)id)) 71a091d823SDavid Xu return (0); 722ff77b92SDavid Xu return (__thr_umtx_timedlock(mtx, id, timeout)); 73a091d823SDavid Xu } 74a091d823SDavid Xu 75a091d823SDavid Xu static inline int 76a091d823SDavid Xu _thr_umtx_unlock(volatile umtx_t *mtx, long id) 77a091d823SDavid Xu { 782ff77b92SDavid Xu if (atomic_cmpset_rel_ptr((volatile uintptr_t *)mtx, 792ff77b92SDavid Xu (uintptr_t)id, (uintptr_t)UMTX_UNOWNED)) 80a091d823SDavid Xu return (0); 81a091d823SDavid Xu return __thr_umtx_unlock(mtx, id); 82a091d823SDavid Xu } 83a091d823SDavid Xu 84a091d823SDavid Xu int _thr_umtx_wait(volatile umtx_t *mtx, umtx_t exp, 858429e734SDavid Xu const struct timespec *timeout) __hidden; 868429e734SDavid Xu int _thr_umtx_wake(volatile umtx_t *mtx, int count) __hidden; 87a091d823SDavid Xu #endif 88