Skip to content

Commit

Permalink
basic lock around hash_items counter
Browse files Browse the repository at this point in the history
could/should be an atomic. Previously all write mutations were wrapped with
cache_lock, but that's not the case anymore. Just enforce consistency around
the hash_items counter, which is used for hash table expansion.
  • Loading branch information
dormando committed Feb 7, 2015
1 parent c1f0262 commit 5d7dc88
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions assoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

static pthread_cond_t maintenance_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t maintenance_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t hash_items_counter_lock = PTHREAD_MUTEX_INITIALIZER;

typedef unsigned long int ub4; /* unsigned 4-byte quantities */
typedef unsigned char ub1; /* unsigned 1-byte quantities */
Expand Down Expand Up @@ -167,10 +168,12 @@ int assoc_insert(item *it, const uint32_t hv) {
primary_hashtable[hv & hashmask(hashpower)] = it;
}

pthread_mutex_lock(&hash_items_counter_lock);
hash_items++;
if (! expanding && hash_items > (hashsize(hashpower) * 3) / 2) {
assoc_start_expand();
}
pthread_mutex_unlock(&hash_items_counter_lock);

MEMCACHED_ASSOC_INSERT(ITEM_key(it), it->nkey, hash_items);
return 1;
Expand All @@ -181,7 +184,9 @@ void assoc_delete(const char *key, const size_t nkey, const uint32_t hv) {

if (*before) {
item *nxt;
pthread_mutex_lock(&hash_items_counter_lock);
hash_items--;
pthread_mutex_unlock(&hash_items_counter_lock);
/* The DTrace probe cannot be triggered as the last instruction
* due to possible tail-optimization by the compiler
*/
Expand Down

0 comments on commit 5d7dc88

Please sign in to comment.