add functions: create_work_threads_ex and fc_create_thread
parent
3d0956d302
commit
4d653d1b3e
3
HISTORY
3
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 test file src/tests/test_pthread_lock.c
|
||||||
* add uniq_skiplist.[hc]
|
* add uniq_skiplist.[hc]
|
||||||
* add function split_string_ex
|
* 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 thread_data add waiting_queue for Linux eventfd notify
|
||||||
* struct fast_task_info add canceled field for complicated thread model
|
* struct fast_task_info add canceled field for complicated thread model
|
||||||
* nio_thread_data support thread notify
|
* 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
|
Version 1.43 2019-12-25
|
||||||
* replace function call system to getExecResult,
|
* replace function call system to getExecResult,
|
||||||
|
|
|
||||||
|
|
@ -29,36 +29,33 @@ int init_pthread_lock(pthread_mutex_t *pthread_lock)
|
||||||
pthread_mutexattr_t mat;
|
pthread_mutexattr_t mat;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if ((result=pthread_mutexattr_init(&mat)) != 0)
|
if ((result=pthread_mutexattr_init(&mat)) != 0) {
|
||||||
{
|
logError("file: "__FILE__", line: %d, "
|
||||||
logError("file: "__FILE__", line: %d, " \
|
"call pthread_mutexattr_init fail, "
|
||||||
"call pthread_mutexattr_init fail, " \
|
"errno: %d, error info: %s",
|
||||||
"errno: %d, error info: %s", \
|
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if ((result=pthread_mutexattr_settype(&mat, \
|
if ((result=pthread_mutexattr_settype(&mat,
|
||||||
PTHREAD_MUTEX_ERRORCHECK)) != 0)
|
PTHREAD_MUTEX_ERRORCHECK)) != 0)
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, " \
|
logError("file: "__FILE__", line: %d, "
|
||||||
"call pthread_mutexattr_settype fail, " \
|
"call pthread_mutexattr_settype fail, "
|
||||||
"errno: %d, error info: %s", \
|
"errno: %d, error info: %s",
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if ((result=pthread_mutex_init(pthread_lock, &mat)) != 0)
|
if ((result=pthread_mutex_init(pthread_lock, &mat)) != 0) {
|
||||||
{
|
logError("file: "__FILE__", line: %d, "
|
||||||
logError("file: "__FILE__", line: %d, " \
|
"call pthread_mutex_init fail, "
|
||||||
"call pthread_mutex_init fail, " \
|
"errno: %d, error info: %s",
|
||||||
"errno: %d, error info: %s", \
|
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if ((result=pthread_mutexattr_destroy(&mat)) != 0)
|
if ((result=pthread_mutexattr_destroy(&mat)) != 0) {
|
||||||
{
|
logError("file: "__FILE__", line: %d, "
|
||||||
logError("file: "__FILE__", line: %d, " \
|
"call thread_mutexattr_destroy fail, "
|
||||||
"call thread_mutexattr_destroy fail, " \
|
"errno: %d, error info: %s",
|
||||||
"errno: %d, error info: %s", \
|
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -72,46 +69,35 @@ int init_pthread_attr(pthread_attr_t *pattr, const int stack_size)
|
||||||
size_t new_stack_size;
|
size_t new_stack_size;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if ((result=pthread_attr_init(pattr)) != 0)
|
if ((result=pthread_attr_init(pattr)) != 0) {
|
||||||
{
|
logError("file: "__FILE__", line: %d, "
|
||||||
logError("file: "__FILE__", line: %d, " \
|
"call pthread_attr_init fail, "
|
||||||
"call pthread_attr_init fail, " \
|
"errno: %d, error info: %s",
|
||||||
"errno: %d, error info: %s", \
|
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((result=pthread_attr_getstacksize(pattr, &old_stack_size)) != 0)
|
if ((result=pthread_attr_getstacksize(pattr, &old_stack_size)) != 0) {
|
||||||
{
|
logError("file: "__FILE__", line: %d, "
|
||||||
logError("file: "__FILE__", line: %d, " \
|
"call pthread_attr_getstacksize fail, "
|
||||||
"call pthread_attr_getstacksize fail, " \
|
"errno: %d, error info: %s",
|
||||||
"errno: %d, error info: %s", \
|
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stack_size > 0)
|
if (stack_size > 0) {
|
||||||
{
|
if (old_stack_size != stack_size) {
|
||||||
if (old_stack_size != stack_size)
|
|
||||||
{
|
|
||||||
new_stack_size = stack_size;
|
new_stack_size = stack_size;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
new_stack_size = 0;
|
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;
|
new_stack_size = 1 * 1024 * 1024;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
new_stack_size = 0;
|
new_stack_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (new_stack_size > 0)
|
if (new_stack_size > 0) {
|
||||||
{
|
|
||||||
if ((result=pthread_attr_setstacksize(pattr,
|
if ((result=pthread_attr_setstacksize(pattr,
|
||||||
new_stack_size)) != 0)
|
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)
|
PTHREAD_CREATE_DETACHED)) != 0)
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, " \
|
logError("file: "__FILE__", line: %d, "
|
||||||
"call pthread_attr_setdetachstate fail, " \
|
"call pthread_attr_setdetachstate fail, "
|
||||||
"errno: %d, error info: %s", \
|
"errno: %d, error info: %s",
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
return 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 *),
|
int create_work_threads(int *count, void *(*start_func)(void *),
|
||||||
void **args, pthread_t *tids, const int stack_size)
|
void **args, pthread_t *tids, const int stack_size)
|
||||||
{
|
{
|
||||||
|
#define FIXED_TID_COUNT 256
|
||||||
|
|
||||||
int result;
|
int result;
|
||||||
pthread_attr_t thread_attr;
|
pthread_attr_t thread_attr;
|
||||||
void **current_arg;
|
void **current_arg;
|
||||||
|
pthread_t fixed_tids[FIXED_TID_COUNT];
|
||||||
|
pthread_t *the_tids;
|
||||||
pthread_t *ptid;
|
pthread_t *ptid;
|
||||||
pthread_t *ptid_end;
|
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;
|
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;
|
result = 0;
|
||||||
ptid_end = tids + (*count);
|
ptid_end = the_tids + (*count);
|
||||||
for (ptid=tids,current_arg=args; ptid<ptid_end; ptid++,current_arg++)
|
for (ptid=the_tids,current_arg=args; ptid<ptid_end;
|
||||||
{
|
ptid++,current_arg++)
|
||||||
|
{
|
||||||
if ((result=pthread_create(ptid, &thread_attr,
|
if ((result=pthread_create(ptid, &thread_attr,
|
||||||
start_func, *current_arg)) != 0)
|
start_func, *current_arg)) != 0)
|
||||||
{
|
{
|
||||||
*count = ptid - tids;
|
*count = ptid - the_tids;
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"create threads #%d fail, "
|
"create threads #%d fail, "
|
||||||
"errno: %d, error info: %s",
|
"errno: %d, error info: %s",
|
||||||
|
|
@ -167,10 +175,52 @@ int create_work_threads(int *count, void *(*start_func)(void *),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (the_tids != tids && the_tids != fixed_tids) {
|
||||||
|
free(the_tids);
|
||||||
|
}
|
||||||
|
|
||||||
pthread_attr_destroy(&thread_attr);
|
pthread_attr_destroy(&thread_attr);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int create_work_threads_ex(int *count, void *(*start_func)(void *),
|
||||||
|
void *args, const int elment_size, pthread_t *tids,
|
||||||
|
const int stack_size)
|
||||||
|
{
|
||||||
|
#define FIXED_ARG_COUNT 256
|
||||||
|
|
||||||
|
void *fixed_args[FIXED_ARG_COUNT];
|
||||||
|
void **pp_args;
|
||||||
|
char *p;
|
||||||
|
int result;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (*count <= FIXED_ARG_COUNT) {
|
||||||
|
pp_args = fixed_args;
|
||||||
|
} else {
|
||||||
|
int bytes;
|
||||||
|
bytes = sizeof(void *) * (*count);
|
||||||
|
pp_args = (void **)malloc(bytes);
|
||||||
|
if (pp_args == NULL) {
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"malloc %d bytes fail", __LINE__, bytes);
|
||||||
|
return ENOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p = (char *)args;
|
||||||
|
for (i=0; i<*count; i++) {
|
||||||
|
pp_args[i] = p;
|
||||||
|
p += elment_size;
|
||||||
|
}
|
||||||
|
result = create_work_threads(count, start_func,
|
||||||
|
pp_args, tids, stack_size);
|
||||||
|
if (pp_args != fixed_args) {
|
||||||
|
free(pp_args);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int kill_work_threads(pthread_t *tids, const int count)
|
int kill_work_threads(pthread_t *tids, const int count)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
@ -178,13 +228,11 @@ int kill_work_threads(pthread_t *tids, const int count)
|
||||||
pthread_t *ptid_end;
|
pthread_t *ptid_end;
|
||||||
|
|
||||||
ptid_end = tids + count;
|
ptid_end = tids + count;
|
||||||
for (ptid=tids; ptid<ptid_end; ptid++)
|
for (ptid=tids; ptid<ptid_end; ptid++) {
|
||||||
{
|
if ((result=pthread_kill(*ptid, SIGINT)) != 0) {
|
||||||
if ((result=pthread_kill(*ptid, SIGINT)) != 0)
|
logError("file: "__FILE__", line: %d, "
|
||||||
{
|
"kill thread failed, "
|
||||||
logError("file: "__FILE__", line: %d, " \
|
"errno: %d, error info: %s",
|
||||||
"kill thread failed, " \
|
|
||||||
"errno: %d, error info: %s", \
|
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -192,3 +240,23 @@ int kill_work_threads(pthread_t *tids, const int count)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fc_create_thread(pthread_t *tid, void *(*start_func)(void *),
|
||||||
|
void *args, const int stack_size)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
pthread_attr_t thread_attr;
|
||||||
|
|
||||||
|
if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((result=pthread_create(tid, &thread_attr, start_func, args)) != 0) {
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"create thread fail, "
|
||||||
|
"errno: %d, error info: %s",
|
||||||
|
__LINE__, result, STRERROR(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_attr_destroy(&thread_attr);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,16 @@ int init_pthread_attr(pthread_attr_t *pattr, const int stack_size);
|
||||||
|
|
||||||
int create_work_threads(int *count, void *(*start_func)(void *),
|
int create_work_threads(int *count, void *(*start_func)(void *),
|
||||||
void **args, pthread_t *tids, const int stack_size);
|
void **args, pthread_t *tids, const int stack_size);
|
||||||
|
|
||||||
|
int create_work_threads_ex(int *count, void *(*start_func)(void *),
|
||||||
|
void *args, const int elment_size, pthread_t *tids,
|
||||||
|
const int stack_size);
|
||||||
|
|
||||||
int kill_work_threads(pthread_t *tids, const int count);
|
int kill_work_threads(pthread_t *tids, const int count);
|
||||||
|
|
||||||
|
int fc_create_thread(pthread_t *tid, void *(*start_func)(void *),
|
||||||
|
void *args, const int stack_size);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue