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

Bug 1130271: Call |add_fd_to_epoll_loop| directly in |add_timer|. Remove |remove_timer|. #4

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

#endif
70 changes: 3 additions & 67 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 Down Expand Up @@ -103,26 +70,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 +105,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;
}