summaryrefslogtreecommitdiff
path: root/src/gsmd/timer.c
blob: 31901c763219ca981b27da5ba11a80329245b389 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* gsmd timer code
 *
 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
 * (C) 2007 by OpenMoko, Inc.
 * Written by Harald Welte <laforge@openmoko.org>
 * All Rights Reserved
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

#include <common/linux_list.h>

#include "gsmd.h"

#include <gsmd/gsmd.h>
#include <gsmd/talloc.h>

static LLIST_HEAD(gsmd_timers);
static void *__tmr_ctx;

#if 0
static void tv_normalize(struct timeval *out)
{
	out->tv_sec += (out->tv_usec / 1000000);
	out->tv_usec = (out->tv_usec % 1000000);
}
#endif
/* subtract two struct timevals */
static int tv_sub(struct timeval *res, const struct timeval *from,
		  const struct timeval *sub)
{
	res->tv_sec = from->tv_sec - sub->tv_sec;
	res->tv_usec = from->tv_usec - sub->tv_usec;

	while (res->tv_usec < 0) {
		res->tv_sec -= 1;
		res->tv_usec += 1000000;
	}

	return 0;
}
#if 0
static int tv_add(struct timeval *res, const struct timeval *a1,
		  const struct timeval *a2)
{
	unsigned int carry;

	res->tv_sec = a1->tv_sec + a2->tv_sec;
	res->tv_usec = a1->tv_usec + a2->tv_usec;

	tv_normalize(res);
}
#endif
static int tv_later(const struct timeval *expires, const struct timeval *now)
{
	if (expires->tv_sec < now->tv_sec)
		return 0;
	else if (expires->tv_sec > now->tv_sec)
		return 1;
	else /* if (expires->tv_sec == now->tv_sec */ {
		if (expires->tv_usec >= now->tv_usec)
			return 1;
	}

	return 0;
}

static int tv_smaller(const struct timeval *t1, const struct timeval *t2)
{
	return tv_later(t2, t1);
}

static int calc_next_expiration(void)
{
	struct gsmd_timer *cur;
	struct timeval min, now, diff;
	struct itimerval iti;
	int ret;

	gettimeofday(&now, NULL);

retry:
	if (llist_empty(&gsmd_timers))
		return 0;

	llist_for_each_entry(cur, &gsmd_timers, list) {
		if (gsmd_timers.next == &cur->list)
			min = cur->expires;

		if (tv_smaller(&cur->expires, &min))
			min = cur->expires;
	}

	if (tv_sub(&diff, &min, &now) < 0) {
		/* FIXME: run expired timer callbacks */
		/* we cannot run timers from here since we might be
		 * called from register_timer() within check_n_run() */

		/* FIXME: restart with next minimum timer */
		goto retry;
	}

	/* re-set kernel timer */
	memset(&iti, 0, sizeof(iti));
	memcpy(&iti.it_value, &diff, sizeof(iti.it_value));
	ret = setitimer(ITIMER_REAL, &iti, NULL);
	if (ret < 0)
		return ret;

	return 0;
}

void gsmd_timer_check_n_run(void)
{
	struct gsmd_timer *cur, *cur2;
	struct timeval now;

	if (gettimeofday(&now, NULL) < 0)
		return;

	llist_for_each_entry_safe(cur, cur2, &gsmd_timers, list) {
		if (tv_later(&now, &cur->expires)) {
			/* fist delete it from the list of timers */
			llist_del(&cur->list);
			/* then call.  called function can re-add it */
			(cur->cb)(cur, cur->data);
		}
	}

	calc_next_expiration();
}

int gsmd_timer_init(void)
{
	__tmr_ctx = talloc_named_const(gsmd_tallocs, 1, "timers");

	return 0;
}

struct gsmd_timer *gsmd_timer_alloc(void)
{
	struct gsmd_timer *tmr;

	tmr = talloc_zero_size(__tmr_ctx, sizeof(*tmr));
	return tmr;
}

int gsmd_timer_register(struct gsmd_timer *timer)
{
	int ret;
	struct timeval tv;

	ret = gettimeofday(&tv, NULL);
	if (ret < 0)
		return ret;

	/* convert expiration time into absoulte time */
	timer->expires.tv_sec += tv.tv_sec;
	timer->expires.tv_usec += tv.tv_usec;

	llist_add_tail(&timer->list, &gsmd_timers);

	/* re-calculate next expiration */
	calc_next_expiration();

	return 0;
}

struct gsmd_timer *gsmd_timer_create(struct timeval *expires,
				     void (*cb)(struct gsmd_timer *tmr,
						void *data),
				     void *data)
{
	struct gsmd_timer *tmr = gsmd_timer_alloc();
	int rc;

	if (!tmr)
		return NULL;

	memcpy(&tmr->expires, expires, sizeof(tmr->expires));
	tmr->cb = cb;
	tmr->data = data;

	rc = gsmd_timer_register(tmr);
	if (rc < 0) {
		talloc_free(tmr);
		return NULL;
	}

	return tmr;
}

void gsmd_timer_unregister(struct gsmd_timer *timer)
{
	llist_del(&timer->list);

	/* re-calculate next expiration */
	calc_next_expiration();
}
personal git repositories of Harald Welte. Your mileage may vary