2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* This work was initially developed by Kurt D. Zeilenga for inclusion
16 * in OpenLDAP Software. Additional significant contributors include:
21 * This is an improved implementation of Reader/Writer locks does
22 * not protect writers from starvation. That is, if a writer is
23 * currently waiting on a reader, any new reader will get
24 * the lock before the writer.
26 * Does not support cancellation nor does any status checking.
28 /* Adapted from publically available examples for:
29 * "Programming with Posix Threads"
30 * by David R Butenhof, Addison-Wesley
31 * http://cseng.aw.com/bookpage.taf?ISBN=0-201-63392-2
36 #include <ac/stdlib.h>
39 #include <ac/string.h>
43 #include "ldap_pvt_thread.h" /* Get the thread interface */
44 #define LDAP_THREAD_RDWR_IMPLEMENTATION
45 #include "ldap_thr_debug.h" /* May rename the symbols defined below */
48 * implementations that provide their own compatible
49 * reader/writer locks define LDAP_THREAD_HAVE_RDWR
50 * in ldap_pvt_thread.h
52 #ifndef LDAP_THREAD_HAVE_RDWR
54 struct ldap_int_thread_rdwr_s {
55 ldap_pvt_thread_mutex_t ltrw_mutex;
56 ldap_pvt_thread_cond_t ltrw_read; /* wait for read */
57 ldap_pvt_thread_cond_t ltrw_write; /* wait for write */
59 #define LDAP_PVT_THREAD_RDWR_VALID 0x0bad
64 #ifdef LDAP_RDWR_DEBUG
65 /* keep track of who has these locks */
66 #define MAX_READERS 32
67 int ltrw_more_readers; /* Set if ltrw_readers[] is incomplete */
68 ldap_pvt_thread_t ltrw_readers[MAX_READERS];
69 ldap_pvt_thread_t ltrw_writer;
74 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rwlock )
76 struct ldap_int_thread_rdwr_s *rw;
78 assert( rwlock != NULL );
80 rw = (struct ldap_int_thread_rdwr_s *) LDAP_CALLOC( 1,
81 sizeof( struct ldap_int_thread_rdwr_s ) );
83 return LDAP_NO_MEMORY;
85 /* we should check return results */
86 ldap_pvt_thread_mutex_init( &rw->ltrw_mutex );
87 ldap_pvt_thread_cond_init( &rw->ltrw_read );
88 ldap_pvt_thread_cond_init( &rw->ltrw_write );
90 rw->ltrw_valid = LDAP_PVT_THREAD_RDWR_VALID;
97 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rwlock )
99 struct ldap_int_thread_rdwr_s *rw;
101 assert( rwlock != NULL );
104 assert( rw != NULL );
105 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
107 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
108 return LDAP_PVT_THREAD_EINVAL;
110 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
112 assert( rw->ltrw_w_active >= 0 );
113 assert( rw->ltrw_w_wait >= 0 );
114 assert( rw->ltrw_r_active >= 0 );
115 assert( rw->ltrw_r_wait >= 0 );
117 /* active threads? */
118 if( rw->ltrw_r_active > 0 || rw->ltrw_w_active > 0) {
119 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
120 return LDAP_PVT_THREAD_EBUSY;
123 /* waiting threads? */
124 if( rw->ltrw_r_wait > 0 || rw->ltrw_w_wait > 0) {
125 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
126 return LDAP_PVT_THREAD_EBUSY;
131 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
133 ldap_pvt_thread_mutex_destroy( &rw->ltrw_mutex );
134 ldap_pvt_thread_cond_destroy( &rw->ltrw_read );
135 ldap_pvt_thread_cond_destroy( &rw->ltrw_write );
142 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rwlock )
144 struct ldap_int_thread_rdwr_s *rw;
146 assert( rwlock != NULL );
149 assert( rw != NULL );
150 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
152 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
153 return LDAP_PVT_THREAD_EINVAL;
155 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
157 assert( rw->ltrw_w_active >= 0 );
158 assert( rw->ltrw_w_wait >= 0 );
159 assert( rw->ltrw_r_active >= 0 );
160 assert( rw->ltrw_r_wait >= 0 );
162 if( rw->ltrw_w_active > 0 ) {
163 /* writer is active */
168 ldap_pvt_thread_cond_wait(
169 &rw->ltrw_read, &rw->ltrw_mutex );
170 } while( rw->ltrw_w_active > 0 );
173 assert( rw->ltrw_r_wait >= 0 );
176 #ifdef LDAP_RDWR_DEBUG
177 if( rw->ltrw_r_active < MAX_READERS )
178 rw->ltrw_readers[rw->ltrw_r_active] = ldap_pvt_thread_self();
180 rw->ltrw_more_readers = 1;
185 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
190 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rwlock )
192 struct ldap_int_thread_rdwr_s *rw;
194 assert( rwlock != NULL );
197 assert( rw != NULL );
198 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
200 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
201 return LDAP_PVT_THREAD_EINVAL;
203 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
205 assert( rw->ltrw_w_active >= 0 );
206 assert( rw->ltrw_w_wait >= 0 );
207 assert( rw->ltrw_r_active >= 0 );
208 assert( rw->ltrw_r_wait >= 0 );
210 if( rw->ltrw_w_active > 0) {
211 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
212 return LDAP_PVT_THREAD_EBUSY;
215 #ifdef LDAP_RDWR_DEBUG
216 if( rw->ltrw_r_active < MAX_READERS )
217 rw->ltrw_readers[rw->ltrw_r_active] = ldap_pvt_thread_self();
219 rw->ltrw_more_readers = 1;
223 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
228 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rwlock )
230 struct ldap_int_thread_rdwr_s *rw;
232 assert( rwlock != NULL );
235 assert( rw != NULL );
236 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
238 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
239 return LDAP_PVT_THREAD_EINVAL;
241 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
244 #ifdef LDAP_RDWR_DEBUG
245 /* Remove us from the list of readers */
247 ldap_pvt_thread_t self = ldap_pvt_thread_self();
249 for( i = j = rw->ltrw_r_active; i >= 0; i--) {
250 if (rw->ltrw_readers[i] == self) {
251 rw->ltrw_readers[i] = rw->ltrw_readers[j];
252 rw->ltrw_readers[j] = 0;
256 if( !rw->ltrw_more_readers )
259 rw->ltrw_more_readers = 0;
263 assert( rw->ltrw_w_active >= 0 );
264 assert( rw->ltrw_w_wait >= 0 );
265 assert( rw->ltrw_r_active >= 0 );
266 assert( rw->ltrw_r_wait >= 0 );
268 if (rw->ltrw_r_active == 0 && rw->ltrw_w_wait > 0 ) {
269 ldap_pvt_thread_cond_signal( &rw->ltrw_write );
272 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
277 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rwlock )
279 struct ldap_int_thread_rdwr_s *rw;
281 assert( rwlock != NULL );
284 assert( rw != NULL );
285 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
287 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
288 return LDAP_PVT_THREAD_EINVAL;
290 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
292 assert( rw->ltrw_w_active >= 0 );
293 assert( rw->ltrw_w_wait >= 0 );
294 assert( rw->ltrw_r_active >= 0 );
295 assert( rw->ltrw_r_wait >= 0 );
297 if ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 ) {
301 ldap_pvt_thread_cond_wait(
302 &rw->ltrw_write, &rw->ltrw_mutex );
303 } while ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 );
306 assert( rw->ltrw_w_wait >= 0 );
309 #ifdef LDAP_RDWR_DEBUG
310 rw->ltrw_writer = ldap_pvt_thread_self();
314 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
319 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rwlock )
321 struct ldap_int_thread_rdwr_s *rw;
323 assert( rwlock != NULL );
326 assert( rw != NULL );
327 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
329 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
330 return LDAP_PVT_THREAD_EINVAL;
332 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
334 assert( rw->ltrw_w_active >= 0 );
335 assert( rw->ltrw_w_wait >= 0 );
336 assert( rw->ltrw_r_active >= 0 );
337 assert( rw->ltrw_r_wait >= 0 );
339 if ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 ) {
340 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
341 return LDAP_PVT_THREAD_EBUSY;
344 #ifdef LDAP_RDWR_DEBUG
345 rw->ltrw_writer = ldap_pvt_thread_self();
349 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
354 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rwlock )
356 struct ldap_int_thread_rdwr_s *rw;
358 assert( rwlock != NULL );
361 assert( rw != NULL );
362 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
364 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
365 return LDAP_PVT_THREAD_EINVAL;
367 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
371 assert( rw->ltrw_w_active >= 0 );
372 assert( rw->ltrw_w_wait >= 0 );
373 assert( rw->ltrw_r_active >= 0 );
374 assert( rw->ltrw_r_wait >= 0 );
376 if (rw->ltrw_r_wait > 0) {
377 ldap_pvt_thread_cond_broadcast( &rw->ltrw_read );
379 } else if (rw->ltrw_w_wait > 0) {
380 ldap_pvt_thread_cond_signal( &rw->ltrw_write );
383 #ifdef LDAP_RDWR_DEBUG
384 assert( rw->ltrw_writer == ldap_pvt_thread_self() );
387 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
392 #ifdef LDAP_RDWR_DEBUG
395 * return 0 if false, suitable for assert(ldap_pvt_thread_rdwr_Xchk(rdwr))
397 * Currently they don't check if the calling thread is the one
398 * that has the lock, just that there is a reader or writer.
400 * Basically sufficent for testing that places that should have
404 int ldap_pvt_thread_rdwr_readers(ldap_pvt_thread_rdwr_t *rwlock)
406 struct ldap_int_thread_rdwr_s *rw;
408 assert( rwlock != NULL );
411 assert( rw != NULL );
412 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
413 assert( rw->ltrw_w_active >= 0 );
414 assert( rw->ltrw_w_wait >= 0 );
415 assert( rw->ltrw_r_active >= 0 );
416 assert( rw->ltrw_r_wait >= 0 );
418 return( rw->ltrw_r_active );
421 int ldap_pvt_thread_rdwr_writers(ldap_pvt_thread_rdwr_t *rwlock)
423 struct ldap_int_thread_rdwr_s *rw;
425 assert( rwlock != NULL );
428 assert( rw != NULL );
429 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
430 assert( rw->ltrw_w_active >= 0 );
431 assert( rw->ltrw_w_wait >= 0 );
432 assert( rw->ltrw_r_active >= 0 );
433 assert( rw->ltrw_r_wait >= 0 );
435 return( rw->ltrw_w_active );
438 int ldap_pvt_thread_rdwr_active(ldap_pvt_thread_rdwr_t *rwlock)
440 struct ldap_int_thread_rdwr_s *rw;
442 assert( rwlock != NULL );
445 assert( rw != NULL );
446 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
447 assert( rw->ltrw_w_active >= 0 );
448 assert( rw->ltrw_w_wait >= 0 );
449 assert( rw->ltrw_r_active >= 0 );
450 assert( rw->ltrw_r_wait >= 0 );
452 return(ldap_pvt_thread_rdwr_readers(rwlock) +
453 ldap_pvt_thread_rdwr_writers(rwlock));
456 #endif /* LDAP_RDWR_DEBUG */
458 #endif /* LDAP_THREAD_HAVE_RDWR */