/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2003 The OpenLDAP Foundation.
+ * Copyright 1998-2007 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
+/* This work was initially developed by Kurt D. Zeilenga for inclusion
+ * in OpenLDAP Software. Additional significant contributors include:
+ * Stuart Lynne
+ */
/*
-** This is an improved implementation of Reader/Writer locks does
-** not protect writers from starvation. That is, if a writer is
-** currently waiting on a reader, any new reader will get
-** the lock before the writer.
-**
-** Does not support cancellation nor does any status checking.
-*/
+ * This is an improved implementation of Reader/Writer locks does
+ * not protect writers from starvation. That is, if a writer is
+ * currently waiting on a reader, any new reader will get
+ * the lock before the writer.
+ *
+ * Does not support cancellation nor does any status checking.
+ */
/* Adapted from publically available examples for:
* "Programming with Posix Threads"
* by David R Butenhof, Addison-Wesley
#include <ac/time.h>
#include "ldap-int.h"
-#include "ldap_pvt_thread.h"
+#include "ldap_pvt_thread.h" /* Get the thread interface */
+#define LDAP_THREAD_RDWR_IMPLEMENTATION
+#include "ldap_thr_debug.h" /* May rename the symbols defined below */
/*
* implementations that provide their own compatible
int ltrw_w_active;
int ltrw_r_wait;
int ltrw_w_wait;
+#ifdef LDAP_RDWR_DEBUG
+ /* keep track of who has these locks */
+#define MAX_READERS 32
+ int ltrw_more_readers; /* Set if ltrw_readers[] is incomplete */
+ ldap_pvt_thread_t ltrw_readers[MAX_READERS];
+ ldap_pvt_thread_t ltrw_writer;
+#endif
};
int
rw = (struct ldap_int_thread_rdwr_s *) LDAP_CALLOC( 1,
sizeof( struct ldap_int_thread_rdwr_s ) );
+ if ( !rw )
+ return LDAP_NO_MEMORY;
/* we should check return results */
ldap_pvt_thread_mutex_init( &rw->ltrw_mutex );
assert( rw->ltrw_r_wait >= 0 );
}
+#ifdef LDAP_RDWR_DEBUG
+ if( rw->ltrw_r_active < MAX_READERS )
+ rw->ltrw_readers[rw->ltrw_r_active] = ldap_pvt_thread_self();
+ else
+ rw->ltrw_more_readers = 1;
+#endif
rw->ltrw_r_active++;
+
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
return 0;
return LDAP_PVT_THREAD_EBUSY;
}
+#ifdef LDAP_RDWR_DEBUG
+ if( rw->ltrw_r_active < MAX_READERS )
+ rw->ltrw_readers[rw->ltrw_r_active] = ldap_pvt_thread_self();
+ else
+ rw->ltrw_more_readers = 1;
+#endif
rw->ltrw_r_active++;
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );
rw->ltrw_r_active--;
+#ifdef LDAP_RDWR_DEBUG
+ /* Remove us from the list of readers */
+ {
+ ldap_pvt_thread_t self = ldap_pvt_thread_self();
+ int i, j;
+ for( i = j = rw->ltrw_r_active; i >= 0; i--) {
+ if (rw->ltrw_readers[i] == self) {
+ rw->ltrw_readers[i] = rw->ltrw_readers[j];
+ rw->ltrw_readers[j] = 0;
+ break;
+ }
+ }
+ if( !rw->ltrw_more_readers )
+ assert( i >= 0 );
+ else if( j == 0 )
+ rw->ltrw_more_readers = 0;
+ }
+#endif
assert( rw->ltrw_w_active >= 0 );
assert( rw->ltrw_w_wait >= 0 );
assert( rw->ltrw_w_wait >= 0 );
}
+#ifdef LDAP_RDWR_DEBUG
+ rw->ltrw_writer = ldap_pvt_thread_self();
+#endif
rw->ltrw_w_active++;
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
return LDAP_PVT_THREAD_EBUSY;
}
+#ifdef LDAP_RDWR_DEBUG
+ rw->ltrw_writer = ldap_pvt_thread_self();
+#endif
rw->ltrw_w_active++;
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
ldap_pvt_thread_cond_signal( &rw->ltrw_write );
}
+#ifdef LDAP_RDWR_DEBUG
+ assert( rw->ltrw_writer == ldap_pvt_thread_self() );
+ rw->ltrw_writer = 0;
+#endif
ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
return 0;
assert( rw->ltrw_r_active >= 0 );
assert( rw->ltrw_r_wait >= 0 );
- return(ldap_pvt_thread_rdwr_readers(rw) +
- ldap_pvt_thread_rdwr_writers(rw));
+ return(ldap_pvt_thread_rdwr_readers(rwlock) +
+ ldap_pvt_thread_rdwr_writers(rwlock));
}
-#endif /* LDAP_DEBUG */
+#endif /* LDAP_RDWR_DEBUG */
#endif /* LDAP_THREAD_HAVE_RDWR */