3 ** This is an improved implementation of Reader/Writer locks does
4 ** not protect writers from starvation. That is, if a writer is
5 ** currently waiting on a reader, any new reader will get
6 ** the lock before the writer.
8 ** Does not support cancellation nor does any status checking.
10 /* Adapted from publically available examples for:
11 * "Programming with Posix Threads"
12 * by David R Butenhof, Addison-Wesley
13 * http://cseng.aw.com/bookpage.taf?ISBN=0-201-63392-2
18 #include <ac/stdlib.h>
21 #include <ac/string.h>
25 #include "ldap_pvt_thread.h"
28 * implementations that provide their own compatible
29 * reader/writer locks define LDAP_THREAD_HAVE_RDWR
30 * in ldap_pvt_thread.h
32 #ifndef LDAP_THREAD_HAVE_RDWR
34 struct ldap_int_thread_rdwr_s {
35 ldap_pvt_thread_mutex_t ltrw_mutex;
36 ldap_pvt_thread_cond_t ltrw_read; /* wait for read */
37 ldap_pvt_thread_cond_t ltrw_write; /* wait for write */
39 #define LDAP_PVT_THREAD_RDWR_VALID 0x0bad
47 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rwlock )
49 struct ldap_int_thread_rdwr_s *rw;
51 assert( rwlock != NULL );
53 rw = (struct ldap_int_thread_rdwr_s *) LDAP_CALLOC( 1,
54 sizeof( struct ldap_int_thread_rdwr_s ) );
56 /* we should check return results */
57 ldap_pvt_thread_mutex_init( &rw->ltrw_mutex );
58 ldap_pvt_thread_cond_init( &rw->ltrw_read );
59 ldap_pvt_thread_cond_init( &rw->ltrw_write );
61 rw->ltrw_valid = LDAP_PVT_THREAD_RDWR_VALID;
68 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rwlock )
70 struct ldap_int_thread_rdwr_s *rw;
72 assert( rwlock != NULL );
76 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
78 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
79 return LDAP_PVT_THREAD_EINVAL;
81 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
83 assert( rw->ltrw_w_active >= 0 );
84 assert( rw->ltrw_w_wait >= 0 );
85 assert( rw->ltrw_r_active >= 0 );
86 assert( rw->ltrw_r_wait >= 0 );
89 if( rw->ltrw_r_active > 0 || rw->ltrw_w_active > 0) {
90 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
91 return LDAP_PVT_THREAD_EBUSY;
94 /* waiting threads? */
95 if( rw->ltrw_r_wait > 0 || rw->ltrw_w_wait > 0) {
96 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
97 return LDAP_PVT_THREAD_EBUSY;
102 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
104 ldap_pvt_thread_mutex_destroy( &rw->ltrw_mutex );
105 ldap_pvt_thread_cond_destroy( &rw->ltrw_read );
106 ldap_pvt_thread_cond_destroy( &rw->ltrw_write );
113 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rwlock )
115 struct ldap_int_thread_rdwr_s *rw;
117 assert( rwlock != NULL );
120 assert( rw != NULL );
121 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
123 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
124 return LDAP_PVT_THREAD_EINVAL;
126 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
128 assert( rw->ltrw_w_active >= 0 );
129 assert( rw->ltrw_w_wait >= 0 );
130 assert( rw->ltrw_r_active >= 0 );
131 assert( rw->ltrw_r_wait >= 0 );
133 if( rw->ltrw_w_active > 0 ) {
134 /* writer is active */
139 ldap_pvt_thread_cond_wait(
140 &rw->ltrw_read, &rw->ltrw_mutex );
141 } while( rw->ltrw_w_active > 0 );
144 assert( rw->ltrw_r_wait >= 0 );
149 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
154 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rwlock )
156 struct ldap_int_thread_rdwr_s *rw;
158 assert( rwlock != NULL );
161 assert( rw != NULL );
162 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
164 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
165 return LDAP_PVT_THREAD_EINVAL;
167 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
169 assert( rw->ltrw_w_active >= 0 );
170 assert( rw->ltrw_w_wait >= 0 );
171 assert( rw->ltrw_r_active >= 0 );
172 assert( rw->ltrw_r_wait >= 0 );
174 if( rw->ltrw_w_active > 0) {
175 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
176 return LDAP_PVT_THREAD_EBUSY;
181 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
186 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rwlock )
188 struct ldap_int_thread_rdwr_s *rw;
190 assert( rwlock != NULL );
193 assert( rw != NULL );
194 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
196 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
197 return LDAP_PVT_THREAD_EINVAL;
199 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
203 assert( rw->ltrw_w_active >= 0 );
204 assert( rw->ltrw_w_wait >= 0 );
205 assert( rw->ltrw_r_active >= 0 );
206 assert( rw->ltrw_r_wait >= 0 );
208 if (rw->ltrw_r_active == 0 && rw->ltrw_w_wait > 0 ) {
209 ldap_pvt_thread_cond_signal( &rw->ltrw_write );
212 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
217 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rwlock )
219 struct ldap_int_thread_rdwr_s *rw;
221 assert( rwlock != NULL );
224 assert( rw != NULL );
225 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
227 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
228 return LDAP_PVT_THREAD_EINVAL;
230 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
232 assert( rw->ltrw_w_active >= 0 );
233 assert( rw->ltrw_w_wait >= 0 );
234 assert( rw->ltrw_r_active >= 0 );
235 assert( rw->ltrw_r_wait >= 0 );
237 if ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 ) {
241 ldap_pvt_thread_cond_wait(
242 &rw->ltrw_write, &rw->ltrw_mutex );
243 } while ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 );
246 assert( rw->ltrw_w_wait >= 0 );
251 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
256 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rwlock )
258 struct ldap_int_thread_rdwr_s *rw;
260 assert( rwlock != NULL );
263 assert( rw != NULL );
264 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
266 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
267 return LDAP_PVT_THREAD_EINVAL;
269 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
271 assert( rw->ltrw_w_active >= 0 );
272 assert( rw->ltrw_w_wait >= 0 );
273 assert( rw->ltrw_r_active >= 0 );
274 assert( rw->ltrw_r_wait >= 0 );
276 if ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 ) {
277 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
278 return LDAP_PVT_THREAD_EBUSY;
283 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
288 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rwlock )
290 struct ldap_int_thread_rdwr_s *rw;
292 assert( rwlock != NULL );
295 assert( rw != NULL );
296 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
298 if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
299 return LDAP_PVT_THREAD_EINVAL;
301 ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
305 assert( rw->ltrw_w_active >= 0 );
306 assert( rw->ltrw_w_wait >= 0 );
307 assert( rw->ltrw_r_active >= 0 );
308 assert( rw->ltrw_r_wait >= 0 );
310 if (rw->ltrw_r_wait > 0) {
311 ldap_pvt_thread_cond_broadcast( &rw->ltrw_read );
313 } else if (rw->ltrw_w_wait > 0) {
314 ldap_pvt_thread_cond_signal( &rw->ltrw_write );
317 ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
322 #ifdef LDAP_RDWR_DEBUG
325 * return 0 if false, suitable for assert(ldap_pvt_thread_rdwr_Xchk(rdwr))
327 * Currently they don't check if the calling thread is the one
328 * that has the lock, just that there is a reader or writer.
330 * Basically sufficent for testing that places that should have
334 int ldap_pvt_thread_rdwr_readers(ldap_pvt_thread_rdwr_t *rwlock)
336 struct ldap_int_thread_rdwr_s *rw;
338 assert( rwlock != NULL );
341 assert( rw != NULL );
342 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
343 assert( rw->ltrw_w_active >= 0 );
344 assert( rw->ltrw_w_wait >= 0 );
345 assert( rw->ltrw_r_active >= 0 );
346 assert( rw->ltrw_r_wait >= 0 );
348 return( rw->ltrw_r_active );
351 int ldap_pvt_thread_rdwr_writers(ldap_pvt_thread_rdwr_t *rwlock)
353 struct ldap_int_thread_rdwr_s *rw;
355 assert( rwlock != NULL );
358 assert( rw != NULL );
359 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
360 assert( rw->ltrw_w_active >= 0 );
361 assert( rw->ltrw_w_wait >= 0 );
362 assert( rw->ltrw_r_active >= 0 );
363 assert( rw->ltrw_r_wait >= 0 );
365 return( rw->ltrw_w_active );
368 int ldap_pvt_thread_rdwr_active(ldap_pvt_thread_rdwr_t *rwlock)
370 struct ldap_int_thread_rdwr_s *rw;
372 assert( rwlock != NULL );
375 assert( rw != NULL );
376 assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );
377 assert( rw->ltrw_w_active >= 0 );
378 assert( rw->ltrw_w_wait >= 0 );
379 assert( rw->ltrw_r_active >= 0 );
380 assert( rw->ltrw_r_wait >= 0 );
382 return(ldap_pvt_thread_rdwr_readers(rw) +
383 ldap_pvt_thread_rdwr_writers(rw));
386 #endif /* LDAP_DEBUG */
388 #endif /* LDAP_THREAD_HAVE_RDWR */