ioevent support set timeout

pull/4/head
yuqing 2015-06-11 09:49:04 +08:00
parent b3e37bd584
commit 7e1abd6054
2 changed files with 21 additions and 7 deletions

View File

@ -30,7 +30,7 @@ int kqueue_ev_convert(int16_t event, uint16_t flags)
#endif
int ioevent_init(IOEventPoller *ioevent, const int size,
const int timeout, const int extra_events)
const int timeout_ms, const int extra_events)
{
int bytes;
@ -40,26 +40,24 @@ int ioevent_init(IOEventPoller *ioevent, const int size,
ioevent->iterator.count = 0;
#if IOEVENT_USE_EPOLL
ioevent->timeout = timeout;
ioevent->poll_fd = epoll_create(ioevent->size);
bytes = sizeof(struct epoll_event) * size;
ioevent->events = (struct epoll_event *)malloc(bytes);
#elif IOEVENT_USE_KQUEUE
ioevent->timeout.tv_sec = timeout / 1000;
ioevent->timeout.tv_nsec = 1000000 * (timeout % 1000);
ioevent->poll_fd = kqueue();
bytes = sizeof(struct kevent) * size;
ioevent->events = (struct kevent *)malloc(bytes);
#elif IOEVENT_USE_PORT
ioevent->timeout.tv_sec = timeout / 1000;
ioevent->timeout.tv_nsec = 1000000 * (timeout % 1000);
ioevent->poll_fd = port_create();
bytes = sizeof(port_event_t) * size;
ioevent->events = (port_event_t *)malloc(bytes);
#endif
if (ioevent->events == NULL) {
return errno != 0 ? errno : ENOMEM;
}
ioevent_set_timeout(ioevent, timeout_ms);
return 0;
}

View File

@ -113,7 +113,7 @@ extern "C" {
#endif
int ioevent_init(IOEventPoller *ioevent, const int size,
const int timeout, const int extra_events);
const int timeout_ms, const int extra_events);
void ioevent_destroy(IOEventPoller *ioevent);
int ioevent_attach(IOEventPoller *ioevent, const int fd, const int e,
@ -123,6 +123,22 @@ int ioevent_modify(IOEventPoller *ioevent, const int fd, const int e,
int ioevent_detach(IOEventPoller *ioevent, const int fd);
int ioevent_poll(IOEventPoller *ioevent);
static inline void ioevent_set_timeout(IOEventPoller *ioevent, const int timeout_ms)
{
#if IOEVENT_USE_EPOLL
ioevent->timeout = timeout_ms;
#else
ioevent->timeout.tv_sec = timeout_ms / 1000;
ioevent->timeout.tv_nsec = 1000000 * (timeout_ms % 1000);
#endif
}
static inline int ioevent_poll_ex(IOEventPoller *ioevent, const int timeout_ms)
{
ioevent_set_timeout(ioevent, timeout_ms);
return ioevent_poll(ioevent);
}
#ifdef __cplusplus
}
#endif