aboutsummaryrefslogtreecommitdiff
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
parente47faca1b8079c5aa13915e3a98d35c47fe0a78a (diff)
downloadvmod-dict-75a077fa0ee4b0094b6d43619cd0a068a82ebfd7.tar.gz
vmod-dict-75a077fa0ee4b0094b6d43619cd0a068a82ebfd7.tar.bz2
Switch to POSIX pthread_rwlock functions instead of the custom locker
-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
@@ -65,93 +65,25 @@ entry_remove(struct entry *ent)
p->prev = ent->prev;
else
ent_tail = ent->prev;
ent_count--;
free(ent);
}
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,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79,
@@ -431,84 +363,84 @@ fill_table(void)
if (max_coll <= 0 || cn < max_coll)
break;
next_size = next_size * 2 + 1;
} while (next_size < max_hash_size);
}
int
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:
case VCL_EVENT_COLD:
break;
}
return 0;
}
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
vmod_collisions(VRT_CTX, VCL_INT coll)
{
max_coll = 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
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 *
lookup_unlocked(char const *key)
{
size_t i, h;
char const *s = NULL;
if (hash_size == 0)
return NULL;
h = string_hash(key) % hash_size;
@@ -521,23 +453,23 @@ lookup_unlocked(char const *key)
i = (i + 1) % hash_size;
if (i == h)
break;
}
return s;
}
VCL_STRING
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.