Skip to content

Commit

Permalink
push cache_lock deeper into item_alloc
Browse files Browse the repository at this point in the history
easy win without restructuring item_alloc more: push the lock down after it's
done fiddling with snprintf.
  • Loading branch information
dormando committed Oct 5, 2011
1 parent 8af7504 commit 90ce9e2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 4 additions & 0 deletions items.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
if (id == 0)
return 0;

mutex_lock(&cache_lock);
/* do a quick check if we have any expired items in the tail.. */
item *search;
rel_time_t oldest_live = settings.oldest_live;
Expand Down Expand Up @@ -133,6 +134,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
(search->exptime == 0 || search->exptime > current_time)) {
if (settings.evict_to_free == 0) {
itemstats[id].outofmemory++;
pthread_mutex_unlock(&cache_lock);
return NULL;
}
itemstats[id].evicted++;
Expand Down Expand Up @@ -174,6 +176,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
search->refcount = 0;
do_item_unlink_nolock(search, hash(ITEM_key(search), search->nkey, 0));
}
pthread_mutex_unlock(&cache_lock);
return NULL;
}

Expand All @@ -193,6 +196,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
it->exptime = exptime;
memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
it->nsuffix = nsuffix;
pthread_mutex_unlock(&cache_lock);
return it;
}

Expand Down
3 changes: 1 addition & 2 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,8 @@ int is_listen_thread() {
*/
item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) {
item *it;
mutex_lock(&cache_lock);
/* do_item_alloc handles its own locks */
it = do_item_alloc(key, nkey, flags, exptime, nbytes);
pthread_mutex_unlock(&cache_lock);
return it;
}

Expand Down

0 comments on commit 90ce9e2

Please sign in to comment.