umtx.h revision 139013
1/* 2 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/sys/umtx.h 139013 2004-12-18 12:52:44Z davidxu $ 27 * 28 */ 29 30#ifndef _SYS_UMTX_H_ 31#define _SYS_UMTX_H_ 32 33/* 34 * See pthread_* 35 */ 36 37#define UMTX_UNOWNED 0x0 38 39struct umtx { 40 void *u_owner; /* Owner of the mutex. */ 41}; 42 43/* op code for _umtx_op */ 44#define UMTX_OP_LOCK 0 45#define UMTX_OP_UNLOCK 1 46#define UMTX_OP_UNLOCK_AND_WAIT 2 47#define UMTX_OP_WAKE 3 48 49#ifndef _KERNEL 50 51/* 52 * System calls for acquiring and releasing contested mutexes. 53 */ 54/* deprecated becaues it can only use thread id */ 55int _umtx_lock(struct umtx *mtx); 56/* deprecated becaues it can only use thread id */ 57int _umtx_unlock(struct umtx *mtx); 58int _umtx_op(struct umtx *umtx, int op, long id, void *uaddr, 59 struct timespec *abstime); 60 61/* 62 * Standard api. Try uncontested acquire/release and asks the 63 * kernel to resolve failures. 64 */ 65static __inline int 66umtx_lock(struct umtx *umtx, long id) 67{ 68 if (atomic_cmpset_acq_ptr(&umtx->u_owner, (void *)UMTX_UNOWNED, 69 (void *)id) == 0) 70 if (_umtx_op(umtx, UMTX_OP_LOCK, id, 0, 0) == -1) 71 return (errno); 72 return (0); 73} 74 75static __inline int 76umtx_trylock(struct umtx *umtx, long id) 77{ 78 if (atomic_cmpset_acq_ptr(&umtx->u_owner, (void *)UMTX_UNOWNED, 79 (void *)id) == 0) 80 return (EBUSY); 81 return (0); 82} 83 84static __inline int 85umtx_timedlock(struct umtx *umtx, long id, struct timespec *abstime) 86{ 87 if (atomic_cmpset_acq_ptr(&umtx->u_owner, (void *)UMTX_UNOWNED, 88 (void *)id) == 0) 89 if (_umtx_op(umtx, UMTX_OP_LOCK, id, 0, abstime) == -1) 90 return (errno); 91 return (0); 92} 93 94static __inline int 95umtx_unlock(struct umtx *umtx, long id) 96{ 97 if (atomic_cmpset_rel_ptr(&umtx->u_owner, (void *)id, 98 (void *)UMTX_UNOWNED) == 0) 99 if (_umtx_op(umtx, UMTX_OP_UNLOCK, id, 0, 0) == -1) 100 return (errno); 101 return (0); 102} 103 104/* Unlock umtx and wait on a user address. */ 105 106static __inline int 107umtx_wait(struct umtx *umtx, long id, void *uaddr) 108{ 109 if (_umtx_op(umtx, UMTX_OP_UNLOCK_AND_WAIT, id, uaddr, 0) == -1) 110 return (errno); 111 return (0); 112} 113 114static __inline int 115umtx_timedwait(struct umtx *umtx, long id, void *uaddr, 116 struct timespec *abstime) 117{ 118 if (_umtx_op(umtx, UMTX_OP_UNLOCK_AND_WAIT, id, uaddr, abstime) == -1) 119 return (errno); 120 return (0); 121} 122 123/* Wake threads waiting on a user address. */ 124static __inline int 125umtx_wake(void *uaddr, int broadcast) 126{ 127 if (_umtx_op(0, UMTX_OP_WAKE, broadcast, uaddr, 0) == -1) 128 return (errno); 129 return (0); 130} 131 132#endif /* !_KERNEL */ 133#endif /* !_SYS_UMTX_H_ */ 134