aboutsummaryrefslogtreecommitdiff
path: root/src/vmod_dict.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2017-08-06 18:14:55 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2017-08-06 18:14:55 +0300
commit75a077fa0ee4b0094b6d43619cd0a068a82ebfd7 (patch)
tree4b00c3d02abaf982929da25b056bb78477fb207a /src/vmod_dict.c
parente47faca1b8079c5aa13915e3a98d35c47fe0a78a (diff)
downloadvmod-dict-75a077fa0ee4b0094b6d43619cd0a068a82ebfd7.tar.gz
vmod-dict-75a077fa0ee4b0094b6d43619cd0a068a82ebfd7.tar.bz2
Switch to POSIX pthread_rwlock functions instead of the custom locker
Diffstat (limited to 'src/vmod_dict.c')
-rw-r--r--src/vmod_dict.c92
1 files changed, 12 insertions, 80 deletions
diff --git a/src/vmod_dict.c b/src/vmod_dict.c
index 5875abd..04ee52c 100644
--- a/src/vmod_dict.c
+++ b/src/vmod_dict.c
@@ -75,73 +75,5 @@ static struct entry **hash_table;
static size_t max_hash_size = 8192;
static ssize_t max_coll = -1; /* Max. number of collisions allowed */
-
-typedef struct {
- size_t readers;
- size_t writer;
- pthread_mutex_t mutex;
- pthread_cond_t lock_free;
-} locker_t;
-
-static locker_t rwlock;
-
-static void
-locker_init(locker_t *rdwr)
-{
- rdwr->readers = 0;
- rdwr->writer = 0;
- pthread_mutex_init(&rdwr->mutex, NULL);
- pthread_cond_init(&rdwr->lock_free, NULL);
-}
-
-static void
-locker_rlock(locker_t *rdwr)
-{
- pthread_mutex_lock(&rdwr->mutex);
- while (rdwr->writer)
- pthread_cond_wait(&rdwr->lock_free, &rdwr->mutex);
- rdwr->readers++;
- pthread_mutex_unlock(&rdwr->mutex);
-}
-
-static void
-locker_wlock(locker_t *rdwr)
-{
- pthread_mutex_lock(&rdwr->mutex);
- while (rdwr->writer || rdwr->readers)
- pthread_cond_wait(&rdwr->lock_free, &rdwr->mutex);
- rdwr->writer++;
- pthread_mutex_unlock(&rdwr->mutex);
-}
-
-static int
-locker_runlock(locker_t *rdwr)
-{
- pthread_mutex_lock(&rdwr->mutex);
- if (rdwr->readers == 0) {
- pthread_mutex_unlock(&rdwr->mutex);
- return -1;
- } else {
- rdwr->readers--;
- if (rdwr->readers == 0)
- pthread_cond_signal(&rdwr->lock_free);
- pthread_mutex_unlock(&rdwr->mutex);
- return 0;
- }
-}
-
-static int
-locker_wunlock(locker_t *rdwr)
-{
- pthread_mutex_lock(&rdwr->mutex);
- if (rdwr->writer == 0) {
- pthread_mutex_unlock(&rdwr->mutex);
- return -1;
- } else {
- rdwr->writer = 0;
- pthread_cond_signal(&rdwr->lock_free);
- pthread_mutex_unlock(&rdwr->mutex);
- return 0;
- }
-}
+static pthread_rwlock_t rwlock;
static int cs_coll[] = {
@@ -441,9 +373,9 @@ dict_event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
switch (e) {
case VCL_EVENT_LOAD:
- locker_init(&rwlock);
+ pthread_rwlock_init(&rwlock, NULL);
break;
case VCL_EVENT_DISCARD:
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
while (ent_head)
entry_remove(ent_head);
@@ -451,5 +383,5 @@ dict_event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
hash_table = NULL;
hash_size = 0;
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
break;
@@ -465,5 +397,5 @@ vmod_ci(VRT_CTX, VCL_BOOL ci)
{
int *cp;
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
cp = ci ? ci_coll : cs_coll;
if (cp != collation) {
@@ -472,5 +404,5 @@ vmod_ci(VRT_CTX, VCL_BOOL ci)
fill_table();
}
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
@@ -484,8 +416,8 @@ VCL_VOID
vmod_load(VRT_CTX, VCL_STRING file)
{
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
load_entries(file);
fill_table();
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
@@ -495,10 +427,10 @@ vmod_clear(VRT_CTX)
size_t i;
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
for (i = 0; i < hash_size; i++)
hash_table[i] = NULL;
while (ent_head)
entry_remove(ent_head);
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
@@ -531,5 +463,5 @@ vmod_lookup(VRT_CTX, VCL_STRING key)
char *s = NULL;
if (key) {
- locker_rlock(&rwlock);
+ pthread_rwlock_rdlock(&rwlock);
char const *p = lookup_unlocked(key);
if (p) {
@@ -537,5 +469,5 @@ vmod_lookup(VRT_CTX, VCL_STRING key)
AN(s);
}
- locker_runlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
return s;

Return to:

Send suggestions and report system problems to the System administrator.