From 90ea025cb5e59e26ea2b65ba35540abbc5051641 Mon Sep 17 00:00:00 2001 From: yuqing Date: Fri, 16 Oct 2015 10:43:02 +0800 Subject: [PATCH] sched_thread.c: task can execute in a new thread --- HISTORY | 3 +++ libfastcommon.spec | 2 +- src/sched_thread.c | 52 +++++++++++++++++++++++++++++++++++++++++++++- src/sched_thread.h | 6 ++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/HISTORY b/HISTORY index dc410f8..828e872 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.23 2015-10-16 + * sched_thread.c: task can execute in a new thread + Version 1.22 2015-10-10 * export php function: fastcommon_get_first_local_ip * add function is_private_ip diff --git a/libfastcommon.spec b/libfastcommon.spec index 085ad5c..cf5cafa 100644 --- a/libfastcommon.spec +++ b/libfastcommon.spec @@ -2,7 +2,7 @@ %define LibFastcommonDevel libfastcommon-devel Name: libfastcommon -Version: 1.0.22 +Version: 1.0.23 Release: 1%{?dist} Summary: c common functions library extracted from my open source projects FastDFS License: GPL diff --git a/src/sched_thread.c b/src/sched_thread.c index 0c668ea..393a6fc 100644 --- a/src/sched_thread.c +++ b/src/sched_thread.c @@ -266,6 +266,29 @@ static int sched_check_waiting(ScheduleContext *pContext) return 0; } +static void *sched_call_func(void *args) +{ + ScheduleEntry *pEntry; + void *func_args; + TaskFunc task_func; + int task_id; + + pEntry = (ScheduleEntry *)args; + task_func = pEntry->task_func; + func_args = pEntry->func_args; + task_id = pEntry->id; + + logDebug("file: "__FILE__", line: %d, " \ + "thread enter, task id: %d", __LINE__, task_id); + + pEntry->thread_running = true; + task_func(func_args); + + logDebug("file: "__FILE__", line: %d, " \ + "thread exit, task id: %d", __LINE__, task_id); + return NULL; +} + static void *sched_thread_entrance(void *args) { ScheduleContext *pContext; @@ -319,7 +342,34 @@ static void *sched_thread_entrance(void *args) && pCurrent->next_call_time <= g_current_time)) { //fprintf(stderr, "exec task id=%d\n", pCurrent->id); - pCurrent->task_func(pCurrent->func_args); + if (!pCurrent->new_thread) + { + pCurrent->task_func(pCurrent->func_args); + } + else + { + pthread_t tid; + int result; + + pCurrent->thread_running = false; + if ((result=pthread_create(&tid, NULL, + sched_call_func, pCurrent)) != 0) + { + logError("file: "__FILE__", line: %d, " \ + "create thread failed, " \ + "errno: %d, error info: %s", \ + __LINE__, result, STRERROR(result)); + } + usleep(1*1000); + for (i=1; !pCurrent->thread_running && i<100; i++) + { + logDebug("file: "__FILE__", line: %d, " + "task_id: %d, waiting thread ready, count %d", + __LINE__, pCurrent->id, i); + usleep(1*1000); + } + } + do { pCurrent->next_call_time += pCurrent->interval; diff --git a/src/sched_thread.h b/src/sched_thread.h index d67fe40..72d9048 100644 --- a/src/sched_thread.h +++ b/src/sched_thread.h @@ -24,6 +24,10 @@ typedef struct tagScheduleEntry int interval; //the interval for execute task, unit is second + bool new_thread; //run in a new thread + + bool thread_running; //if new thread running, for internal use + TaskFunc task_func; //callback function void *func_args; //arguments pass to callback function @@ -54,6 +58,7 @@ typedef struct (schedule_entry).time_base.second = _second; \ (schedule_entry).interval = _interval; \ (schedule_entry).task_func = _task_func; \ + (schedule_entry).new_thread = false; \ (schedule_entry).func_args = _func_args #define INIT_SCHEDULE_ENTRY_EX(schedule_entry, _id, _time_base, \ @@ -62,6 +67,7 @@ typedef struct (schedule_entry).time_base = _time_base; \ (schedule_entry).interval = _interval; \ (schedule_entry).task_func = _task_func; \ + (schedule_entry).new_thread = false; \ (schedule_entry).func_args = _func_args #ifdef __cplusplus