diff --git a/HISTORY b/HISTORY index 4de0c13..9e7aeac 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-03-10 +Version 1.44 2020-03-12 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -19,6 +19,7 @@ Version 1.44 2020-03-10 * struct thread_data add waiting_queue for Linux eventfd notify * struct fast_task_info add canceled field for complicated thread model * nio_thread_data support thread notify + * pthread_func.[hc] add functions: create_work_threads_ex and fc_create_thread Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/pthread_func.c b/src/pthread_func.c index a66bd10..8bc71bd 100644 --- a/src/pthread_func.c +++ b/src/pthread_func.c @@ -29,36 +29,33 @@ int init_pthread_lock(pthread_mutex_t *pthread_lock) pthread_mutexattr_t mat; int result; - if ((result=pthread_mutexattr_init(&mat)) != 0) - { - logError("file: "__FILE__", line: %d, " \ - "call pthread_mutexattr_init fail, " \ - "errno: %d, error info: %s", \ + if ((result=pthread_mutexattr_init(&mat)) != 0) { + logError("file: "__FILE__", line: %d, " + "call pthread_mutexattr_init fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } - if ((result=pthread_mutexattr_settype(&mat, \ + if ((result=pthread_mutexattr_settype(&mat, PTHREAD_MUTEX_ERRORCHECK)) != 0) { - logError("file: "__FILE__", line: %d, " \ - "call pthread_mutexattr_settype fail, " \ - "errno: %d, error info: %s", \ + logError("file: "__FILE__", line: %d, " + "call pthread_mutexattr_settype fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } - if ((result=pthread_mutex_init(pthread_lock, &mat)) != 0) - { - logError("file: "__FILE__", line: %d, " \ - "call pthread_mutex_init fail, " \ - "errno: %d, error info: %s", \ + if ((result=pthread_mutex_init(pthread_lock, &mat)) != 0) { + logError("file: "__FILE__", line: %d, " + "call pthread_mutex_init fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } - if ((result=pthread_mutexattr_destroy(&mat)) != 0) - { - logError("file: "__FILE__", line: %d, " \ - "call thread_mutexattr_destroy fail, " \ - "errno: %d, error info: %s", \ + if ((result=pthread_mutexattr_destroy(&mat)) != 0) { + logError("file: "__FILE__", line: %d, " + "call thread_mutexattr_destroy fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } @@ -72,46 +69,35 @@ int init_pthread_attr(pthread_attr_t *pattr, const int stack_size) size_t new_stack_size; int result; - if ((result=pthread_attr_init(pattr)) != 0) - { - logError("file: "__FILE__", line: %d, " \ - "call pthread_attr_init fail, " \ - "errno: %d, error info: %s", \ + if ((result=pthread_attr_init(pattr)) != 0) { + logError("file: "__FILE__", line: %d, " + "call pthread_attr_init fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } - if ((result=pthread_attr_getstacksize(pattr, &old_stack_size)) != 0) - { - logError("file: "__FILE__", line: %d, " \ - "call pthread_attr_getstacksize fail, " \ - "errno: %d, error info: %s", \ + if ((result=pthread_attr_getstacksize(pattr, &old_stack_size)) != 0) { + logError("file: "__FILE__", line: %d, " + "call pthread_attr_getstacksize fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } - if (stack_size > 0) - { - if (old_stack_size != stack_size) - { + if (stack_size > 0) { + if (old_stack_size != stack_size) { new_stack_size = stack_size; - } - else - { + } else { new_stack_size = 0; } - } - else if (old_stack_size < 1 * 1024 * 1024) - { + } else if (old_stack_size < 1 * 1024 * 1024) { new_stack_size = 1 * 1024 * 1024; - } - else - { + } else { new_stack_size = 0; } - if (new_stack_size > 0) - { + if (new_stack_size > 0) { if ((result=pthread_attr_setstacksize(pattr, new_stack_size)) != 0) { @@ -123,12 +109,12 @@ int init_pthread_attr(pthread_attr_t *pattr, const int stack_size) } } - if ((result=pthread_attr_setdetachstate(pattr, \ + if ((result=pthread_attr_setdetachstate(pattr, PTHREAD_CREATE_DETACHED)) != 0) { - logError("file: "__FILE__", line: %d, " \ - "call pthread_attr_setdetachstate fail, " \ - "errno: %d, error info: %s", \ + logError("file: "__FILE__", line: %d, " + "call pthread_attr_setdetachstate fail, " + "errno: %d, error info: %s", __LINE__, result, STRERROR(result)); return result; } @@ -139,25 +125,47 @@ int init_pthread_attr(pthread_attr_t *pattr, const int stack_size) int create_work_threads(int *count, void *(*start_func)(void *), void **args, pthread_t *tids, const int stack_size) { +#define FIXED_TID_COUNT 256 + int result; pthread_attr_t thread_attr; void **current_arg; + pthread_t fixed_tids[FIXED_TID_COUNT]; + pthread_t *the_tids; pthread_t *ptid; pthread_t *ptid_end; - if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) - { + if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { return result; } + if (tids != NULL) { + the_tids = tids; + } else { + if (*count <= FIXED_TID_COUNT) { + the_tids = fixed_tids; + } else { + int bytes; + bytes = sizeof(pthread_t) * *count; + the_tids = (pthread_t *)malloc(bytes); + if (the_tids == NULL) { + logError("file: "__FILE__", line: %d, " + "malloc %d bytes fail", __LINE__, bytes); + pthread_attr_destroy(&thread_attr); + return ENOMEM; + } + } + } + result = 0; - ptid_end = tids + (*count); - for (ptid=tids,current_arg=args; ptid