From b9cae5de7f5ba7cb35a20800308fc91bc07cac60 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sun, 10 May 2020 20:29:05 +0800 Subject: [PATCH] add macro PTHREAD_MUTEX_LOCK --- src/ini_file_reader.c | 6 ------ src/pthread_func.h | 26 ++++++++++++++++++++++++++ src/tests/test_pthread_lock.c | 5 +++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index bdd6fd3..86fb6f4 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -1344,9 +1344,6 @@ static inline int checkInitDynamicContentArray() { return 0; } - - logInfo("file: "__FILE__", line: %d, func: %s, " - "init_pthread_lock", __LINE__, __FUNCTION__); return init_pthread_lock(&g_dynamic_content_array.lock); } @@ -1380,9 +1377,6 @@ static int checkAllocDynamicContentArray() free(g_dynamic_content_array.contents); } - logInfo("file: "__FILE__", line: %d, func: %s, " - "alloc count: %d", __LINE__, __FUNCTION__, alloc); - g_dynamic_content_array.contents = contents; g_dynamic_content_array.alloc = alloc; return 0; diff --git a/src/pthread_func.h b/src/pthread_func.h index 5984b53..a5c78ef 100644 --- a/src/pthread_func.h +++ b/src/pthread_func.h @@ -24,6 +24,32 @@ extern "C" { int init_pthread_lock(pthread_mutex_t *pthread_lock); int init_pthread_attr(pthread_attr_t *pattr, const int stack_size); +#define PTHREAD_MUTEX_LOCK(lock) \ + do { \ + int lock_res; \ + if ((lock_res=pthread_mutex_lock(lock)) != 0) \ + { \ + logWarning("file: "__FILE__", line: %d, " \ + "call pthread_mutex_lock fail, " \ + "errno: %d, error info: %s", \ + __LINE__, lock_res, STRERROR(lock_res)); \ + } \ + } while (0) + + +#define PTHREAD_MUTEX_UNLOCK(lock) \ + do { \ + int unlock_res; \ + if ((unlock_res=pthread_mutex_unlock(lock)) != 0) \ + { \ + logWarning("file: "__FILE__", line: %d, " \ + "call pthread_mutex_unlock fail, " \ + "errno: %d, error info: %s", \ + __LINE__, unlock_res, STRERROR(unlock_res)); \ + } \ + } while (0) + + int create_work_threads(int *count, void *(*start_func)(void *), void **args, pthread_t *tids, const int stack_size); diff --git a/src/tests/test_pthread_lock.c b/src/tests/test_pthread_lock.c index eb41f4b..b4965de 100644 --- a/src/tests/test_pthread_lock.c +++ b/src/tests/test_pthread_lock.c @@ -46,6 +46,11 @@ int main(int argc, char *argv[]) "%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff), sum, get_current_time_ms() - start_time); + printf("lock 1: %d\n", pthread_mutex_lock(&lock)); + printf("lock 2: %d\n", pthread_mutex_lock(&lock)); + printf("unlock 1: %d\n", pthread_mutex_unlock(&lock)); + printf("unlock 2: %d\n", pthread_mutex_unlock(&lock)); + start_time = get_current_time_ms(); sum = 0; for (k=1; k<=LOOP_COUNT; k++) {