summaryrefslogtreecommitdiff
path: root/src/common/evsched.c
blob: 8b6f721ed20aabe5729a0e0d7fd8ed00d9327ab0 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <config.h>

#include "common/evsched.h"

/*! \todo Fix properly (issue #1581). */
#ifndef HAVE_PSELECT
#define OPENBSD_SLAB_BROKEN
#endif

/* Heap only cares about x<y. */
static int compare_event_heap_nodes(event_t **e1, event_t **e2)
{
	if (timercmp(&(*e1)->tv, &(*e2)->tv, <)) return -1;
	if (timercmp(&(*e1)->tv, &(*e2)->tv, >)) return 1;
	return 0;
}


/*!
 * \brief Set event timer to T (now) + dt miliseconds.
 */
static void evsched_settimer(event_t *e, uint32_t dt)
{
	if (!e) {
		return;
	}

	/* Get absolute time T. */
	gettimeofday(&e->tv, 0);

	/* Add number of seconds. */
	e->tv.tv_sec += dt / 1000;

	/* Add the number of microseconds. */
	e->tv.tv_usec += (dt % 1000) * 1000;

	/* Check for overflow. */
	while (e->tv.tv_usec > 999999) {
		e->tv.tv_sec += 1;
		e->tv.tv_usec -= 1 * 1000 * 1000;
	}
}

/*! \brief Singleton application-wide event scheduler. */
evsched_t *s_evsched = 0;

evsched_t *evsched_new()
{
	evsched_t *s = malloc(sizeof(evsched_t));
	if (!s) {
		return 0;
	}

	/* Initialize event calendar. */
	pthread_mutex_init(&s->rl, 0);
	pthread_mutex_init(&s->mx, 0);
	pthread_cond_init(&s->notify, 0);
	pthread_mutex_init(&s->cache.lock, 0);
#ifndef OPENBSD_SLAB_BROKEN
	slab_cache_init(&s->cache.alloc, sizeof(event_t));
#endif
	heap_init(&s->heap, sizeof(event_t *), compare_event_heap_nodes, 0, NULL);
	return s;
}

void evsched_delete(evsched_t **s)
{
	if (!s) {
		return;
	}
	if (!*s) {
		return;
	}

	/* Deinitialize event calendar. */
	pthread_mutex_destroy(&(*s)->rl);
	pthread_mutex_destroy(&(*s)->mx);
	pthread_cond_destroy(&(*s)->notify);

	while (! EMPTY_HEAP(&(*s)->heap))	/* FIXME: Would be faster to simply walk through the array */
	{
		event_t *e = *((event_t**)(HHEAD(&(*s)->heap)));
		heap_delmin(&(*s)->heap);
		evsched_event_free((*s), e);
	}
	
	free((*s)->heap.data);
	(*s)->heap.data = NULL;;

#ifndef OPENBSD_SLAB_BROKEN
	/* Free allocator. */
	slab_cache_destroy(&(*s)->cache.alloc);
#endif
	pthread_mutex_destroy(&(*s)->cache.lock);

	/* Free scheduler. */
	free(*s);
	*s = 0;
}

event_t *evsched_event_new(evsched_t *s, int type)
{
	if (!s) {
		return 0;
	}

	/* Allocate. */
#ifndef OPENBSD_SLAB_BROKEN
	pthread_mutex_lock(&s->cache.lock);
	event_t *e = slab_cache_alloc(&s->cache.alloc);
	pthread_mutex_unlock(&s->cache.lock);
#else
	event_t *e = malloc(sizeof(event_t));
#endif
        if (e == NULL) {
		return NULL;
	}

	/* Initialize. */
	memset(e, 0, sizeof(event_t));
	e->type = type;
	return e;
}

void evsched_event_free(evsched_t *s, event_t *ev)
{
	if (!s || !ev) {
		return;
	}

#ifndef OPENBSD_SLAB_BROKEN
	pthread_mutex_lock(&s->cache.lock);
	slab_free(ev);
	pthread_mutex_unlock(&s->cache.lock);
#else
	free(ev);
#endif
}

