1 /* 2 * Copyright (c) 2005 David Xu <davidxu@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$ 27 * 28 */ 29 30 #include "thr_private.h" 31 #include "thr_umtx.h" 32 33 #ifndef HAS__UMTX_OP_ERR 34 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2) 35 { 36 if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1) 37 return (errno); 38 return (0); 39 } 40 #endif 41 42 void 43 _thr_umutex_init(struct umutex *mtx) 44 { 45 static struct umutex default_mtx = DEFAULT_UMUTEX; 46 47 *mtx = default_mtx; 48 } 49 50 void 51 _thr_urwlock_init(struct urwlock *rwl) 52 { 53 static struct urwlock default_rwl = DEFAULT_URWLOCK; 54 *rwl = default_rwl; 55 } 56 57 int 58 __thr_umutex_lock(struct umutex *mtx, uint32_t id) 59 { 60 uint32_t owner; 61 62 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) { 63 for (;;) { 64 /* wait in kernel */ 65 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0); 66 67 owner = mtx->m_owner; 68 if ((owner & ~UMUTEX_CONTESTED) == 0 && 69 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner)) 70 return (0); 71 } 72 } 73 74 return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0); 75 } 76 77 int 78 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id, 79 const struct timespec *ets) 80 { 81 struct timespec timo, cts; 82 uint32_t owner; 83 int ret; 84 85 clock_gettime(CLOCK_REALTIME, &cts); 86 TIMESPEC_SUB(&timo, ets, &cts); 87 88 if (timo.tv_sec < 0) 89 return (ETIMEDOUT); 90 91 for (;;) { 92 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) { 93 94 /* wait in kernel */ 95 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, &timo); 96 97 /* now try to lock it */ 98 owner = mtx->m_owner; 99 if ((owner & ~UMUTEX_CONTESTED) == 0 && 100 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner)) 101 return (0); 102 } else { 103 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, &timo); 104 if (ret == 0) 105 break; 106 } 107 if (ret == ETIMEDOUT) 108 break; 109 clock_gettime(CLOCK_REALTIME, &cts); 110 TIMESPEC_SUB(&timo, ets, &cts); 111 if (timo.tv_sec < 0 || (timo.tv_sec == 0 && timo.tv_nsec == 0)) { 112 ret = ETIMEDOUT; 113 break; 114 } 115 } 116 return (ret); 117 } 118 119 int 120 __thr_umutex_unlock(struct umutex *mtx, uint32_t id) 121 { 122 #ifndef __ia64__ 123 /* XXX this logic has a race-condition on ia64. */ 124 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) { 125 atomic_cmpset_rel_32(&mtx->m_owner, id | UMUTEX_CONTESTED, UMUTEX_CONTESTED); 126 return _umtx_op_err(mtx, UMTX_OP_MUTEX_WAKE, 0, 0, 0); 127 } 128 #endif /* __ia64__ */ 129 return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0); 130 } 131 132 int 133 __thr_umutex_trylock(struct umutex *mtx) 134 { 135 return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0); 136 } 137 138 int 139 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling, 140 uint32_t *oldceiling) 141 { 142 return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0); 143 } 144 145 int 146 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout) 147 { 148 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 && 149 timeout->tv_nsec <= 0))) 150 return (ETIMEDOUT); 151 return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0, 152 __DECONST(void*, timeout)); 153 } 154 155 int 156 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared) 157 { 158 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 && 159 timeout->tv_nsec <= 0))) 160 return (ETIMEDOUT); 161 return _umtx_op_err(__DEVOLATILE(void *, mtx), 162 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0, 163 __DECONST(void*, timeout)); 164 } 165 166 int 167 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared) 168 { 169 return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE, 170 nr_wakeup, 0, 0); 171 } 172 173 void 174 _thr_ucond_init(struct ucond *cv) 175 { 176 bzero(cv, sizeof(struct ucond)); 177 } 178 179 int 180 _thr_ucond_wait(struct ucond *cv, struct umutex *m, 181 const struct timespec *timeout, int check_unparking) 182 { 183 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 && 184 timeout->tv_nsec <= 0))) { 185 struct pthread *curthread = _get_curthread(); 186 _thr_umutex_unlock(m, TID(curthread)); 187 return (ETIMEDOUT); 188 } 189 return _umtx_op_err(cv, UMTX_OP_CV_WAIT, 190 check_unparking ? UMTX_CHECK_UNPARKING : 0, 191 m, __DECONST(void*, timeout)); 192 } 193 194 int 195 _thr_ucond_signal(struct ucond *cv) 196 { 197 if (!cv->c_has_waiters) 198 return (0); 199 return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL); 200 } 201 202 int 203 _thr_ucond_broadcast(struct ucond *cv) 204 { 205 if (!cv->c_has_waiters) 206 return (0); 207 return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL); 208 } 209 210 int 211 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp) 212 { 213 return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp); 214 } 215 216 int 217 __thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp) 218 { 219 return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp); 220 } 221 222 int 223 __thr_rwlock_unlock(struct urwlock *rwlock) 224 { 225 return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL); 226 } 227 228 void 229 _thr_rwl_rdlock(struct urwlock *rwlock) 230 { 231 int ret; 232 233 for (;;) { 234 if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0) 235 return; 236 ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL); 237 if (ret == 0) 238 return; 239 if (ret != EINTR) 240 PANIC("rdlock error"); 241 } 242 } 243 244 void 245 _thr_rwl_wrlock(struct urwlock *rwlock) 246 { 247 int ret; 248 249 for (;;) { 250 if (_thr_rwlock_trywrlock(rwlock) == 0) 251 return; 252 ret = __thr_rwlock_wrlock(rwlock, NULL); 253 if (ret == 0) 254 return; 255 if (ret != EINTR) 256 PANIC("wrlock error"); 257 } 258 } 259 260 void 261 _thr_rwl_unlock(struct urwlock *rwlock) 262 { 263 if (_thr_rwlock_unlock(rwlock)) 264 PANIC("unlock error"); 265 } 266