sched_thread.c: task can execute in a new thread
parent
75198e5005
commit
90ea025cb5
3
HISTORY
3
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
|
Version 1.22 2015-10-10
|
||||||
* export php function: fastcommon_get_first_local_ip
|
* export php function: fastcommon_get_first_local_ip
|
||||||
* add function is_private_ip
|
* add function is_private_ip
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
%define LibFastcommonDevel libfastcommon-devel
|
%define LibFastcommonDevel libfastcommon-devel
|
||||||
|
|
||||||
Name: libfastcommon
|
Name: libfastcommon
|
||||||
Version: 1.0.22
|
Version: 1.0.23
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: c common functions library extracted from my open source projects FastDFS
|
Summary: c common functions library extracted from my open source projects FastDFS
|
||||||
License: GPL
|
License: GPL
|
||||||
|
|
|
||||||
|
|
@ -266,6 +266,29 @@ static int sched_check_waiting(ScheduleContext *pContext)
|
||||||
return 0;
|
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)
|
static void *sched_thread_entrance(void *args)
|
||||||
{
|
{
|
||||||
ScheduleContext *pContext;
|
ScheduleContext *pContext;
|
||||||
|
|
@ -319,7 +342,34 @@ static void *sched_thread_entrance(void *args)
|
||||||
&& pCurrent->next_call_time <= g_current_time))
|
&& pCurrent->next_call_time <= g_current_time))
|
||||||
{
|
{
|
||||||
//fprintf(stderr, "exec task id=%d\n", pCurrent->id);
|
//fprintf(stderr, "exec task id=%d\n", pCurrent->id);
|
||||||
|
if (!pCurrent->new_thread)
|
||||||
|
{
|
||||||
pCurrent->task_func(pCurrent->func_args);
|
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
|
do
|
||||||
{
|
{
|
||||||
pCurrent->next_call_time += pCurrent->interval;
|
pCurrent->next_call_time += pCurrent->interval;
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ typedef struct tagScheduleEntry
|
||||||
|
|
||||||
int interval; //the interval for execute task, unit is second
|
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
|
TaskFunc task_func; //callback function
|
||||||
void *func_args; //arguments pass to callback function
|
void *func_args; //arguments pass to callback function
|
||||||
|
|
||||||
|
|
@ -54,6 +58,7 @@ typedef struct
|
||||||
(schedule_entry).time_base.second = _second; \
|
(schedule_entry).time_base.second = _second; \
|
||||||
(schedule_entry).interval = _interval; \
|
(schedule_entry).interval = _interval; \
|
||||||
(schedule_entry).task_func = _task_func; \
|
(schedule_entry).task_func = _task_func; \
|
||||||
|
(schedule_entry).new_thread = false; \
|
||||||
(schedule_entry).func_args = _func_args
|
(schedule_entry).func_args = _func_args
|
||||||
|
|
||||||
#define INIT_SCHEDULE_ENTRY_EX(schedule_entry, _id, _time_base, \
|
#define INIT_SCHEDULE_ENTRY_EX(schedule_entry, _id, _time_base, \
|
||||||
|
|
@ -62,6 +67,7 @@ typedef struct
|
||||||
(schedule_entry).time_base = _time_base; \
|
(schedule_entry).time_base = _time_base; \
|
||||||
(schedule_entry).interval = _interval; \
|
(schedule_entry).interval = _interval; \
|
||||||
(schedule_entry).task_func = _task_func; \
|
(schedule_entry).task_func = _task_func; \
|
||||||
|
(schedule_entry).new_thread = false; \
|
||||||
(schedule_entry).func_args = _func_args
|
(schedule_entry).func_args = _func_args
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue