diff --git a/src/ioevent.c b/src/ioevent.c index 77eab332c932533294af48cbccfddd0b7a4ccac3..7454b7cbcec8d18e9fa4df0cbc38324a4ccdc8b6 100644 --- a/src/ioevent.c +++ b/src/ioevent.c @@ -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; } diff --git a/src/ioevent.h b/src/ioevent.h index 8aa79b70a9a9e6dacfefbcf9ce43bb61371ee30e..5d4630efed5a71c73eab52ebb44cbbfcfe4446f3 100644 --- a/src/ioevent.h +++ b/src/ioevent.h @@ -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