Skip to content

Commit 90ce9e2

Browse files
committed
push cache_lock deeper into item_alloc
easy win without restructuring item_alloc more: push the lock down after it's done fiddling with snprintf.
1 parent 8af7504 commit 90ce9e2

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

items.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
9898
if (id == 0)
9999
return 0;
100100

101+
mutex_lock(&cache_lock);
101102
/* do a quick check if we have any expired items in the tail.. */
102103
item *search;
103104
rel_time_t oldest_live = settings.oldest_live;
@@ -133,6 +134,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
133134
(search->exptime == 0 || search->exptime > current_time)) {
134135
if (settings.evict_to_free == 0) {
135136
itemstats[id].outofmemory++;
137+
pthread_mutex_unlock(&cache_lock);
136138
return NULL;
137139
}
138140
itemstats[id].evicted++;
@@ -174,6 +176,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
174176
search->refcount = 0;
175177
do_item_unlink_nolock(search, hash(ITEM_key(search), search->nkey, 0));
176178
}
179+
pthread_mutex_unlock(&cache_lock);
177180
return NULL;
178181
}
179182

@@ -193,6 +196,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
193196
it->exptime = exptime;
194197
memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
195198
it->nsuffix = nsuffix;
199+
pthread_mutex_unlock(&cache_lock);
196200
return it;
197201
}
198202

thread.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,8 @@ int is_listen_thread() {
340340
*/
341341
item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) {
342342
item *it;
343-
mutex_lock(&cache_lock);
343+
/* do_item_alloc handles its own locks */
344344
it = do_item_alloc(key, nkey, flags, exptime, nbytes);
345-
pthread_mutex_unlock(&cache_lock);
346345
return it;
347346
}
348347

0 commit comments

Comments
 (0)