From: Kurt Zeilenga Date: Sat, 29 Aug 1998 21:43:18 +0000 (+0000) Subject: Added Reader/Writer lock code from publicily available examples for X-Git-Tag: PHP3_TOOL_0_0~102 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=5d64c8f95ddfb5e92d171476ddbffc5ef8ace3c6;p=openldap Added Reader/Writer lock code from publicily available examples for Nichols, Butler, Farrell's book "Pthreads Programming" (O'Reilly). --- diff --git a/libraries/liblthread/rdwr.c b/libraries/liblthread/rdwr.c new file mode 100644 index 0000000000..8de2be407a --- /dev/null +++ b/libraries/liblthread/rdwr.c @@ -0,0 +1,76 @@ +/******************************************************** + * An example source module to accompany... + * + * "Using POSIX Threads: Programming with Pthreads" + * by Brad nichols, Dick Buttlar, Jackie Farrell + * O'Reilly & Associates, Inc. + * + ******************************************************** + * rdwr.c -- + * + * Library of functions implementing reader/writer locks + */ +#include +#include "rdwr.h" + +int pthread_rdwr_init_np(pthread_rdwr_t *rdwrp, pthread_rdwrattr_t *attrp) +{ + rdwrp->readers_reading = 0; + rdwrp->writer_writing = 0; + pthread_mutex_init(&(rdwrp->mutex), NULL); + pthread_cond_init(&(rdwrp->lock_free), NULL); + return 0; +} + +int pthread_rdwr_rlock_np(pthread_rdwr_t *rdwrp){ + pthread_mutex_lock(&(rdwrp->mutex)); + while(rdwrp->writer_writing) { + pthread_cond_wait(&(rdwrp->lock_free), &(rdwrp->mutex)); + } + rdwrp->readers_reading++; + pthread_mutex_unlock(&(rdwrp->mutex)); + return 0; +} + +int pthread_rdwr_runlock_np(pthread_rdwr_t *rdwrp) +{ + pthread_mutex_lock(&(rdwrp->mutex)); + if (rdwrp->readers_reading == 0) { + pthread_mutex_unlock(&(rdwrp->mutex)); + return -1; + } + else { + rdwrp->readers_reading--; + if (rdwrp->readers_reading == 0) { + pthread_cond_signal(&(rdwrp->lock_free)); + } + pthread_mutex_unlock(&(rdwrp->mutex)); + return 0; + } +} + +int pthread_rdwr_wlock_np(pthread_rdwr_t *rdwrp) +{ + pthread_mutex_lock(&(rdwrp->mutex)); + while(rdwrp->writer_writing || rdwrp->readers_reading) { + pthread_cond_wait(&(rdwrp->lock_free), &(rdwrp->mutex)); + } + rdwrp->writer_writing++; + pthread_mutex_unlock(&(rdwrp->mutex)); + return 0; +} + +int pthread_rdwr_wunlock_np(pthread_rdwr_t *rdwrp) +{ + pthread_mutex_lock(&(rdwrp->mutex)); + if (rdwrp->writer_writing == 0) { + pthread_mutex_unlock(&(rdwrp->mutex)); + return -1; + } + else { + rdwrp->writer_writing = 0; + pthread_cond_broadcast(&(rdwrp->lock_free)); + pthread_mutex_unlock(&(rdwrp->mutex)); + return 0; + } +} diff --git a/libraries/liblthread/rdwr.h b/libraries/liblthread/rdwr.h new file mode 100644 index 0000000000..f4cda4d563 --- /dev/null +++ b/libraries/liblthread/rdwr.h @@ -0,0 +1,28 @@ +/******************************************************** + * An example source module to accompany... + * + * "Using POSIX Threads: Programming with Pthreads" + * by Brad nichols, Dick Buttlar, Jackie Farrell + * O'Reilly & Associates, Inc. + * + ******************************************************** + * rdwr.h -- + * + * Include file for reader/writer locks + */ +typedef struct rdwr_var { + int readers_reading; + int writer_writing; + pthread_mutex_t mutex; + pthread_cond_t lock_free; +} pthread_rdwr_t; + +typedef void * pthread_rdwrattr_t; + +#define pthread_rdwrattr_default NULL; + +int pthread_rdwr_init_np(pthread_rdwr_t *rdwrp, pthread_rdwrattr_t *attrp); +int pthread_rdwr_rlock_np(pthread_rdwr_t *rdwrp); +int pthread_rdwr_runlock_np(pthread_rdwr_t *rdwrp); +int pthread_rdwr_wlock_np(pthread_rdwr_t *rdwrp); +int pthread_rdwr_wunlock_np(pthread_rdwr_t *rdwrp);