From da24c698740c4240e3a7e55b1d342ad8f6026950 Mon Sep 17 00:00:00 2001 From: Bruce Sun Date: Wed, 11 Feb 2015 18:29:11 +0800 Subject: [PATCH] Bug 1130271: Call |add_fd_to_epoll_loop| directly in |add_timer|. Remove |remove_timer|. --- include/fdio/timer.h | 11 +++---- src/timer.c | 71 ++------------------------------------------ 2 files changed, 7 insertions(+), 75 deletions(-) diff --git a/include/fdio/timer.h b/include/fdio/timer.h index a1084c1..795d105 100644 --- a/include/fdio/timer.h +++ b/include/fdio/timer.h @@ -37,10 +37,10 @@ * an absolute time. All other parameters are the same as for * |add_relative_timer_to_epoll_loop|. * - * To remove an existing timer, call |remove_timer|. The parameter is - * a timer that has been returned by |add_timer_to_epoll_loop|. The - * function will remove the timer from the I/O loop and cleanup its - * resources. + * To remove an existing timer, call |remove_fd_from_epoll_loop|. The + * parameter is a timer that has been returned by |add_{relative,absolute}_ + * timer_to_epoll_loop|. The function will remove the timer from the I/O + * loop. File descriptors must be closed by callers. */ #include @@ -64,9 +64,6 @@ add_absolute_timer_to_epoll_loop(int clockid, enum ioresult (*func)(int, uint32_t, void*), void* data); -void -remove_timer(int timer); - #ifdef __cplusplus } #endif diff --git a/src/timer.c b/src/timer.c index 1076481..175435e 100644 --- a/src/timer.c +++ b/src/timer.c @@ -23,39 +23,6 @@ #include #include "log.h" -struct timer_param { - enum ioresult (*func)(int, uint32_t, void*); - void* data; -}; - -struct add_timer_param { - int fd; - struct timer_param param; -}; - -static enum ioresult -add_timer_cb(void* data) -{ - struct add_timer_param* param = data; - assert(param); - - add_fd_to_epoll_loop(param->fd, EPOLLIN | EPOLLERR, - param->param.func, param->param.data); - free(param); - - return IO_OK; -} - -static enum ioresult -remove_timer_cb(void* data) -{ - int fd = (int)data; - - remove_fd_from_epoll_loop(fd); - - return IO_OK; -} - static struct timespec* set_timespec(struct timespec* timespec, unsigned long long time_ms) @@ -79,7 +46,6 @@ add_timer(int clockid, { int fd, flags; struct itimerspec timeout; - struct add_timer_param* param; assert(timeout_ms); @@ -103,26 +69,12 @@ add_timer(int clockid, } /* install timerfd from within I/O loop */ - - errno = 0; - param = malloc(sizeof(*param)); - if (errno) { - ALOGE_ERRNO("malloc"); - goto err_malloc; - } - - param->fd = fd; - param->param.func = func; - param->param.data = data; - - if (run_task(add_timer_cb, param) < 0) - goto err_run_task; + if (add_fd_to_epoll_loop(fd, EPOLLIN | EPOLLERR, func, data) < 0) + goto err_add_fd_to_epoll_loop; return fd; -err_run_task: - free(param); -err_malloc: +err_add_fd_to_epoll_loop: err_timerfd_settime: if (TEMP_FAILURE_RETRY(close(fd)) < 0) ALOGW_ERRNO("close"); @@ -152,20 +104,3 @@ add_absolute_timer_to_epoll_loop(int clockid, return add_timer(clockid, 1, timeout_ms, interval_ms, func, data); } - -void -remove_timer(int timer) -{ - if (run_task(remove_timer_cb, (void*)timer) < 0) - goto err_run_task; - - return; - -err_run_task: - /* If anything goes wrong, we cleanup here and hope for the best. - */ - remove_fd_from_epoll_loop(timer); - if (TEMP_FAILURE_RETRY(close(timer)) < 0) - ALOGW_ERRNO("close"); - return; -}