Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 1130271: Call |add_fd_to_epoll_loop| directly in |add_timer|. Rem…
Browse files Browse the repository at this point in the history
…ove |remove_timer|.
  • Loading branch information
BruceSunMozilla authored and rvandermeulen committed Feb 20, 2015
1 parent 20c8dbc commit da24c69
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 75 deletions.
11 changes: 4 additions & 7 deletions include/fdio/timer.h
Expand Up @@ -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 <stdint.h>
Expand All @@ -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
71 changes: 3 additions & 68 deletions src/timer.c
Expand Up @@ -23,39 +23,6 @@
#include <fdio/timer.h>
#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)
Expand All @@ -79,7 +46,6 @@ add_timer(int clockid,
{
int fd, flags;
struct itimerspec timeout;
struct add_timer_param* param;

assert(timeout_ms);

Expand All @@ -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");
Expand Down Expand Up @@ -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;
}

0 comments on commit da24c69

Please sign in to comment.