3
* @brief Implementation of the GEIS timer module.
5
/* Copyright (C) 2011-2012 Canonical Ltd
7
* This library is free software: you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License version 3
9
* as published by the Free Software Foundation.
11
* This library is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranties of
13
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
* PURPOSE. See the GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
19
#include "geis_config.h"
20
#include "geis_timer.h"
23
#include "geis_logging.h"
24
#include "geis_private.h"
27
#include <sys/timerfd.h>
30
static const int nanosecondsPerMillisecond = 1000000;
37
GeisTimerCallback callback;
43
* Bounces the multiplexor callback into the timer callback.
46
_timer_trampoline(int fd GEIS_UNUSED,
47
GeisBackendMultiplexorActivity ev GEIS_UNUSED,
50
GeisTimer timer = (GeisTimer)ctx;
52
timer->callback(timer, timer->context);
57
* Creates a new timer object on a GEIS API instance.
60
geis_timer_new(Geis geis, GeisTimerCallback callback, void *context)
62
GeisTimer timer = calloc(1, sizeof(struct GeisTimer));
65
geis_error("failed to allocate timer structure");
69
timer->fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
72
geis_error("error %d creating timerfd: %s", errno, strerror(errno));
76
timer->callback = callback;
77
timer->context = context;
78
geis_multiplex_fd(geis, timer->fd, GEIS_BE_MX_READ_AVAILABLE,
79
_timer_trampoline, timer);
91
* Destroys an existing timer.
94
geis_timer_delete(GeisTimer timer)
102
* Cancels an outstanding timer.
105
geis_timer_cancel(GeisTimer timer)
107
struct itimerspec in = { { 0, 0 }, { 0, 0 } };
108
struct itimerspec out;
109
int status = timerfd_settime(timer->fd, 0, &in, &out);
112
geis_error("error %d cancelling timerfd: %s", errno, strerror(errno));
118
* Starts a timer object a-ticking.
121
geis_timer_start(GeisTimer timer, GeisInteger msec)
123
struct itimerspec in = { { 0, 0 }, { 0, msec * nanosecondsPerMillisecond } };
124
struct itimerspec out;
125
int status = timerfd_settime(timer->fd, 0, &in, &out);
128
geis_error("error %d starting timerfd: %s", errno, strerror(errno));