libuev 2.4.1
timer.c
Go to the documentation of this file.
1/* libuEv - Micro event loop library
2 *
3 * Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
4 * Copyright (c) 2013-2024 Joachim Wiberg <troglobit()gmail!com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include <errno.h>
26#include <sys/timerfd.h>
27#include <unistd.h> /* close(), read() */
28
29#include "uev.h"
30
35
36
37static void msec2tspec(int msec, struct timespec *ts)
38{
39 if (msec) {
40 ts->tv_sec = msec / 1000;
41 ts->tv_nsec = (msec % 1000) * 1000000;
42 } else {
43 ts->tv_sec = 0;
44 ts->tv_nsec = 0;
45 }
46}
47
77int uev_timer_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period)
78{
79 int fd;
80
81 if (timeout < 0 || period < 0) {
82 errno = ERANGE;
83 return -1;
84 }
85
86 fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
87 if (fd < 0)
88 return -1;
89
90 if (_uev_watcher_init(ctx, w, UEV_TIMER_TYPE, cb, arg, fd, UEV_READ))
91 goto exit;
92
93 if (uev_timer_set(w, timeout, period)) {
94 _uev_watcher_stop(w);
95 exit:
96 close(fd);
97 w->fd = -1;
98 return -1;
99 }
100
101 return 0;
102}
103
116int uev_timer_set(uev_t *w, int timeout, int period)
117{
118 /* Every watcher must be registered to a context */
119 if (!w || !w->ctx) {
120 errno = EINVAL;
121 return -1;
122 }
123
124 if (timeout < 0 || period < 0) {
125 errno = ERANGE;
126 return -1;
127 }
128
129 /* Handle stopped timers */
130 if (w->fd < 0) {
131 /* Timer already stopped */
132 if (!timeout && !period)
133 return 0;
134
135 if (uev_timer_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, timeout, period))
136 return -1;
137 }
138
139 w->u.t.timeout = timeout;
140 w->u.t.period = period;
141
142 if (w->ctx->running) {
143 struct itimerspec time;
144
145 msec2tspec(timeout, &time.it_value);
146 msec2tspec(period, &time.it_interval);
147 if (timerfd_settime(w->fd, 0, &time, NULL) < 0)
148 return 1;
149 }
150
151 return _uev_watcher_start(w);
152}
153
161{
162 if (!w) {
163 errno = EINVAL;
164 return -1;
165 }
166
167 if (-1 != w->fd)
168 _uev_watcher_stop(w);
169
170 return uev_timer_set(w, w->u.t.timeout, w->u.t.period);
171}
172
180{
181 if (!_uev_watcher_active(w))
182 return 0;
183
184 if (_uev_watcher_stop(w))
185 return -1;
186
187 close(w->fd);
188 w->fd = -1;
189
190 return 0;
191}
192
int fd
Definition uev.h:79
uev_ctx_t * ctx
Definition uev.h:80
int uev_timer_start(uev_t *w)
Definition timer.c:160
int uev_timer_stop(uev_t *w)
Definition timer.c:179
int uev_timer_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period)
Definition timer.c:77
int uev_timer_set(uev_t *w, int timeout, int period)
Definition timer.c:116
struct uev_ctx uev_ctx_t
Definition uev.h:70
#define UEV_READ
Definition uev.h:46
void uev_cb_t(uev_t *w, void *arg, int events)
Definition uev.h:97
struct uev uev_t