fast_mblock.c support none lock

pull/4/head
yuqing 2015-05-22 14:24:09 +08:00
parent 0fd3425fb6
commit d1d225c8db
3 changed files with 17 additions and 12 deletions

View File

@ -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 * fast_task_info support set_buffer_size and realloc_buffer
* use file lock when write logger header * use file lock when write logger header
* bugfixed: sockopt.c correct fsbytes to sbytes in FreeBSD * bugfixed: sockopt.c correct fsbytes to sbytes in FreeBSD
* macro FDFS_WRITE_BUFF_SIZE change to FAST_WRITE_BUFF_SIZE * macro FDFS_WRITE_BUFF_SIZE change to FAST_WRITE_BUFF_SIZE
* logger.c call log_check_rotate in lock * logger.c call log_check_rotate in lock
* bug fixed: log header correctly when rotate * bug fixed: log header correctly when rotate
* fast_mblock.c support none lock
Version 1.13 2015-02-27 Version 1.13 2015-02-27
* support php extension * support php extension

View File

@ -10,7 +10,8 @@
#include "pthread_func.h" #include "pthread_func.h"
int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, 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; 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; 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, " \ logError("file: "__FILE__", line: %d, " \
"init_pthread_lock fail, errno: %d, error info: %s", \ "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->malloc_chain_head = NULL;
mblock->free_chain_head = NULL; mblock->free_chain_head = NULL;
mblock->total_count = 0; mblock->total_count = 0;
mblock->need_lock = need_lock;
return 0; return 0;
} }
@ -139,7 +141,7 @@ void fast_mblock_destroy(struct fast_mblock_man *mblock)
mblock->free_chain_head = NULL; mblock->free_chain_head = NULL;
mblock->total_count = 0; 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) 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; struct fast_mblock_node *pNode;
int result; 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, " \ logError("file: "__FILE__", line: %d, " \
"call pthread_mutex_lock fail, " \ "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, " \ logError("file: "__FILE__", line: %d, " \
"call pthread_mutex_unlock fail, " \ "call pthread_mutex_unlock fail, " \
@ -190,7 +192,7 @@ int fast_mblock_free(struct fast_mblock_man *mblock, \
{ {
int result; 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, " \ logError("file: "__FILE__", line: %d, " \
"call pthread_mutex_lock fail, " \ "call pthread_mutex_lock fail, " \
@ -202,7 +204,7 @@ int fast_mblock_free(struct fast_mblock_man *mblock, \
pNode->next = mblock->free_chain_head; pNode->next = mblock->free_chain_head;
mblock->free_chain_head = pNode; 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, " \ logError("file: "__FILE__", line: %d, " \
"call pthread_mutex_unlock fail, " \ "call pthread_mutex_unlock fail, " \
@ -219,7 +221,7 @@ int fast_mblock_free_count(struct fast_mblock_man *mblock)
int count; int count;
int result; 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, " \ logError("file: "__FILE__", line: %d, " \
"call pthread_mutex_lock fail, " \ "call pthread_mutex_lock fail, " \
@ -236,7 +238,7 @@ int fast_mblock_free_count(struct fast_mblock_man *mblock)
count++; 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, " \ logError("file: "__FILE__", line: %d, " \
"call pthread_mutex_unlock fail, " \ "call pthread_mutex_unlock fail, " \

View File

@ -41,6 +41,7 @@ struct fast_mblock_man
int element_size; //element size int element_size; //element size
int alloc_elements_once; //alloc elements once int alloc_elements_once; //alloc elements once
int total_count; //total element count int total_count; //total element count
bool need_lock;
pthread_mutex_t lock; //the lock for read / write free node chain pthread_mutex_t lock; //the lock for read / write free node chain
}; };
@ -53,7 +54,7 @@ extern "C" {
#endif #endif
#define fast_mblock_init(mblock, element_size, alloc_elements_once) \ #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 mblock init
@ -64,7 +65,8 @@ parameters:
return error no, 0 for success, != 0 fail return error no, 0 for success, != 0 fail
*/ */
int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, 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 mblock destroy