From 4b085fbc0953b24aa1e48ba48c921e101020237e Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 22 Jul 2020 17:54:02 +0800 Subject: [PATCH] add functions: get thread counters --- src/tests/test_thread_pool.c | 22 ++++++++++++++-------- src/thread_pool.c | 5 ----- src/thread_pool.h | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/tests/test_thread_pool.c b/src/tests/test_thread_pool.c index 10f0401..9e3384f 100644 --- a/src/tests/test_thread_pool.c +++ b/src/tests/test_thread_pool.c @@ -81,6 +81,17 @@ int test(FCThreadPool *pool) return 0; } +static void output(FCThreadPool *pool, const int64_t start_time) +{ + printf("thread pool dealing count: %d, avail count: %d, " + "counter: %d, total: %"PRId64", time used: %"PRId64" ms\n", + fc_thread_pool_dealing_count(pool), + fc_thread_pool_avail_count(pool), + __sync_add_and_fetch(&counter, 0), + __sync_add_and_fetch(&total, 0), + get_current_time_ms() - start_time); +} + int main(int argc, char *argv[]) { FCThreadPool pool; @@ -104,12 +115,10 @@ int main(int argc, char *argv[]) } result = test(&pool); + output(&pool, start_time); sleep(10); - printf("counter: %d, total: %"PRId64", time used: %"PRId64" ms\n", - __sync_add_and_fetch(&counter, 0), - __sync_add_and_fetch(&total, 0), - get_current_time_ms() - start_time); + output(&pool, start_time); result = test(&pool); sleep(5); @@ -117,10 +126,7 @@ int main(int argc, char *argv[]) continue_flag = false; sleep(2); - printf("counter: %d, total: %"PRId64", time used: %"PRId64" ms\n", - __sync_add_and_fetch(&counter, 0), - __sync_add_and_fetch(&total, 0), - get_current_time_ms() - start_time); + output(&pool, start_time); fc_thread_pool_destroy(&pool); logInfo("exit"); diff --git a/src/thread_pool.c b/src/thread_pool.c index e60cf8c..0cd887f 100644 --- a/src/thread_pool.c +++ b/src/thread_pool.c @@ -3,7 +3,6 @@ #include #include #include -#include "pthread_func.h" #include "sched_thread.h" #include "fc_memory.h" #include "thread_pool.h" @@ -204,10 +203,6 @@ int fc_thread_pool_run(FCThreadPool *pool, fc_thread_pool_callback func, struct timespec ts; int result; - if (func == NULL) { - return EINVAL; - } - thread = NULL; ts.tv_nsec = 0; PTHREAD_MUTEX_LOCK(&pool->lock); diff --git a/src/thread_pool.h b/src/thread_pool.h index 27be837..9392cee 100644 --- a/src/thread_pool.h +++ b/src/thread_pool.h @@ -4,6 +4,7 @@ #include #include #include "fast_mblock.h" +#include "pthread_func.h" typedef void (*fc_thread_pool_callback)(void *arg); @@ -52,6 +53,28 @@ void fc_thread_pool_destroy(FCThreadPool *pool); int fc_thread_pool_run(FCThreadPool *pool, fc_thread_pool_callback func, void *arg); +static inline int fc_thread_pool_dealing_count(FCThreadPool *pool) +{ + return __sync_add_and_fetch(&pool->thread_counts.dealing, 0); +} + +static inline int fc_thread_pool_avail_count(FCThreadPool *pool) +{ + return pool->thread_counts.limit - + __sync_add_and_fetch(&pool->thread_counts.dealing, 0); +} + +static inline int fc_thread_pool_running_count(FCThreadPool *pool) +{ + int running_count; + + PTHREAD_MUTEX_LOCK(&pool->lock); + running_count = pool->thread_counts.running; + PTHREAD_MUTEX_UNLOCK(&pool->lock); + + return running_count; +} + #ifdef __cplusplus } #endif