From d1d225c8db82d45bbd9abadae352da6ec1f5b3da Mon Sep 17 00:00:00 2001 From: yuqing Date: Fri, 22 May 2015 14:24:09 +0800 Subject: [PATCH] fast_mblock.c support none lock --- HISTORY | 3 ++- src/fast_mblock.c | 20 +++++++++++--------- src/fast_mblock.h | 6 ++++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/HISTORY b/HISTORY index 85eb7ff..56f1aba 100644 --- a/HISTORY +++ b/HISTORY @@ -1,11 +1,12 @@ -Version 1.14 2015-05-15 +Version 1.14 2015-05-22 * fast_task_info support set_buffer_size and realloc_buffer * use file lock when write logger header * bugfixed: sockopt.c correct fsbytes to sbytes in FreeBSD * macro FDFS_WRITE_BUFF_SIZE change to FAST_WRITE_BUFF_SIZE * logger.c call log_check_rotate in lock * bug fixed: log header correctly when rotate + * fast_mblock.c support none lock Version 1.13 2015-02-27 * support php extension diff --git a/src/fast_mblock.c b/src/fast_mblock.c index 4e4ecd1..d7a0080 100644 --- a/src/fast_mblock.c +++ b/src/fast_mblock.c @@ -10,7 +10,8 @@ #include "pthread_func.h" int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, - const int alloc_elements_once, fast_mblock_alloc_init_func init_func) + const int alloc_elements_once, fast_mblock_alloc_init_func init_func, + const bool need_lock) { int result; @@ -35,7 +36,7 @@ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, mblock->alloc_elements_once = (1024 * 1024) / block_size; } - if ((result=init_pthread_lock(&(mblock->lock))) != 0) + if (need_lock && (result=init_pthread_lock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_lock fail, errno: %d, error info: %s", \ @@ -47,6 +48,7 @@ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, mblock->malloc_chain_head = NULL; mblock->free_chain_head = NULL; mblock->total_count = 0; + mblock->need_lock = need_lock; return 0; } @@ -139,7 +141,7 @@ void fast_mblock_destroy(struct fast_mblock_man *mblock) mblock->free_chain_head = NULL; mblock->total_count = 0; - pthread_mutex_destroy(&(mblock->lock)); + if (mblock->need_lock) pthread_mutex_destroy(&(mblock->lock)); } struct fast_mblock_node *fast_mblock_alloc(struct fast_mblock_man *mblock) @@ -147,7 +149,7 @@ struct fast_mblock_node *fast_mblock_alloc(struct fast_mblock_man *mblock) struct fast_mblock_node *pNode; int result; - if ((result=pthread_mutex_lock(&(mblock->lock))) != 0) + if (mblock->need_lock && (result=pthread_mutex_lock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ @@ -174,7 +176,7 @@ struct fast_mblock_node *fast_mblock_alloc(struct fast_mblock_man *mblock) } } - if ((result=pthread_mutex_unlock(&(mblock->lock))) != 0) + if (mblock->need_lock && (result=pthread_mutex_unlock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ @@ -190,7 +192,7 @@ int fast_mblock_free(struct fast_mblock_man *mblock, \ { int result; - if ((result=pthread_mutex_lock(&(mblock->lock))) != 0) + if (mblock->need_lock && (result=pthread_mutex_lock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ @@ -202,7 +204,7 @@ int fast_mblock_free(struct fast_mblock_man *mblock, \ pNode->next = mblock->free_chain_head; mblock->free_chain_head = pNode; - if ((result=pthread_mutex_unlock(&(mblock->lock))) != 0) + if (mblock->need_lock && (result=pthread_mutex_unlock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ @@ -219,7 +221,7 @@ int fast_mblock_free_count(struct fast_mblock_man *mblock) int count; int result; - if ((result=pthread_mutex_lock(&(mblock->lock))) != 0) + if (mblock->need_lock && (result=pthread_mutex_lock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ @@ -236,7 +238,7 @@ int fast_mblock_free_count(struct fast_mblock_man *mblock) count++; } - if ((result=pthread_mutex_unlock(&(mblock->lock))) != 0) + if (mblock->need_lock && (result=pthread_mutex_unlock(&(mblock->lock))) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ diff --git a/src/fast_mblock.h b/src/fast_mblock.h index b383af0..a4ec29e 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -41,6 +41,7 @@ struct fast_mblock_man int element_size; //element size int alloc_elements_once; //alloc elements once int total_count; //total element count + bool need_lock; pthread_mutex_t lock; //the lock for read / write free node chain }; @@ -53,7 +54,7 @@ extern "C" { #endif #define fast_mblock_init(mblock, element_size, alloc_elements_once) \ - fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL) + fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL, true) /** mblock init @@ -64,7 +65,8 @@ parameters: return error no, 0 for success, != 0 fail */ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, - const int alloc_elements_once, fast_mblock_alloc_init_func init_func); + const int alloc_elements_once, fast_mblock_alloc_init_func init_func, + const bool need_lock); /** mblock destroy