提交 7e1abd60 编写于 作者: Y yuqing

ioevent support set timeout

上级 b3e37bd5
......@@ -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;
}
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册