Permalink
Browse files

Refactor: move the slabs mutex (and all usage of it) into slabs.c

  • Loading branch information...
1 parent 7c23e79 commit d9220d64213c7ff9ccd492abeb9f13076dfe9192 Trond Norbye committed Mar 5, 2009
Showing with 56 additions and 58 deletions.
  1. +0 −6 memcached.h
  2. +49 −5 slabs.c
  3. +7 −4 slabs.h
  4. +0 −43 thread.c
View
@@ -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);
View
54 slabs.c
@@ -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;
+}
View
11 slabs.h
@@ -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,13 +31,14 @@ 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);
/* Request some slab be moved between classes
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
View
@@ -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);

0 comments on commit d9220d6

Please sign in to comment.