summaryrefslogtreecommitdiff
path: root/shared/n-acd/src/util/timer.c
blob: 07dbf34eb8845f4040d71da274b8f8776c0adc49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Timer Utility Library
 */

#include <assert.h>
#include <c-rbtree.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <time.h>
#include "timer.h"

int timer_init(Timer *timer) {
        clockid_t clock = CLOCK_BOOTTIME;
        int r;

        r = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
        if (r < 0 && errno == EINVAL) {
                clock = CLOCK_MONOTONIC;
                r = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
        }
        if (r < 0)
                return -errno;

        *timer = (Timer)TIMER_NULL(*timer);
        timer->fd = r;
        timer->clock = clock;

        return 0;
}

void timer_deinit(Timer *timer) {
        assert(c_rbtree_is_empty(&timer->tree));

        if (timer->fd >= 0) {
                close(timer->fd);
                timer->fd = -1;
        }
}

void timer_now(Timer *timer, uint64_t *nowp) {
        struct timespec ts;
        int r;

        r = clock_gettime(timer->clock, &ts);
        assert(r >= 0);
        (void)r;

        *nowp = ts.tv_sec * UINT64_C(1000000000) + ts.tv_nsec;
}

void timer_rearm(Timer *timer) {
        uint64_t time;
        Timeout *timeout;
        int r;

        /*
         * A timeout value of 0 clears the timer, we sholud only set that if
         * no timeout exists in the tree.
         */

        timeout = c_rbnode_entry(c_rbtree_first(&timer->tree), Timeout, node);
        assert(!timeout || timeout->timeout);

        time = timeout ? timeout->timeout : 0;

        if (time != timer->scheduled_timeout) {
                r = timerfd_settime(timer->fd,
                                    TFD_TIMER_ABSTIME,
                                    &(struct itimerspec){
                                            .it_value = {
                                                    .tv_sec = time / UINT64_C(1000000000),
                                                    .tv_nsec = time % UINT64_C(1000000000),
                                            },
                                    },
                                    NULL);
                assert(r >= 0);
                (void)r;

                timer->scheduled_timeout = time;
        }
}

int timer_read(Timer *timer) {
        uint64_t v;
        int r;

        r = read(timer->fd, &v, sizeof(v));
        if (r < 0) {
                if (errno == EAGAIN) {
                        /*
                         * No more pending events.
                         */
                        return 0;
                } else {
                        /*
                         * Something failed. We use CLOCK_BOOTTIME/MONOTONIC,
                         * so ECANCELED cannot happen. Hence, there is no
                         * error that we could gracefully handle. Fail hard
                         * and let the caller deal with it.
                         */
                        return -errno;
                }
        } else if (r != sizeof(v) || v == 0) {
                /*
                 * Kernel guarantees 8-byte reads, and only to return
                 * data if at least one timer triggered; fail hard if
                 * it suddenly starts doing weird shit.
                 */
                return -EIO;
        }

        return TIMER_E_TRIGGERED;
}


int timer_pop_timeout(Timer *timer, uint64_t until, Timeout **timeoutp) {
        Timeout *timeout;

        /*
         * If the first timeout is scheduled before @until, then unlink
         * it and return it. Otherwise, return NULL.
         */
        timeout = c_rbnode_entry(c_rbtree_first(&timer->tree), Timeout, node);
        if (timeout && timeout->timeout <= until) {
                c_rbnode_unlink(&timeout->node);
                timeout->timeout = 0;
                *timeoutp = timeout;
        } else {
                *timeoutp = NULL;
        }

        return 0;
}

void timeout_schedule(Timeout *timeout, Timer *timer, uint64_t time) {

        assert(time);

        /*
         * In case @timeout was already scheduled, remove it from the
         * tree. If we are moving it to a new timer, rearm the old one.
         */
        if (timeout->timer) {
                c_rbnode_unlink(&timeout->node);
                if (timeout->timer != timer)
                        timer_rearm(timeout->timer);
        }
        timeout->timer = timer;
        timeout->timeout = time;

        /*
         * Now insert it back into the tree in the correct new position.
         * We allow duplicates in the tree, so this insertion is open-coded.
         */
        {
                Timeout *other;
                CRBNode **slot, *parent;

                slot = &timer->tree.root;
                parent = NULL;
                while (*slot) {
                        other = c_rbnode_entry(*slot, Timeout, node);
                        parent = *slot;
                        if (timeout->timeout < other->timeout)
                                slot = &(*slot)->left;
                        else
                                slot = &(*slot)->right;
                }

                c_rbtree_add(&timer->tree, parent, slot, &timeout->node);
        }

        /*
         * Rearm the timer as we updated the timeout tree.
         */
        timer_rearm(timer);
}

void timeout_unschedule(Timeout *timeout) {
        Timer *timer = timeout->timer;

        if (!timer)
                return;

        c_rbnode_unlink(&timeout->node);
        timeout->timeout = 0;
        timeout->timer = NULL;

        timer_rearm(timer);
}