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) (unidiff)
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;
74static struct entry **hash_table; 74static struct entry **hash_table;
75static size_t max_hash_size = 8192; 75static size_t max_hash_size = 8192;
76static ssize_t max_coll = -1; /* Max. number of collisions allowed */ 76static ssize_t max_coll = -1; /* Max. number of collisions allowed */
77 77static pthread_rwlock_t rwlock;
78typedef struct {
79 size_t readers;
80 size_t writer;
81 pthread_mutex_t mutex;
82 pthread_cond_t lock_free;
83} locker_t;
84
85static locker_t rwlock;
86
87static void
88locker_init(locker_t *rdwr)
89{
90 rdwr->readers = 0;
91 rdwr->writer = 0;
92 pthread_mutex_init(&rdwr->mutex, NULL);
93 pthread_cond_init(&rdwr->lock_free, NULL);
94}
95
96static void
97locker_rlock(locker_t *rdwr)
98{
99 pthread_mutex_lock(&rdwr->mutex);
100 while (rdwr->writer)
101 pthread_cond_wait(&rdwr->lock_free, &rdwr->mutex);
102 rdwr->readers++;
103 pthread_mutex_unlock(&rdwr->mutex);
104}
105
106static void
107locker_wlock(locker_t *rdwr)
108{
109 pthread_mutex_lock(&rdwr->mutex);
110 while (rdwr->writer || rdwr->readers)
111 pthread_cond_wait(&rdwr->lock_free, &rdwr->mutex);
112 rdwr->writer++;
113 pthread_mutex_unlock(&rdwr->mutex);
114}
115
116static int
117locker_runlock(locker_t *rdwr)
118{
119 pthread_mutex_lock(&rdwr->mutex);
120 if (rdwr->readers == 0) {
121 pthread_mutex_unlock(&rdwr->mutex);
122 return -1;
123 } else {
124 rdwr->readers--;
125 if (rdwr->readers == 0)
126 pthread_cond_signal(&rdwr->lock_free);
127 pthread_mutex_unlock(&rdwr->mutex);
128 return 0;
129 }
130}
131
132static int
133locker_wunlock(locker_t *rdwr)
134{
135 pthread_mutex_lock(&rdwr->mutex);
136 if (rdwr->writer == 0) {
137 pthread_mutex_unlock(&rdwr->mutex);
138 return -1;
139 } else {
140 rdwr->writer = 0;
141 pthread_cond_signal(&rdwr->lock_free);
142 pthread_mutex_unlock(&rdwr->mutex);
143 return 0;
144 }
145}
146 78
147static int cs_coll[] = { 79static int cs_coll[] = {
148 0, 1, 2, 3, 4, 5, 6, 7, 80 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)
440{ 372{
441 switch (e) { 373 switch (e) {
442 case VCL_EVENT_LOAD: 374 case VCL_EVENT_LOAD:
443 locker_init(&rwlock); 375 pthread_rwlock_init(&rwlock, NULL);
444 break; 376 break;
445 377
446 case VCL_EVENT_DISCARD: 378 case VCL_EVENT_DISCARD:
447 locker_wlock(&rwlock); 379 pthread_rwlock_wrlock(&rwlock);
448 while (ent_head) 380 while (ent_head)
449 entry_remove(ent_head); 381 entry_remove(ent_head);
450 free(hash_table); 382 free(hash_table);
451 hash_table = NULL; 383 hash_table = NULL;
452 hash_size = 0; 384 hash_size = 0;
453 locker_wunlock(&rwlock); 385 pthread_rwlock_unlock(&rwlock);
454 break; 386 break;
455 387
456 case VCL_EVENT_WARM: 388 case VCL_EVENT_WARM:
@@ -464,14 +396,14 @@ VCL_VOID
464vmod_ci(VRT_CTX, VCL_BOOL ci) 396vmod_ci(VRT_CTX, VCL_BOOL ci)
465{ 397{
466 int *cp; 398 int *cp;
467 locker_wlock(&rwlock); 399 pthread_rwlock_wrlock(&rwlock);
468 cp = ci ? ci_coll : cs_coll; 400 cp = ci ? ci_coll : cs_coll;
469 if (cp != collation) { 401 if (cp != collation) {
470 collation = cp; 402 collation = cp;
471 rehash(); 403 rehash();
472 fill_table(); 404 fill_table();
473 } 405 }
474 locker_wunlock(&rwlock); 406 pthread_rwlock_unlock(&rwlock);
475} 407}
476 408
477VCL_VOID 409VCL_VOID
@@ -483,10 +415,10 @@ vmod_collisions(VRT_CTX, VCL_INT coll)
483VCL_VOID 415VCL_VOID
484vmod_load(VRT_CTX, VCL_STRING file) 416vmod_load(VRT_CTX, VCL_STRING file)
485{ 417{
486 locker_wlock(&rwlock); 418 pthread_rwlock_wrlock(&rwlock);
487 load_entries(file); 419 load_entries(file);
488 fill_table(); 420 fill_table();
489 locker_wunlock(&rwlock); 421 pthread_rwlock_unlock(&rwlock);
490} 422}
491 423
492VCL_VOID 424VCL_VOID
@@ -494,12 +426,12 @@ vmod_clear(VRT_CTX)
494{ 426{
495 size_t i; 427 size_t i;
496 428
497 locker_wlock(&rwlock); 429 pthread_rwlock_wrlock(&rwlock);
498 for (i = 0; i < hash_size; i++) 430 for (i = 0; i < hash_size; i++)
499 hash_table[i] = NULL; 431 hash_table[i] = NULL;
500 while (ent_head) 432 while (ent_head)
501 entry_remove(ent_head); 433 entry_remove(ent_head);
502 locker_wunlock(&rwlock); 434 pthread_rwlock_unlock(&rwlock);
503 } 435 }
504 436
505static char const * 437static char const *
@@ -530,13 +462,13 @@ vmod_lookup(VRT_CTX, VCL_STRING key)
530{ 462{
531 char *s = NULL; 463 char *s = NULL;
532 if (key) { 464 if (key) {
533 locker_rlock(&rwlock); 465 pthread_rwlock_rdlock(&rwlock);
534 char const *p = lookup_unlocked(key); 466 char const *p = lookup_unlocked(key);
535 if (p) { 467 if (p) {
536 s = WS_Copy(ctx->ws, p, -1); 468 s = WS_Copy(ctx->ws, p, -1);
537 AN(s); 469 AN(s);
538 } 470 }
539 locker_runlock(&rwlock); 471 pthread_rwlock_unlock(&rwlock);
540 } 472 }
541 return s; 473 return s;
542} 474}

Return to:

Send suggestions and report system problems to the System administrator.