Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'uloop-epoll.c')
-rw-r--r--uloop-epoll.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/uloop-epoll.c b/uloop-epoll.c
index 70e45e4..cf5733f 100644
--- a/uloop-epoll.c
+++ b/uloop-epoll.c
@@ -104,3 +104,83 @@ static int uloop_fetch_events(int timeout)
return nfds;
}
+
+static void dispatch_timer(struct uloop_fd *u, unsigned int events)
+{
+ if (!(events & ULOOP_READ))
+ return;
+
+ uint64_t fired;
+
+ if (read(u->fd, &fired, sizeof(fired)) != sizeof(fired))
+ return;
+
+ struct uloop_interval *tm = container_of(u, struct uloop_interval, private.ufd);
+
+ tm->expirations += fired;
+ tm->cb(tm);
+}
+
+static int timer_register(struct uloop_interval *tm, unsigned int msecs)
+{
+ if (!tm->private.ufd.registered) {
+ int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC|TFD_NONBLOCK);
+
+ if (fd == -1)
+ return -1;
+
+ tm->private.ufd.fd = fd;
+ tm->private.ufd.cb = dispatch_timer;
+ }
+
+ struct itimerspec spec = {
+ .it_value = {
+ .tv_sec = msecs / 1000,
+ .tv_nsec = (msecs % 1000) * 1000000
+ },
+ .it_interval = {
+ .tv_sec = msecs / 1000,
+ .tv_nsec = (msecs % 1000) * 1000000
+ }
+ };
+
+ if (timerfd_settime(tm->private.ufd.fd, 0, &spec, NULL) == -1)
+ goto err;
+
+ if (uloop_fd_add(&tm->private.ufd, ULOOP_READ) == -1)
+ goto err;
+
+ return 0;
+
+err:
+ uloop_fd_delete(&tm->private.ufd);
+ close(tm->private.ufd.fd);
+ memset(&tm->private.ufd, 0, sizeof(tm->private.ufd));
+
+ return -1;
+}
+
+static int timer_remove(struct uloop_interval *tm)
+{
+ int ret = __uloop_fd_delete(&tm->private.ufd);
+
+ if (ret == 0) {
+ close(tm->private.ufd.fd);
+ memset(&tm->private.ufd, 0, sizeof(tm->private.ufd));
+ }
+
+ return ret;
+}
+
+static int64_t timer_next(struct uloop_interval *tm)
+{
+ struct itimerspec spec;
+
+ if (!tm->private.ufd.registered)
+ return -1;
+
+ if (timerfd_gettime(tm->private.ufd.fd, &spec) == -1)
+ return -1;
+
+ return spec.it_value.tv_sec * 1000 + spec.it_value.tv_nsec / 1000000;
+}