event_t* evsched_next(evsched_t *s)
{
	/* Check. */
	if (!s) {
		return 0;
	}

	/* Lock calendar. */
	pthread_mutex_lock(&s->mx);

	while(1) {

		/* Check event queue. */
		if (!EMPTY_HEAP(&s->heap)) {

			/* Get current time. */
			struct timeval dt;
			gettimeofday(&dt, 0);

			/* Get next event. */
			event_t *next_ev = *((event_t**)HHEAD(&s->heap));

			/* Immediately return. */
			if (timercmp(&dt, &next_ev->tv, >=)) {
				s->current = next_ev;
				heap_delmin(&s->heap);
				pthread_mutex_unlock(&s->mx);
				pthread_mutex_lock(&s->rl);
				return next_ev;
			}

			/* Wait for next event or interrupt. Unlock calendar. */
			/* FIXME: Blocks this the possibility to add any event earlier? */
			struct timespec ts;
			ts.tv_sec = next_ev->tv.tv_sec;
			ts.tv_nsec = next_ev->tv.tv_usec * 1000L;
			pthread_cond_timedwait(&s->notify, &s->mx, &ts);
		} else {
			/* Block until an event is scheduled. Unlock calendar.*/
			pthread_cond_wait(&s->notify, &s->mx);
		}
	}

	/* Unlock calendar, this shouldn't happen. */
	pthread_mutex_unlock(&s->mx);
	return 0;

}

int evsched_event_finished(evsched_t *s)
{
	if (!s) {
		return -1;
	}

	/* Mark as finished. */
	if (s->current) {
		s->current = 0;
		pthread_mutex_unlock(&s->rl);
		return 0;
	}

	/* Finished event is not current. */
	return -1;
}

int evsched_schedule(evsched_t *s, event_t *ev, uint32_t dt)
{
	if (!s || !ev) {
		return -1;
	}

	/* Update event timer. */
	evsched_settimer(ev, dt);
	ev->parent = s;
	
	/* Lock calendar. */
	pthread_mutex_lock(&s->mx);
	
	heap_insert(&s->heap, &ev);

	/* Unlock calendar. */
	pthread_cond_signal(&s->notify);
	pthread_mutex_unlock(&s->mx);

	return 0;
}

event_t* evsched_schedule_cb(evsched_t *s, event_cb_t cb, void *data, uint32_t dt)
{
	if (!s) {
		return 0;
	}

	/* Create event. */
	event_t *e = evsched_event_new(s, EVSCHED_CB);
	if (!e) {
		return 0;
	}
	e->cb = cb;
	e->data = data;

	/* Schedule. */
	if (evsched_schedule(s, e, dt) != 0) {
		evsched_event_free(s, e);
		e = 0;
	}

	return e;
}

event_t* evsched_schedule_term(evsched_t *s, uint32_t dt)
{
	if (!s) {
		return 0;
	}

	/* Create event. */
	event_t *e = evsched_event_new(s, EVSCHED_TERM);
	if (!e) {
		return 0;
	}

	/* Schedule. */
	if (evsched_schedule(s, e, dt) != 0) {
		evsched_event_free(s, e);
		e = 0;
	}

	return e;
}

int evsched_cancel(evsched_t *s, event_t *ev)
{
	int found;

	if (!s || !ev) {
		return -1;
	}

	/* Make sure not running. */
	pthread_mutex_lock(&s->rl);

	/* Lock calendar. */
	pthread_mutex_lock(&s->mx);

	if ((found = heap_find(&s->heap, &ev))) {
		heap_delete(&s->heap, found);
	}

	/* Unlock calendar. */
	pthread_cond_signal(&s->notify);
	pthread_mutex_unlock(&s->mx);

	/* Enable running events. */
	pthread_mutex_unlock(&s->rl);

	return found;
}