diff options
author | Trond Norbye <Trond.Norbye@sun.com> | 2009-03-05 14:08:17 +0100 |
---|---|---|
committer | Trond Norbye <Trond.Norbye@sun.com> | 2009-03-05 14:08:17 +0100 |
commit | d9220d64213c7ff9ccd492abeb9f13076dfe9192 (patch) | |
tree | c53f7922952ffe31fbefa495436388bcd3a19ce5 | |
parent | 7c23e79f975e52a348937cf7ad7e238cc72aa1da (diff) | |
download | memcached-d9220d64213c7ff9ccd492abeb9f13076dfe9192.tar.gz |
Refactor: move the slabs mutex (and all usage of it) into slabs.c
-rw-r--r-- | memcached.h | 6 | ||||
-rw-r--r-- | slabs.c | 54 | ||||
-rw-r--r-- | slabs.h | 11 | ||||
-rw-r--r-- | thread.c | 43 |
4 files changed, 56 insertions, 58 deletions
diff --git a/memcached.h b/memcached.h index 07fdc2d..7b527f8 100644 --- a/memcached.h +++ b/memcached.h @@ -358,12 +358,6 @@ char *item_stats_sizes(uint32_t (*add_stats)(char *buf, const uint32_t vlen, void *cookie), void *c, int *bytes); void item_unlink(item *it); void item_update(item *it); -void *slabs_alloc(size_t size, unsigned int id); -void slabs_free(void *ptr, size_t size, unsigned int id); -int slabs_reassign(unsigned char srcid, unsigned char dstid); -char *slabs_stats(uint32_t (*add_stats)(char *buf, - const char *key, const uint16_t klen, const char *val, - const uint32_t vlen, void *cookie), void *c, int *buflen); void STATS_LOCK(void); void STATS_UNLOCK(void); @@ -21,6 +21,7 @@ #include <stdio.h> #include <string.h> #include <assert.h> +#include <pthread.h> #define POWER_SMALLEST 1 #define POWER_LARGEST 200 @@ -58,6 +59,11 @@ static void *mem_base = NULL; static void *mem_current = NULL; static size_t mem_avail = 0; +/** + * Access to the slab allocator is protected by this lock + */ +static pthread_mutex_t slabs_lock = PTHREAD_MUTEX_INITIALIZER; + /* * Forward Declarations */ @@ -220,7 +226,7 @@ static int do_slabs_newslab(const unsigned int id) { } /*@null@*/ -void *do_slabs_alloc(const size_t size, unsigned int id) { +static void *do_slabs_alloc(const size_t size, unsigned int id) { slabclass_t *p; void *ret = NULL; @@ -272,7 +278,7 @@ void *do_slabs_alloc(const size_t size, unsigned int id) { return ret; } -void do_slabs_free(void *ptr, const size_t size, unsigned int id) { +static void do_slabs_free(void *ptr, const size_t size, unsigned int id) { slabclass_t *p; assert(((item *)ptr)->slabs_clsid == 0); @@ -367,9 +373,12 @@ char *get_stats(const char *stat_type, int nkey, } /*@null@*/ -char *do_slabs_stats(uint32_t (*add_stats)(char *buf, - const char *key, const uint16_t klen, const char *val, - const uint32_t vlen, void *cookie), void *c, int *buflen) { +static char *do_slabs_stats(uint32_t (*add_stats)(char *buf, const char *key, + const uint16_t klen, + const char *val, + const uint32_t vlen, + void *cookie), + void *c, int *buflen) { int i, total, linelen; char *buf = (char *)malloc(power_largest * 200 + 100); char *bufcurr = buf; @@ -533,6 +542,15 @@ int do_slabs_reassign(unsigned char srcid, unsigned char dstid) { } return 1; } + +int slabs_reassign(unsigned char srcid, unsigned char dstid) { + int ret; + + pthread_mutex_lock(&slabs_lock); + ret = do_slabs_reassign(srcid, dstid); + pthread_mutex_unlock(&slabs_lock); + return ret; +} #endif static void *memory_allocate(size_t size) { @@ -563,3 +581,29 @@ static void *memory_allocate(size_t size) { return ret; } + +void *slabs_alloc(size_t size, unsigned int id) { + void *ret; + + pthread_mutex_lock(&slabs_lock); + ret = do_slabs_alloc(size, id); + pthread_mutex_unlock(&slabs_lock); + return ret; +} + +void slabs_free(void *ptr, size_t size, unsigned int id) { + pthread_mutex_lock(&slabs_lock); + do_slabs_free(ptr, size, id); + pthread_mutex_unlock(&slabs_lock); +} + +char *slabs_stats(uint32_t (*add_stats)(char *buf, + const char *key, const uint16_t klen, const char *val, + const uint32_t vlen, void *cookie), void *c, int *buflen) { + char *ret; + + pthread_mutex_lock(&slabs_lock); + ret = do_slabs_stats(add_stats, c, buflen); + pthread_mutex_unlock(&slabs_lock); + return ret; +} @@ -1,4 +1,6 @@ /* slabs memory allocation */ +#ifndef SLABS_H +#define SLABS_H /** Init the subsystem. 1st argument is the limit on no. of bytes to allocate, 0 if no limit. 2nd argument is the growth factor; each slab will use a chunk @@ -17,10 +19,10 @@ void slabs_init(const size_t limit, const double factor, const bool prealloc); unsigned int slabs_clsid(const size_t size); /** Allocate object of given length. 0 on error */ /*@null@*/ -void *do_slabs_alloc(const size_t size, unsigned int id); +void *slabs_alloc(const size_t size, unsigned int id); /** Free previously allocated object */ -void do_slabs_free(void *ptr, size_t size, unsigned int id); +void slabs_free(void *ptr, size_t size, unsigned int id); /** Return a datum for stats in binary protocol */ char *get_stats(const char *stat_type, int nkey, @@ -29,7 +31,7 @@ char *get_stats(const char *stat_type, int nkey, const uint32_t vlen, void *cookie), void *arg, int *buflen); /** Fill buffer with stats */ /*@null@*/ -char *do_slabs_stats(uint32_t (*add_stats)(char *buf, +char *slabs_stats(uint32_t (*add_stats)(char *buf, const char *key, const uint16_t klen, const char *val, const uint32_t vlen, void *cookie), void *c, int *buflen); @@ -37,5 +39,6 @@ char *do_slabs_stats(uint32_t (*add_stats)(char *buf, 1 = success 0 = fail -1 = tried. busy. send again shortly. */ -int do_slabs_reassign(unsigned char srcid, unsigned char dstid); +int slabs_reassign(unsigned char srcid, unsigned char dstid); +#endif @@ -44,9 +44,6 @@ static pthread_mutex_t suffix_lock; /* Lock for cache operations (item_*, assoc_*) */ pthread_mutex_t cache_lock; -/* Lock for slab allocator operations */ -static pthread_mutex_t slabs_lock; - /* Lock for global stats */ static pthread_mutex_t stats_lock; @@ -524,45 +521,6 @@ char *item_stats_sizes(uint32_t (*add_stats)(char *buf, return ret; } -/******************************* SLAB ALLOCATOR ******************************/ - -void *slabs_alloc(size_t size, unsigned int id) { - void *ret; - - pthread_mutex_lock(&slabs_lock); - ret = do_slabs_alloc(size, id); - pthread_mutex_unlock(&slabs_lock); - return ret; -} - -void slabs_free(void *ptr, size_t size, unsigned int id) { - pthread_mutex_lock(&slabs_lock); - do_slabs_free(ptr, size, id); - pthread_mutex_unlock(&slabs_lock); -} - -char *slabs_stats(uint32_t (*add_stats)(char *buf, - const char *key, const uint16_t klen, const char *val, - const uint32_t vlen, void *cookie), void *c, int *buflen) { - char *ret; - - pthread_mutex_lock(&slabs_lock); - ret = do_slabs_stats(add_stats, c, buflen); - pthread_mutex_unlock(&slabs_lock); - return ret; -} - -#ifdef ALLOW_SLABS_REASSIGN -int slabs_reassign(unsigned char srcid, unsigned char dstid) { - int ret; - - pthread_mutex_lock(&slabs_lock); - ret = do_slabs_reassign(srcid, dstid); - pthread_mutex_unlock(&slabs_lock); - return ret; -} -#endif - /******************************* GLOBAL STATS ******************************/ void STATS_LOCK() { @@ -619,7 +577,6 @@ void thread_init(int nthreads, struct event_base *main_base) { pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&conn_lock, NULL); - pthread_mutex_init(&slabs_lock, NULL); pthread_mutex_init(&stats_lock, NULL); pthread_mutex_init(&init_lock, NULL); |