summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2017-08-06 15:14:55 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2017-08-06 15:14:55 (GMT)
commit75a077fa0ee4b0094b6d43619cd0a068a82ebfd7 (patch) (side-by-side diff)
tree4b00c3d02abaf982929da25b056bb78477fb207a
parente47faca1b8079c5aa13915e3a98d35c47fe0a78a (diff)
downloadvmod-dict-75a077fa0ee4b0094b6d43619cd0a068a82ebfd7.tar.gz
vmod-dict-75a077fa0ee4b0094b6d43619cd0a068a82ebfd7.tar.bz2
Switch to POSIX pthread_rwlock functions instead of the custom locker
Diffstat (more/less context) (ignore whitespace changes)
-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
@@ -76,71 +76,3 @@ 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;
@@ -442,3 +374,3 @@ dict_event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
case VCL_EVENT_LOAD:
- locker_init(&rwlock);
+ pthread_rwlock_init(&rwlock, NULL);
break;
@@ -446,3 +378,3 @@ dict_event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
case VCL_EVENT_DISCARD:
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
while (ent_head)
@@ -452,3 +384,3 @@ dict_event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
hash_size = 0;
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
break;
@@ -466,3 +398,3 @@ vmod_ci(VRT_CTX, VCL_BOOL ci)
int *cp;
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
cp = ci ? ci_coll : cs_coll;
@@ -473,3 +405,3 @@ vmod_ci(VRT_CTX, VCL_BOOL ci)
}
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
@@ -485,6 +417,6 @@ 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);
}
@@ -496,3 +428,3 @@ vmod_clear(VRT_CTX)
- locker_wlock(&rwlock);
+ pthread_rwlock_wrlock(&rwlock);
for (i = 0; i < hash_size; i++)
@@ -501,3 +433,3 @@ vmod_clear(VRT_CTX)
entry_remove(ent_head);
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
@@ -532,3 +464,3 @@ vmod_lookup(VRT_CTX, VCL_STRING key)
if (key) {
- locker_rlock(&rwlock);
+ pthread_rwlock_rdlock(&rwlock);
char const *p = lookup_unlocked(key);
@@ -538,3 +470,3 @@ vmod_lookup(VRT_CTX, VCL_STRING key)
}
- locker_runlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}

Return to:

Send suggestions and report system problems to the System administrator.