libuev 2.4.1
cron.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 <string.h> /* memset() */
27#include <sys/timerfd.h>
28#include <unistd.h> /* close(), read() */
29
30#include "uev.h"
31
32/* Missing defines in GLIBC <= 2.24 */
33#ifndef TFD_TIMER_CANCEL_ON_SET
34#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
35#endif
36#ifndef TFD_SETTIME_FLAGS
37#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)
38#endif
39
44
66int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when, time_t interval)
67{
68 int fd;
69
70 if (when < 0 || interval < 0) {
71 errno = ERANGE;
72 return -1;
73 }
74
75 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
76 if (fd < 0)
77 return -1;
78
79 if (_uev_watcher_init(ctx, w, UEV_CRON_TYPE, cb, arg, fd, UEV_READ))
80 goto exit;
81
82 if (uev_cron_set(w, when, interval)) {
83 _uev_watcher_stop(w);
84 exit:
85 close(fd);
86 return -1;
87 }
88
89 return 0;
90}
91
100int uev_cron_set(uev_t *w, time_t when, time_t interval)
101{
102 /* Every watcher must be registered to a context */
103 if (!w || !w->ctx) {
104 errno = EINVAL;
105 return -1;
106 }
107
108 if (when < 0 || interval < 0) {
109 errno = ERANGE;
110 return -1;
111 }
112
113 /* Handle stopped timers */
114 if (w->fd < 0) {
115 /* Timer already stopped */
116 if (!when && !interval)
117 return 0;
118
119 if (uev_cron_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, when, interval))
120 return -1;
121 }
122
123 w->u.c.when = when;
124 w->u.c.interval = interval;
125
126 if (w->ctx->running) {
127 struct itimerspec time;
128
129 memset(&time, 0, sizeof(time));
130 time.it_value.tv_sec = when;
131 time.it_interval.tv_sec = interval;
132 if (timerfd_settime(w->fd, TFD_SETTIME_FLAGS, &time, NULL) < 0)
133 return 1;
134 }
135
136 return _uev_watcher_start(w);
137}
138
146{
147 return uev_timer_start(w);
148}
149
157{
158 return uev_timer_stop(w);
159}
160
int uev_cron_set(uev_t *w, time_t when, time_t interval)
Definition cron.c:100
int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when, time_t interval)
Definition cron.c:66
int uev_cron_stop(uev_t *w)
Definition cron.c:156
int uev_cron_start(uev_t *w)
Definition cron.c:145
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
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