Skip to content

Commit a456210

Browse files
Trond NorbyeTrond Norbye
Trond Norbye
authored and
Trond Norbye
committed
Moved conn_lock and suffix_lock out of thread.c (the latter wasn't properly initialized....)
1 parent 9a05216 commit a456210

File tree

3 files changed

+36
-95
lines changed

3 files changed

+36
-95
lines changed

memcached.c

+36-23
Original file line numberDiff line numberDiff line change
@@ -230,52 +230,58 @@ static int add_msghdr(conn *c)
230230
static conn **freeconns;
231231
static int freetotal;
232232
static int freecurr;
233+
/* Lock for connection freelist */
234+
static pthread_mutex_t conn_lock = PTHREAD_MUTEX_INITIALIZER;
233235

234236

235237
static void conn_init(void) {
236238
freetotal = 200;
237239
freecurr = 0;
238-
if ((freeconns = (conn **)malloc(sizeof(conn *) * freetotal)) == NULL) {
239-
fprintf(stderr, "malloc()\n");
240+
if ((freeconns = calloc(freetotal, sizeof(conn *))) == NULL) {
241+
fprintf(stderr, "Failed to allocate connection structures\n");
240242
}
241243
return;
242244
}
243245

244246
/*
245-
* Returns a connection from the freelist, if any. Should call this using
246-
* conn_from_freelist() for thread safety.
247+
* Returns a connection from the freelist, if any.
247248
*/
248-
conn *do_conn_from_freelist() {
249+
conn *conn_from_freelist() {
249250
conn *c;
250251

252+
pthread_mutex_lock(&conn_lock);
251253
if (freecurr > 0) {
252254
c = freeconns[--freecurr];
253255
} else {
254256
c = NULL;
255257
}
258+
pthread_mutex_unlock(&conn_lock);
256259

257260
return c;
258261
}
259262

260263
/*
261-
* Adds a connection to the freelist. 0 = success. Should call this using
262-
* conn_add_to_freelist() for thread safety.
264+
* Adds a connection to the freelist. 0 = success.
263265
*/
264-
bool do_conn_add_to_freelist(conn *c) {
266+
bool conn_add_to_freelist(conn *c) {
267+
bool ret = true;
268+
pthread_mutex_lock(&conn_lock);
265269
if (freecurr < freetotal) {
266270
freeconns[freecurr++] = c;
267-
return false;
271+
ret = false;
268272
} else {
269273
/* try to enlarge free connections array */
270-
conn **new_freeconns = realloc(freeconns, sizeof(conn *) * freetotal * 2);
274+
size_t newsize = freetotal * 2;
275+
conn **new_freeconns = realloc(freeconns, sizeof(conn *) * newsize);
271276
if (new_freeconns) {
272-
freetotal *= 2;
277+
freetotal = newsize;
273278
freeconns = new_freeconns;
274279
freeconns[freecurr++] = c;
275-
return false;
280+
ret = false;
276281
}
277282
}
278-
return true;
283+
pthread_mutex_unlock(&conn_lock);
284+
return ret;
279285
}
280286

281287
static const char *prot_text(enum protocol prot) {
@@ -598,44 +604,50 @@ static void conn_set_state(conn *c, enum conn_states state) {
598604
static char **freesuffix;
599605
static int freesuffixtotal;
600606
static int freesuffixcurr;
607+
/* Lock for alternative item suffix freelist */
608+
static pthread_mutex_t suffix_lock = PTHREAD_MUTEX_INITIALIZER;
601609

602610
static void suffix_init(void) {
603611
freesuffixtotal = 500;
604612
freesuffixcurr = 0;
605613

606-
freesuffix = (char **)malloc( sizeof(char *) * freesuffixtotal );
614+
freesuffix = calloc(freesuffixtotal, sizeof(char *));
607615
if (freesuffix == NULL) {
608-
fprintf(stderr, "malloc()\n");
616+
fprintf(stderr, "Failed to allocate suffix pool\n");
609617
}
610618
return;
611619
}
612620

613621
/*
614-
* Returns a suffix buffer from the freelist, if any. Should call this using
615-
* suffix_from_freelist() for thread safety.
622+
* Returns a suffix buffer from the freelist, if any.
616623
*/
617-
char *do_suffix_from_freelist() {
624+
char *suffix_from_freelist() {
618625
char *s;
619626

627+
pthread_mutex_lock(&suffix_lock);
620628
if (freesuffixcurr > 0) {
621629
s = freesuffix[--freesuffixcurr];
622630
} else {
623631
/* If malloc fails, let the logic fall through without spamming
624632
* STDERR on the server. */
625633
s = malloc( SUFFIX_SIZE );
626634
}
635+
pthread_mutex_unlock(&suffix_lock);
627636

628637
return s;
629638
}
630639

631640
/*
632641
* Adds a connection to the freelist. 0 = success. Should call this using
633-
* conn_add_to_freelist() for thread safety.
642+
* suffix_add_to_freelist() for thread safety.
634643
*/
635-
bool do_suffix_add_to_freelist(char *s) {
644+
bool suffix_add_to_freelist(char *s) {
645+
bool ret = true;
646+
647+
pthread_mutex_lock(&suffix_lock);
636648
if (freesuffixcurr < freesuffixtotal) {
637649
freesuffix[freesuffixcurr++] = s;
638-
return false;
650+
ret = false;
639651
} else {
640652
/* try to enlarge free connections array */
641653
char **new_freesuffix = realloc(freesuffix,
@@ -644,10 +656,11 @@ bool do_suffix_add_to_freelist(char *s) {
644656
freesuffixtotal *= 2;
645657
freesuffix = new_freesuffix;
646658
freesuffix[freesuffixcurr++] = s;
647-
return false;
659+
ret = false;
648660
}
649661
}
650-
return true;
662+
pthread_mutex_unlock(&suffix_lock);
663+
return ret;
651664
}
652665

653666
/*

memcached.h

-5
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,6 @@ extern volatile rel_time_t current_time;
321321
/*
322322
* Functions
323323
*/
324-
325-
conn *do_conn_from_freelist(void);
326-
bool do_conn_add_to_freelist(conn *c);
327-
char *do_suffix_from_freelist(void);
328-
bool do_suffix_add_to_freelist(char *s);
329324
char *do_add_delta(conn *c, item *item, const bool incr, const int64_t delta,
330325
char *buf);
331326
enum store_item_type do_store_item(item *item, int comm, conn* c);

thread.c

-67
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ struct conn_queue {
3333
pthread_cond_t cond;
3434
};
3535

36-
/* Lock for connection freelist */
37-
static pthread_mutex_t conn_lock;
38-
39-
/* Lock for alternative item suffix freelist */
40-
static pthread_mutex_t suffix_lock;
41-
4236
/* Lock for cache operations (item_*, assoc_*) */
4337
pthread_mutex_t cache_lock;
4438

@@ -177,66 +171,6 @@ static void create_worker(void *(*func)(void *), void *arg) {
177171
}
178172
}
179173

180-
181-
/*
182-
* Pulls a conn structure from the freelist, if one is available.
183-
*/
184-
conn *conn_from_freelist() {
185-
conn *c;
186-
187-
pthread_mutex_lock(&conn_lock);
188-
c = do_conn_from_freelist();
189-
pthread_mutex_unlock(&conn_lock);
190-
191-
return c;
192-
}
193-
194-
195-
/*
196-
* Adds a conn structure to the freelist.
197-
*
198-
* Returns 0 on success, 1 if the structure couldn't be added.
199-
*/
200-
bool conn_add_to_freelist(conn *c) {
201-
bool result;
202-
203-
pthread_mutex_lock(&conn_lock);
204-
result = do_conn_add_to_freelist(c);
205-
pthread_mutex_unlock(&conn_lock);
206-
207-
return result;
208-
}
209-
210-
/*
211-
* Pulls a suffix buffer from the freelist, if one is available.
212-
*/
213-
char *suffix_from_freelist() {
214-
char *s;
215-
216-
pthread_mutex_lock(&suffix_lock);
217-
s = do_suffix_from_freelist();
218-
pthread_mutex_unlock(&suffix_lock);
219-
220-
return s;
221-
}
222-
223-
224-
/*
225-
* Adds a suffix buffer to the freelist.
226-
*
227-
* Returns 0 on success, 1 if the buffer couldn't be added.
228-
*/
229-
bool suffix_add_to_freelist(char *s) {
230-
bool result;
231-
232-
pthread_mutex_lock(&suffix_lock);
233-
result = do_suffix_add_to_freelist(s);
234-
pthread_mutex_unlock(&suffix_lock);
235-
236-
return result;
237-
}
238-
239-
240174
/****************************** LIBEVENT THREADS *****************************/
241175

242176
/*
@@ -634,7 +568,6 @@ void thread_init(int nthreads, struct event_base *main_base) {
634568
int i;
635569

636570
pthread_mutex_init(&cache_lock, NULL);
637-
pthread_mutex_init(&conn_lock, NULL);
638571
pthread_mutex_init(&stats_lock, NULL);
639572

640573
pthread_mutex_init(&init_lock, NULL);

0 commit comments

Comments
 (0)