+/* $OpenLDAP$ */
/*
** This is an improved implementation of Reader/Writer locks does
** not protect writers from starvation. That is, if a writer is
**
** Does not support cancellation nor does any status checking.
*/
-
-/********************************************************
- * Adapted from:
+/* Adapted from publically available examples for:
* "Programming with Posix Threads"
- * by David R Butenhof
- * Addison-Wesley
- ********************************************************
+ * by David R Butenhof, Addison-Wesley
+ * http://cseng.aw.com/bookpage.taf?ISBN=0-201-63392-2
*/
#include "portable.h"
-#include <stdlib.h>
+#include <ac/stdlib.h>
#include <ac/errno.h>
#include <ac/string.h>
#include "ldap_pvt_thread.h"
+/*
+ * implementations that provide their own compatible
+ * reader/writer locks define LDAP_THREAD_HAVE_RDWR
+ * in ldap_pvt_thread.h
+ */
+#ifndef LDAP_THREAD_HAVE_RDWR
+
int
ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
{
ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
/* active threads? */
- if( rw->ltrw_r_active > 0 || rw->ltrw_w_active > 1) {
+ if( rw->ltrw_r_active > 0 || rw->ltrw_w_active > 0) {
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
return LDAP_PVT_THREAD_EBUSY;
}
ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
- if( rw->ltrw_w_active > 1 ) {
+ if( rw->ltrw_w_active > 0 ) {
/* writer is active */
rw->ltrw_r_wait++;
do {
ldap_pvt_thread_cond_wait(
&rw->ltrw_read, &rw->ltrw_mutex );
- } while( rw->ltrw_w_active > 1 );
+ } while( rw->ltrw_w_active > 0 );
rw->ltrw_r_wait--;
}
ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
- if( rw->ltrw_w_active > 1) {
+ if( rw->ltrw_w_active > 0) {
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
return LDAP_PVT_THREAD_EBUSY;
}
return 0;
}
-#ifdef LDAP_DEBUG
+#ifdef LDAP_RDWR_DEBUG
/* just for testing,
* return 0 if false, suitable for assert(ldap_pvt_thread_rdwr_Xchk(rdwr))
}
#endif /* LDAP_DEBUG */
+
+#endif /* LDAP_THREAD_HAVE_RDWR */