thr_rwlock.c (115399) | thr_rwlock.c (119790) |
---|---|
1/*- 2 * Copyright (c) 1998 Alex Nash 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 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 1998 Alex Nash 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 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/lib/libkse/thread/thr_rwlock.c 115399 2003-05-30 00:21:52Z kan $ | 26 * $FreeBSD: head/lib/libkse/thread/thr_rwlock.c 119790 2003-09-06 00:07:52Z davidxu $ |
27 */ 28 29#include <errno.h> 30#include <limits.h> 31#include <stdlib.h> 32 33#include "namespace.h" 34#include <pthread.h> 35#include "un-namespace.h" 36#include "thr_private.h" 37 38/* maximum number of times a read lock may be obtained */ 39#define MAX_READ_LOCKS (INT_MAX - 1) 40 41__weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy); 42__weak_reference(_pthread_rwlock_init, pthread_rwlock_init); 43__weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock); | 27 */ 28 29#include <errno.h> 30#include <limits.h> 31#include <stdlib.h> 32 33#include "namespace.h" 34#include <pthread.h> 35#include "un-namespace.h" 36#include "thr_private.h" 37 38/* maximum number of times a read lock may be obtained */ 39#define MAX_READ_LOCKS (INT_MAX - 1) 40 41__weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy); 42__weak_reference(_pthread_rwlock_init, pthread_rwlock_init); 43__weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock); |
44__weak_reference(_pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock); |
|
44__weak_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock); 45__weak_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock); 46__weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock); 47__weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock); | 45__weak_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock); 46__weak_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock); 47__weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock); 48__weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock); |
49__weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock); |
|
48 49/* 50 * Prototypes 51 */ 52static int init_static(pthread_rwlock_t *rwlock); 53 54 55static int --- 76 unchanged lines hidden (view full) --- 132 *rwlock = prwlock; 133 } 134 } 135 } 136 137 return (ret); 138} 139 | 50 51/* 52 * Prototypes 53 */ 54static int init_static(pthread_rwlock_t *rwlock); 55 56 57static int --- 76 unchanged lines hidden (view full) --- 134 *rwlock = prwlock; 135 } 136 } 137 } 138 139 return (ret); 140} 141 |
140int 141_pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) | 142static int 143rwlock_rdlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime) |
142{ 143 pthread_rwlock_t prwlock; 144 int ret; 145 146 if (rwlock == NULL) 147 return (EINVAL); 148 149 prwlock = *rwlock; --- 7 unchanged lines hidden (view full) --- 157 } 158 159 /* grab the monitor lock */ 160 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0) 161 return (ret); 162 163 /* give writers priority over readers */ 164 while (prwlock->blocked_writers || prwlock->state < 0) { | 144{ 145 pthread_rwlock_t prwlock; 146 int ret; 147 148 if (rwlock == NULL) 149 return (EINVAL); 150 151 prwlock = *rwlock; --- 7 unchanged lines hidden (view full) --- 159 } 160 161 /* grab the monitor lock */ 162 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0) 163 return (ret); 164 165 /* give writers priority over readers */ 166 while (prwlock->blocked_writers || prwlock->state < 0) { |
165 ret = _thr_cond_wait(&prwlock->read_signal, &prwlock->lock); 166 | 167 if (abstime) 168 ret = _pthread_cond_timedwait(&prwlock->read_signal, 169 &prwlock->lock, abstime); 170 else 171 ret = _thr_cond_wait(&prwlock->read_signal, 172 &prwlock->lock); |
167 if (ret != 0) { 168 /* can't do a whole lot if this fails */ 169 _thr_mutex_unlock(&prwlock->lock); 170 return (ret); 171 } 172 } 173 174 /* check lock count */ --- 8 unchanged lines hidden (view full) --- 183 * lock. Decrementing 'state' is no good because we probably 184 * don't have the monitor lock. 185 */ 186 _thr_mutex_unlock(&prwlock->lock); 187 188 return (ret); 189} 190 | 173 if (ret != 0) { 174 /* can't do a whole lot if this fails */ 175 _thr_mutex_unlock(&prwlock->lock); 176 return (ret); 177 } 178 } 179 180 /* check lock count */ --- 8 unchanged lines hidden (view full) --- 189 * lock. Decrementing 'state' is no good because we probably 190 * don't have the monitor lock. 191 */ 192 _thr_mutex_unlock(&prwlock->lock); 193 194 return (ret); 195} 196 |
197int 198_pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) 199{ 200 return rwlock_rdlock_common (rwlock, NULL); 201} 202 |
|
191__strong_reference(_pthread_rwlock_rdlock, _thr_rwlock_rdlock); 192 193int | 203__strong_reference(_pthread_rwlock_rdlock, _thr_rwlock_rdlock); 204 205int |
206_pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, 207 const struct timespec *abstime) 208{ 209 return rwlock_rdlock_common(rwlock, abstime); 210} 211 212int |
|
194_pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) 195{ 196 pthread_rwlock_t prwlock; 197 int ret; 198 199 if (rwlock == NULL) 200 return (EINVAL); 201 --- 94 unchanged lines hidden (view full) --- 296 /* see the comment on this in pthread_rwlock_rdlock */ 297 _thr_mutex_unlock(&prwlock->lock); 298 299 return (ret); 300} 301 302__strong_reference(_pthread_rwlock_unlock, _thr_rwlock_unlock); 303 | 213_pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) 214{ 215 pthread_rwlock_t prwlock; 216 int ret; 217 218 if (rwlock == NULL) 219 return (EINVAL); 220 --- 94 unchanged lines hidden (view full) --- 315 /* see the comment on this in pthread_rwlock_rdlock */ 316 _thr_mutex_unlock(&prwlock->lock); 317 318 return (ret); 319} 320 321__strong_reference(_pthread_rwlock_unlock, _thr_rwlock_unlock); 322 |
304int 305_pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) | 323static int 324rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime) |
306{ 307 pthread_rwlock_t prwlock; 308 int ret; 309 310 if (rwlock == NULL) 311 return (EINVAL); 312 313 prwlock = *rwlock; --- 8 unchanged lines hidden (view full) --- 322 323 /* grab the monitor lock */ 324 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0) 325 return (ret); 326 327 while (prwlock->state != 0) { 328 ++prwlock->blocked_writers; 329 | 325{ 326 pthread_rwlock_t prwlock; 327 int ret; 328 329 if (rwlock == NULL) 330 return (EINVAL); 331 332 prwlock = *rwlock; --- 8 unchanged lines hidden (view full) --- 341 342 /* grab the monitor lock */ 343 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0) 344 return (ret); 345 346 while (prwlock->state != 0) { 347 ++prwlock->blocked_writers; 348 |
330 ret = _thr_cond_wait(&prwlock->write_signal, &prwlock->lock); 331 | 349 if (abstime != NULL) 350 ret = _pthread_cond_timedwait(&prwlock->write_signal, 351 &prwlock->lock, abstime); 352 else 353 ret = _thr_cond_wait(&prwlock->write_signal, 354 &prwlock->lock); |
332 if (ret != 0) { 333 --prwlock->blocked_writers; 334 _thr_mutex_unlock(&prwlock->lock); 335 return (ret); 336 } 337 338 --prwlock->blocked_writers; 339 } 340 341 /* indicate we are locked for writing */ 342 prwlock->state = -1; 343 344 /* see the comment on this in pthread_rwlock_rdlock */ 345 _thr_mutex_unlock(&prwlock->lock); 346 347 return (ret); 348} 349 | 355 if (ret != 0) { 356 --prwlock->blocked_writers; 357 _thr_mutex_unlock(&prwlock->lock); 358 return (ret); 359 } 360 361 --prwlock->blocked_writers; 362 } 363 364 /* indicate we are locked for writing */ 365 prwlock->state = -1; 366 367 /* see the comment on this in pthread_rwlock_rdlock */ 368 _thr_mutex_unlock(&prwlock->lock); 369 370 return (ret); 371} 372 |
373int 374_pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) 375{ 376 return rwlock_wrlock_common (rwlock, NULL); 377} |
|
350__strong_reference(_pthread_rwlock_wrlock, _thr_rwlock_wrlock); | 378__strong_reference(_pthread_rwlock_wrlock, _thr_rwlock_wrlock); |
379 380int 381_pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, 382 const struct timespec *abstime) 383{ 384 return rwlock_wrlock_common (rwlock, abstime); 385} |
|