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
@@ -74,75 +74,7 @@ static size_t hash_size;
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[] = {
0, 1, 2, 3, 4, 5, 6, 7,
@@ -440,17 +372,17 @@ 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);
free(hash_table);
hash_table = NULL;
hash_size = 0;
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
break;
case VCL_EVENT_WARM:
@@ -464,14 +396,14 @@ VCL_VOID
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) {
collation = cp;
rehash();
fill_table();
}
- locker_wunlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
VCL_VOID
@@ -483,10 +415,10 @@ vmod_collisions(VRT_CTX, VCL_INT coll)
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);
}
VCL_VOID
@@ -494,12 +426,12 @@ 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);
}
static char const *
@@ -530,13 +462,13 @@ 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) {
s = WS_Copy(ctx->ws, p, -1);
AN(s);
}
- locker_runlock(&rwlock);
+ pthread_rwlock_unlock(&rwlock);
}
return s;
}

Return to:

Send suggestions and report system problems to the System administrator.