From 118f657c7fe25c3e33c5e18331545c5650367e7d Mon Sep 17 00:00:00 2001 From: yuqing Date: Mon, 25 Aug 2014 09:24:52 +0800 Subject: [PATCH] add function ioevent_remove --- HISTORY | 3 ++- src/ioevent.c | 2 ++ src/ioevent.h | 18 ++++++++++++++++ src/ioevent_loop.c | 54 ++++++++++++++++++++++++++++++++++++---------- src/ioevent_loop.h | 3 +++ 5 files changed, 68 insertions(+), 12 deletions(-) diff --git a/HISTORY b/HISTORY index 951a57d..7823461 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.06 2014-08-11 +Version 1.06 2014-08-25 * update source code from FastDFS V5.02 * add function short2buff and buff2short * add object memory pool (fast_mblock.h and fast_mblock.c) @@ -22,6 +22,7 @@ Version 1.06 2014-08-11 * support OS Darwin * socket send and recv ignore erno EINTR * http_parse_query_ex support binary buffer, and set both lengths of key and name + * add function ioevent_remove Version 1.05 2012-07-08 * update source code from FastDFS V3.09 diff --git a/src/ioevent.c b/src/ioevent.c index 9ff0658..c75b123 100644 --- a/src/ioevent.c +++ b/src/ioevent.c @@ -36,6 +36,8 @@ int ioevent_init(IOEventPoller *ioevent, const int size, ioevent->size = size; ioevent->extra_events = extra_events; + ioevent->iterator.index = 0; + ioevent->iterator.count = 0; #if IOEVENT_USE_EPOLL ioevent->timeout = timeout; diff --git a/src/ioevent.h b/src/ioevent.h index 79e2c5b..0bc0ed9 100644 --- a/src/ioevent.h +++ b/src/ioevent.h @@ -52,6 +52,11 @@ typedef struct ioevent_puller { int extra_events; int poll_fd; + struct { + int index; + int count; + } iterator; //for deal event loop + #if IOEVENT_USE_EPOLL struct epoll_event *events; int timeout; @@ -90,6 +95,19 @@ typedef struct ioevent_puller { #error port me #endif +#if IOEVENT_USE_EPOLL + #define IOEVENT_CLEAR_DATA(ioevent, index) \ + ioevent->events[index].data.ptr = NULL +#elif IOEVENT_USE_KQUEUE + #define IOEVENT_CLEAR_DATA(ioevent, index) \ + ioevent->events[index].udata = NULL +#elif IOEVENT_USE_PORT + #define IOEVENT_CLEAR_DATA(ioevent, index) \ + ioevent->events[index].portev_user = NULL +#else +#error port me +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/src/ioevent_loop.c b/src/ioevent_loop.c index f09411b..4c25225 100644 --- a/src/ioevent_loop.c +++ b/src/ioevent_loop.c @@ -2,20 +2,52 @@ #include "logger.h" #include "ioevent_loop.h" -static void deal_ioevents(IOEventPoller *ioevent, const int count) +static void deal_ioevents(IOEventPoller *ioevent) { - int i; int event; IOEventEntry *pEntry; - for (i=0; icallback(pEntry->fd, event, pEntry->timer.data); + for (ioevent->iterator.index=0; ioevent->iterator.index < ioevent->iterator. + count; ioevent->iterator.index++) + { + event = IOEVENT_GET_EVENTS(ioevent, ioevent->iterator.index); + pEntry = (IOEventEntry *)IOEVENT_GET_DATA(ioevent, + ioevent->iterator.index); + if (pEntry != NULL) { + pEntry->callback(pEntry->fd, event, pEntry->timer.data); + } } } +int ioevent_remove(IOEventPoller *ioevent, void *data) +{ + IOEventEntry *pEntry; + int index; + + if (ioevent->iterator.index >= ioevent->iterator.count) + { + return ENOENT; + } + + pEntry = (IOEventEntry *)IOEVENT_GET_DATA(ioevent, + ioevent->iterator.index); + if (pEntry != NULL && pEntry->timer.data == data) { + return 0; //do NOT clear current entry + } + + for (index=ioevent->iterator.index + 1; index < ioevent->iterator.count; + index++) + { + pEntry = (IOEventEntry *)IOEVENT_GET_DATA(ioevent, index); + if (pEntry != NULL && pEntry->timer.data == data) { + IOEVENT_CLEAR_DATA(ioevent, index); + return 0; + } + } + + return ENOENT; +} + static void deal_timeouts(FastTimerEntry *head) { FastTimerEntry *entry; @@ -67,12 +99,12 @@ int ioevent_loop(struct nio_thread_data *pThreadData, while (*continue_flag) { pThreadData->deleted_list = NULL; - count = ioevent_poll(&pThreadData->ev_puller); - if (count > 0) + pThreadData->ev_puller.iterator.count = ioevent_poll(&pThreadData->ev_puller); + if (pThreadData->ev_puller.iterator.count > 0) { - deal_ioevents(&pThreadData->ev_puller, count); + deal_ioevents(&pThreadData->ev_puller); } - else if (count < 0) + else if (pThreadData->ev_puller.iterator.count < 0) { result = errno != 0 ? errno : EINVAL; if (result != EINTR) diff --git a/src/ioevent_loop.h b/src/ioevent_loop.h index f4ea160..7740fc7 100644 --- a/src/ioevent_loop.h +++ b/src/ioevent_loop.h @@ -11,6 +11,9 @@ int ioevent_loop(struct nio_thread_data *pThreadData, IOEventCallback recv_notify_callback, TaskCleanUpCallback clean_up_callback, volatile bool *continue_flag); +//remove entry from ready list +int ioevent_remove(IOEventPoller *ioevent, void *data); + int ioevent_set(struct fast_task_info *pTask, struct nio_thread_data *pThread, int sock, short event, IOEventCallback callback, const int timeout);