diff --git a/HISTORY b/HISTORY index 29bfed8..4de0c13 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-03-09 +Version 1.44 2020-03-10 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -18,6 +18,7 @@ Version 1.44 2020-03-09 * struct fast_task_info add ctx pointer for libserverframe nio * 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 Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/fast_task_queue.h b/src/fast_task_queue.h index 469be34..01508fb 100644 --- a/src/fast_task_queue.h +++ b/src/fast_task_queue.h @@ -19,6 +19,9 @@ #include "ioevent.h" #include "fast_timer.h" +#define FC_NOTIFY_READ_FD(tdata) (tdata)->pipe_fds[0] +#define FC_NOTIFY_WRITE_FD(tdata) (tdata)->pipe_fds[1] + #define ALIGNED_TASK_INFO_SIZE MEM_ALIGN(sizeof(struct fast_task_info)) struct nio_thread_data; @@ -51,7 +54,12 @@ struct nio_thread_data struct fast_task_info *head; struct fast_task_info *tail; pthread_mutex_t lock; - } waiting_queue; + } waiting_queue; //task queue + + struct { + bool enabled; + volatile int64_t counter; + } notify; //for thread notify }; struct fast_task_info diff --git a/src/ioevent_loop.c b/src/ioevent_loop.c index 1081c0c..7293031 100644 --- a/src/ioevent_loop.c +++ b/src/ioevent_loop.c @@ -152,6 +152,14 @@ int ioevent_loop(struct nio_thread_data *pThreadData, } } + if (pThreadData->notify.enabled) { + int64_t n; + if ((n=__sync_fetch_and_add(&pThreadData->notify.counter, 0)) != 0) { + logInfo("file: "__FILE__", line: %d, " + "n ==== %"PRId64, __LINE__, n); + __sync_fetch_and_sub(&pThreadData->notify.counter, n); + } + } if (pThreadData->thread_loop_callback != NULL) { pThreadData->thread_loop_callback(pThreadData); } diff --git a/src/ioevent_loop.h b/src/ioevent_loop.h index 0eefbc5..3f1a0a0 100644 --- a/src/ioevent_loop.h +++ b/src/ioevent_loop.h @@ -35,6 +35,28 @@ static inline void iovent_add_to_deleted_list(struct fast_task_info *task) task->thread_data->deleted_list = task; } +static inline int iovent_notify_thread(struct nio_thread_data *thread_data) +{ + int64_t n; + int result; + + if (__sync_fetch_and_add(&thread_data->notify.counter, 1) == 0) + { + n = 1; + if (write(FC_NOTIFY_WRITE_FD(thread_data), &n, sizeof(n)) != sizeof(n)) + { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "write to fd %d fail, errno: %d, error info: %s", + __LINE__, FC_NOTIFY_WRITE_FD(thread_data), + result, STRERROR(result)); + return result; + } + } + + return 0; +} + #ifdef __cplusplus } #endif diff --git a/src/sched_thread.c b/src/sched_thread.c index df068af..f86fafe 100644 --- a/src/sched_thread.c +++ b/src/sched_thread.c @@ -438,7 +438,7 @@ static void *sched_thread_entrance(void *args) exec_count = 0; pCurrent = pContext->head; - while (*(pContext->pcontinue_flag) && (pCurrent != NULL \ + while (*(pContext->pcontinue_flag) && (pCurrent != NULL && pCurrent->next_call_time <= g_current_time)) { //logInfo("exec task id: %d", pCurrent->id);