add function ioevent_remove

pull/2/head
yuqing 2014-08-25 09:24:52 +08:00
parent ed4a70d97b
commit 118f657c7f
5 changed files with 68 additions and 12 deletions

View File

@ -1,5 +1,5 @@
Version 1.06 2014-08-11 Version 1.06 2014-08-25
* update source code from FastDFS V5.02 * update source code from FastDFS V5.02
* add function short2buff and buff2short * add function short2buff and buff2short
* add object memory pool (fast_mblock.h and fast_mblock.c) * add object memory pool (fast_mblock.h and fast_mblock.c)
@ -22,6 +22,7 @@ Version 1.06 2014-08-11
* support OS Darwin * support OS Darwin
* socket send and recv ignore erno EINTR * socket send and recv ignore erno EINTR
* http_parse_query_ex support binary buffer, and set both lengths of key and name * 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 Version 1.05 2012-07-08
* update source code from FastDFS V3.09 * update source code from FastDFS V3.09

View File

@ -36,6 +36,8 @@ int ioevent_init(IOEventPoller *ioevent, const int size,
ioevent->size = size; ioevent->size = size;
ioevent->extra_events = extra_events; ioevent->extra_events = extra_events;
ioevent->iterator.index = 0;
ioevent->iterator.count = 0;
#if IOEVENT_USE_EPOLL #if IOEVENT_USE_EPOLL
ioevent->timeout = timeout; ioevent->timeout = timeout;

View File

@ -52,6 +52,11 @@ typedef struct ioevent_puller {
int extra_events; int extra_events;
int poll_fd; int poll_fd;
struct {
int index;
int count;
} iterator; //for deal event loop
#if IOEVENT_USE_EPOLL #if IOEVENT_USE_EPOLL
struct epoll_event *events; struct epoll_event *events;
int timeout; int timeout;
@ -90,6 +95,19 @@ typedef struct ioevent_puller {
#error port me #error port me
#endif #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 #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -2,18 +2,50 @@
#include "logger.h" #include "logger.h"
#include "ioevent_loop.h" #include "ioevent_loop.h"
static void deal_ioevents(IOEventPoller *ioevent, const int count) static void deal_ioevents(IOEventPoller *ioevent)
{ {
int i;
int event; int event;
IOEventEntry *pEntry; IOEventEntry *pEntry;
for (i=0; i<count; i++)
{
event = IOEVENT_GET_EVENTS(ioevent, i);
pEntry = (IOEventEntry *)IOEVENT_GET_DATA(ioevent, i);
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); 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) static void deal_timeouts(FastTimerEntry *head)
@ -67,12 +99,12 @@ int ioevent_loop(struct nio_thread_data *pThreadData,
while (*continue_flag) while (*continue_flag)
{ {
pThreadData->deleted_list = NULL; pThreadData->deleted_list = NULL;
count = ioevent_poll(&pThreadData->ev_puller); pThreadData->ev_puller.iterator.count = ioevent_poll(&pThreadData->ev_puller);
if (count > 0) 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; result = errno != 0 ? errno : EINVAL;
if (result != EINTR) if (result != EINTR)

View File

@ -11,6 +11,9 @@ 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);
//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 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);