add canceled field for complicated thread model

pull/37/head
YuQing 2020-03-09 10:51:46 +08:00
parent b88d5b03fe
commit f9881d96b7
5 changed files with 36 additions and 26 deletions

2
.gitignore vendored
View File

@ -48,9 +48,11 @@ src/tests/test_pthread_lock
src/tests/test_split_string src/tests/test_split_string
src/tests/test_uniq_skiplist src/tests/test_uniq_skiplist
src/tests/test_server_id_func src/tests/test_server_id_func
src/tests/test_pipe
# other # other
*.swp *.swp
*.swo
php-fastcommon/.deps php-fastcommon/.deps
php-fastcommon/.libs/ php-fastcommon/.libs/

View File

@ -1,5 +1,5 @@
Version 1.44 2020-03-08 Version 1.44 2020-03-09
* 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
@ -17,6 +17,7 @@ Version 1.44 2020-03-08
* char_converter.[hc]: add function fast_char_unescape * char_converter.[hc]: add function fast_char_unescape
* struct fast_task_info add ctx pointer for libserverframe nio * struct fast_task_info add ctx pointer for libserverframe nio
* 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
Version 1.43 2019-12-25 Version 1.43 2019-12-25
* replace function call system to getExecResult, * replace function call system to getExecResult,

View File

@ -44,7 +44,7 @@ struct nio_thread_data
struct ioevent_puller ev_puller; struct ioevent_puller ev_puller;
struct fast_timer timer; struct fast_timer timer;
int pipe_fds[2]; //for notify int pipe_fds[2]; //for notify
struct fast_task_info *deleted_list; struct fast_task_info *deleted_list; //tasks for cleanup
ThreadLoopCallback thread_loop_callback; ThreadLoopCallback thread_loop_callback;
void *arg; //extra argument pointer void *arg; //extra argument pointer
struct { struct {
@ -64,6 +64,7 @@ struct fast_task_info
int length; //data length int length; //data length
int offset; //current offset int offset; //current offset
char nio_stage; //stage for network IO char nio_stage; //stage for network IO
bool canceled; //if task canceled
int64_t req_count; //request count int64_t req_count; //request count
TaskFinishCallback finish_callback; TaskFinishCallback finish_callback;
struct nio_thread_data *thread_data; struct nio_thread_data *thread_data;

View File

@ -77,16 +77,6 @@ static void deal_timeouts(FastTimerEntry *head)
} }
} }
void iovent_add_to_deleted_list(struct fast_task_info *pTask)
{
if (pTask->thread_data == NULL) {
return;
}
pTask->next = pTask->thread_data->deleted_list;
pTask->thread_data->deleted_list = pTask;
}
int ioevent_loop(struct nio_thread_data *pThreadData, int ioevent_loop(struct nio_thread_data *pThreadData,
IOEventCallback recv_notify_callback, TaskCleanUpCallback IOEventCallback recv_notify_callback, TaskCleanUpCallback
clean_up_callback, volatile bool *continue_flag) clean_up_callback, volatile bool *continue_flag)
@ -94,7 +84,7 @@ int ioevent_loop(struct nio_thread_data *pThreadData,
int result; int result;
IOEventEntry ev_notify; IOEventEntry ev_notify;
FastTimerEntry head; FastTimerEntry head;
struct fast_task_info *pTask; struct fast_task_info *task;
time_t last_check_time; time_t last_check_time;
int count; int count;
@ -142,13 +132,13 @@ int ioevent_loop(struct nio_thread_data *pThreadData,
count = 0; count = 0;
while (pThreadData->deleted_list != NULL) while (pThreadData->deleted_list != NULL)
{ {
pTask = pThreadData->deleted_list; task = pThreadData->deleted_list;
pThreadData->deleted_list = pTask->next; pThreadData->deleted_list = task->next;
clean_up_callback(pTask); clean_up_callback(task);
count++; count++;
} }
logDebug("cleanup task count: %d", count); //logInfo("cleanup task count: %d", count);
} }
if (g_current_time - last_check_time > 0) if (g_current_time - last_check_time > 0)
@ -170,16 +160,16 @@ int ioevent_loop(struct nio_thread_data *pThreadData,
return 0; return 0;
} }
int ioevent_set(struct fast_task_info *pTask, struct nio_thread_data *pThread, int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread,
int sock, short event, IOEventCallback callback, const int timeout) int sock, short event, IOEventCallback callback, const int timeout)
{ {
int result; int result;
pTask->thread_data = pThread; task->thread_data = pThread;
pTask->event.fd = sock; task->event.fd = sock;
pTask->event.callback = callback; task->event.callback = callback;
if (ioevent_attach(&pThread->ev_puller, if (ioevent_attach(&pThread->ev_puller,
sock, event, pTask) < 0) sock, event, task) < 0)
{ {
result = errno != 0 ? errno : ENOENT; result = errno != 0 ? errno : ENOENT;
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
@ -189,9 +179,9 @@ int ioevent_set(struct fast_task_info *pTask, struct nio_thread_data *pThread,
return result; return result;
} }
pTask->event.timer.data = pTask; task->event.timer.data = task;
pTask->event.timer.expires = g_current_time + timeout; task->event.timer.expires = g_current_time + timeout;
result = fast_timer_add(&pThread->timer, &pTask->event.timer); result = fast_timer_add(&pThread->timer, &task->event.timer);
if (result != 0) if (result != 0)
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \

View File

@ -17,7 +17,23 @@ int ioevent_remove(IOEventPoller *ioevent, void *data);
int ioevent_set(struct fast_task_info *pTask, struct nio_thread_data *pThread, int ioevent_set(struct fast_task_info *pTask, struct nio_thread_data *pThread,
int sock, short event, IOEventCallback callback, const int timeout); int sock, short event, IOEventCallback callback, const int timeout);
void iovent_add_to_deleted_list(struct fast_task_info *pTask); static inline void iovent_add_to_deleted_list(struct fast_task_info *task)
{
if (task->thread_data == NULL)
{
return;
}
if (task->canceled) {
logError("file: "__FILE__", line: %d, "
"task %p already canceled", __LINE__, task);
return;
}
task->canceled = true;
task->next = task->thread_data->deleted_list;
task->thread_data->deleted_list = task;
}
#ifdef __cplusplus #ifdef __cplusplus
} }