summaryrefslogtreecommitdiffstats
path: root/backend/timer.c
blob: 050419681c1b95f8c133692c208cddf9d53f0c3c (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
192
/*
 *   Copyright (C) 2010-2011 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "timer.h"
#include "main.h"
#include "log.h"
#include "conf.h"

#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <sys/prctl.h>


static LIST_HEAD(timer_list);
static timer_id_t id_counter;


static inline void timer_lock(void)
{
	block_signals();
}

static inline void timer_unlock(void)
{
	unblock_signals();
}

static void timespec_add_msec(struct timespec *ts, unsigned int msec)
{
	unsigned int seconds, nsec;

	seconds = msec / 1000;
	msec = msec % 1000;
	nsec = msec * 1000000;

	ts->tv_nsec += nsec;
	while (ts->tv_nsec >= 1000000000) {
		ts->tv_sec++;
		ts->tv_nsec -= 1000000000;
	}
	ts->tv_sec += seconds;
}

static int timespec_bigger(const struct timespec *a, const struct timespec *b)
{
	if (a->tv_sec > b->tv_sec)
		return 1;
	if ((a->tv_sec == b->tv_sec) && (a->tv_nsec > b->tv_nsec))
		return 1;
	return 0;
}

static inline int timespec_after(const struct timespec *a, const struct timespec *b)
{
	return timespec_bigger(a, b);
}

void sleeptimer_init(struct sleeptimer *timer,
		     const char *name,
		     sleeptimer_callback_t callback)
{
	memset(timer, 0, sizeof(*timer));
	timer->name = name;
	timer->callback = callback;
	INIT_LIST_HEAD(&timer->list);
	logverbose("timer: %s registered\n", name);
}

void sleeptimer_set_timeout_relative(struct sleeptimer *timer,
				     unsigned int msecs)
{
	int err;

	err = clock_gettime(CLOCK_MONOTONIC, &timer->timeout);
	if (err) {
		logerr("WARNING: clock_gettime() failed: %s\n",
		       strerror(errno));
		return;
	}
	timespec_add_msec(&timer->timeout, msecs);
}

static void do_sleeptimer_dequeue(struct sleeptimer *timer)
{
	list_del_init(&timer->list);
}

void sleeptimer_enqueue(struct sleeptimer *timer)
{
	struct sleeptimer *i;
	int inserted = 0;

	timer_lock();

	if (!list_empty(&timer->list))
		do_sleeptimer_dequeue(timer);

	list_for_each_entry(i, &timer_list, list) {
		if (timespec_after(&i->timeout, &timer->timeout)) {
			list_add_tail(&timer->list, &i->list);
			inserted = 1;
			break;
		}
	}
	if (!inserted)
		list_add_tail(&timer->list, &timer_list);
	timer->id = id_counter++;

	timer_unlock();
}

void sleeptimer_dequeue(struct sleeptimer *timer)
{
	timer_lock();
	do_sleeptimer_dequeue(timer);
	timer_unlock();
}

int sleeptimer_system_init(void)
{
#ifdef PR_SET_TIMERSLACK
	unsigned long slack;

	slack = config_get_int(backend.config, "SYSTEM", "event_slack", 1010);
	slack *= 1000000ul; /* To nanoseconds */
	if (prctl(PR_SET_TIMERSLACK, slack, 0, 0, 0))
		logerr("Failed to set timerslack to %lu ns. Ignoring.\n", slack);
#else
# warning "PR_SET_TIMERSLACK not available. Ignoring."
#endif
	return 0;
}

int sleeptimer_wait_next(void)
{
	struct timespec timeout;
	struct sleeptimer *timer = NULL;
	timer_id_t timer_id = 0;
	int err;

	timer_lock();
	if (!list_empty(&timer_list)) {
		timer = list_first_entry(&timer_list, struct sleeptimer, list);
		timeout = timer->timeout;
		timer_id = timer->id;
	}
	timer_unlock();
	if (!timer)
		return -ENOENT;

	while (1) {
		err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
				      &timeout, NULL);
		if (!err)
			break;
		if (err == EAGAIN)
			continue;
		if (err != EINTR) {
			logerr("WARNING: clock_nanosleep() failed: %s\n",
			       strerror(err));
			break;
		}
	}

	timer_lock();
	list_for_each_entry(timer, &timer_list, list) {
		if (timer->id == timer_id) {
			do_sleeptimer_dequeue(timer);
			timer->callback(timer);
			break;
		}
	}
	timer_unlock();

	return 0;
}
bues.ch cgit interface