File: tlist.h

package info (click to toggle)
libqb 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,092 kB
  • sloc: ansic: 22,191; sh: 5,232; makefile: 607
file content (490 lines) | stat: -rw-r--r-- 11,691 bytes parent folder | download | duplicates (3)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * Copyright (c) 2006-2007, 2009-2021 Red Hat, Inc.
 *
 * Author: Jan Friesse <jfriesse@redhat.com>
 *         Steven Dake <sdake@redhat.com>
 *
 * This file is part of libqb.
 *
 * libqb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * libqb 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef QB_TLIST_H_DEFINED
#define QB_TLIST_H_DEFINED

#include "os_base.h"
#include <qb/qbdefs.h>
#include <qb/qbutil.h>
#include <qb/qblist.h>

#ifndef TIMER_HANDLE
typedef void *timer_handle;
#define TIMER_HANDLE
#endif

static int64_t timerlist_hertz;

struct timerlist {
	struct timerlist_timer **heap_entries;
	size_t allocated;
	size_t size;
	pthread_mutex_t list_mutex;
};

struct timerlist_timer {
	uint64_t expire_time;
	int32_t is_absolute_timer;
	void (*timer_fn) (void *data);
	void *data;
	timer_handle handle_addr;
	size_t heap_pos;
};

/*
 * Heap helper functions
 */
static inline size_t
timerlist_heap_index_left(size_t index)
{

	return (2 * index + 1);
}

static inline size_t
timerlist_heap_index_right(size_t index)
{

	return (2 * index + 2);
}

static inline size_t
timerlist_heap_index_parent(size_t index)
{

	return ((index - 1) / 2);
}

static inline void
timerlist_heap_entry_set(struct timerlist *timerlist, size_t item_pos, struct timerlist_timer *timer)
{

	assert(item_pos < timerlist->size);

	timerlist->heap_entries[item_pos] = timer;
	timerlist->heap_entries[item_pos]->heap_pos = item_pos;
}

static inline struct timerlist_timer *
timerlist_heap_entry_get(struct timerlist *timerlist, size_t item_pos)
{

	assert(item_pos < timerlist->size);

	return (timerlist->heap_entries[item_pos]);
}

static inline int
timerlist_entry_cmp(const struct timerlist_timer *t1, const struct timerlist_timer *t2)
{

	if (t1->expire_time == t2->expire_time) {
		return (0);
	} else if (t1->expire_time < t2->expire_time) {
		return (-1);
	} else {
		return (1);
	}
}

static inline void
timerlist_heap_sift_up(struct timerlist *timerlist, size_t item_pos)
{
	size_t parent_pos;
	struct timerlist_timer *parent_timer;
	struct timerlist_timer *timer;

	timer = timerlist_heap_entry_get(timerlist, item_pos);

	parent_pos = timerlist_heap_index_parent(item_pos);

	while (item_pos > 0 &&
	    (parent_timer = timerlist_heap_entry_get(timerlist, parent_pos),
	    timerlist_entry_cmp(parent_timer, timer) > 0)) {
		/*
		 * Swap item and parent
		 */
		timerlist_heap_entry_set(timerlist, parent_pos, timer);
		timerlist_heap_entry_set(timerlist, item_pos, parent_timer);

		item_pos = parent_pos;
		parent_pos = timerlist_heap_index_parent(item_pos);
	}
}

static inline void
timerlist_heap_sift_down(struct timerlist *timerlist, size_t item_pos)
{
	int cont;
	size_t left_pos, right_pos, smallest_pos;
	struct timerlist_timer *left_entry;
	struct timerlist_timer *right_entry;
	struct timerlist_timer *smallest_entry;
	struct timerlist_timer *tmp_entry;

	cont = 1;

	while (cont) {
		smallest_pos = item_pos;
		left_pos = timerlist_heap_index_left(item_pos);
		right_pos = timerlist_heap_index_right(item_pos);

		smallest_entry = timerlist_heap_entry_get(timerlist, smallest_pos);

		if (left_pos < timerlist->size &&
		    (left_entry = timerlist_heap_entry_get(timerlist, left_pos),
		    timerlist_entry_cmp(left_entry, smallest_entry) < 0)) {
			smallest_entry = left_entry;
			smallest_pos = left_pos;
		}

		if (right_pos < timerlist->size &&
		    (right_entry = timerlist_heap_entry_get(timerlist, right_pos),
		    timerlist_entry_cmp(right_entry, smallest_entry) < 0)) {
			smallest_entry = right_entry;
			smallest_pos = right_pos;
		}

		if (smallest_pos == item_pos) {
			/*
			 * Item is smallest (or has no children) -> heap property is restored
			 */
			cont = 0;
		} else {
			/*
			 * Swap item with smallest child
			 */
			tmp_entry = timerlist_heap_entry_get(timerlist, item_pos);
			timerlist_heap_entry_set(timerlist, item_pos, smallest_entry);
			timerlist_heap_entry_set(timerlist, smallest_pos, tmp_entry);

			item_pos = smallest_pos;
		}
	}
}

static inline void
timerlist_heap_delete(struct timerlist *timerlist, struct timerlist_timer *entry)
{
	size_t entry_pos;
	struct timerlist_timer *replacement_entry;
	int cmp_entries;

	entry_pos = entry->heap_pos;
	entry->heap_pos = (~(size_t)0);

	/*
	 * Swap element with last element
	 */
	replacement_entry = timerlist_heap_entry_get(timerlist, timerlist->size - 1);
	timerlist_heap_entry_set(timerlist, entry_pos, replacement_entry);

	/*
	 * And "remove" last element (= entry)
	 */
	timerlist->size--;

	/*
	 * Up (or down) heapify based on replacement item size
	 */
	cmp_entries = timerlist_entry_cmp(replacement_entry, entry);

	if (cmp_entries < 0) {
		timerlist_heap_sift_up(timerlist, entry_pos);
	} else if (cmp_entries > 0) {
		timerlist_heap_sift_down(timerlist, entry_pos);
	}
}

/*
 * Check if heap is valid.
 * - Shape property is always fullfiled because of storage in array
 * - Check heap property
 */
static inline int
timerlist_debug_is_valid_heap(struct timerlist *timerlist)
{
	size_t i;
	size_t left_pos, right_pos;
	struct timerlist_timer *left_entry;
	struct timerlist_timer *right_entry;
	struct timerlist_timer *cur_entry;

	for (i = 0; i < timerlist->size; i++) {
		cur_entry = timerlist_heap_entry_get(timerlist, i);

		left_pos = timerlist_heap_index_left(i);
		right_pos = timerlist_heap_index_right(i);

		if (left_pos < timerlist->size &&
		    (left_entry = timerlist_heap_entry_get(timerlist, left_pos),
		    timerlist_entry_cmp(left_entry, cur_entry) < 0)) {
			return (0);
		}

		if (right_pos < timerlist->size &&
		    (right_entry = timerlist_heap_entry_get(timerlist, right_pos),
		    timerlist_entry_cmp(right_entry, cur_entry) < 0)) {
			return (0);
		}
	}

	return (1);
}

/*
 * Main functions implementation
 */
static inline void timerlist_init(struct timerlist *timerlist)
{

	memset(timerlist, 0, sizeof(*timerlist));

	timerlist->heap_entries = NULL;
	pthread_mutex_init(&timerlist->list_mutex, NULL);
	timerlist_hertz = qb_util_nano_monotonic_hz();
}

static inline void timerlist_destroy(struct timerlist *timerlist)
{
	size_t zi;

	pthread_mutex_destroy(&timerlist->list_mutex);

	for (zi = 0; zi < timerlist->size; zi++) {
		free(timerlist->heap_entries[zi]);
	}
	free(timerlist->heap_entries);
}

static inline int32_t timerlist_add(struct timerlist *timerlist,
				 struct timerlist_timer *timer)
{
	size_t new_size;
	struct timerlist_timer **new_heap_entries;
	int32_t res = 0;

	if ( (res=pthread_mutex_lock(&timerlist->list_mutex))) {
		return -res;
	}

	/*
	 * Check that heap array is large enough
	 */
	if (timerlist->size + 1 > timerlist->allocated) {
		new_size = (timerlist->allocated + 1) * 2;

		new_heap_entries = realloc(timerlist->heap_entries,
		    new_size * sizeof(timerlist->heap_entries[0]));
		if (new_heap_entries == NULL) {
			res = -errno;

			goto cleanup;
		}

		timerlist->allocated = new_size;
		timerlist->heap_entries = new_heap_entries;
	}

	timerlist->size++;

	timerlist_heap_entry_set(timerlist, timerlist->size - 1, timer);
	timerlist_heap_sift_up(timerlist, timerlist->size - 1);

cleanup:
	pthread_mutex_unlock(&timerlist->list_mutex);
	return res;
}

static inline int32_t timerlist_add_duration(struct timerlist *timerlist,
					 void (*timer_fn) (void *data),
					 void *data,
					 uint64_t nano_duration,
					 timer_handle * handle)
{
	int res;
	struct timerlist_timer *timer;

	timer =
	    (struct timerlist_timer *)malloc(sizeof(struct timerlist_timer));

	if (timer == NULL) {
		return -ENOMEM;
	}

	timer->expire_time = qb_util_nano_current_get() + nano_duration;
	timer->is_absolute_timer = QB_FALSE;
	timer->data = data;
	timer->timer_fn = timer_fn;
	timer->handle_addr = handle;
	res = timerlist_add(timerlist, timer);
	if (res) {
		free(timer);
		return res;
	}

	*handle = timer;
	return (0);
}

static inline int32_t timerlist_del(struct timerlist *timerlist,
				 timer_handle _timer_handle)
{
	struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;
	int res;

	if ( (res=pthread_mutex_lock(&timerlist->list_mutex))) {
		return -res;
	}

	memset(timer->handle_addr, 0, sizeof(struct timerlist_timer *));

	timerlist_heap_delete(timerlist, timer);
	free(timer);

	pthread_mutex_unlock(&timerlist->list_mutex);
	return 0;
}

static inline uint64_t timerlist_expire_time(struct timerlist
						       *timerlist,
						       timer_handle
						       _timer_handle)
{
	struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;

	return (timer->expire_time);
}

static inline void timerlist_pre_dispatch(struct timerlist *timerlist,
					  timer_handle _timer_handle)
{
	struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;

	memset(timer->handle_addr, 0, sizeof(struct timerlist_timer *));

	timerlist_heap_delete(timerlist, timer);
}

static inline void timerlist_post_dispatch(struct timerlist *timerlist,
					   timer_handle _timer_handle)
{
	struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;

	free(timer);
}

/*
 * returns the number of msec until the next timer will expire for use with poll
 */
static inline uint64_t timerlist_msec_duration_to_expire(struct timerlist *timerlist)
{
	struct timerlist_timer *timer_from_list;
	volatile uint64_t current_time;
	volatile uint64_t msec_duration_to_expire;

	/*
	 * There is really no reasonable value to return when mutex lock fails
	 */
	if (pthread_mutex_lock(&timerlist->list_mutex)) {
		return (-1);
	}

	/*
	 * empty list, no expire
	 */
	if (timerlist->size == 0) {
		pthread_mutex_unlock(&timerlist->list_mutex);

		return (-1);
	}

	timer_from_list = timerlist_heap_entry_get(timerlist, 0);

	/*
	 * Mutex is no longer needed
	 */
	pthread_mutex_unlock(&timerlist->list_mutex);

	if (timer_from_list->is_absolute_timer) {
		current_time = qb_util_nano_from_epoch_get();
	} else {
		current_time = qb_util_nano_current_get();
	}

	/*
	 * timer at head of list is expired, zero msecs required
	 */
	if (timer_from_list->expire_time < current_time) {
		return (0);
	}

	msec_duration_to_expire =
	    ((timer_from_list->expire_time -
	      current_time) / QB_TIME_NS_IN_MSEC) + (1000 / timerlist_hertz);
	return (msec_duration_to_expire);
}

/*
 * Expires any timers that should be expired
 */
static inline int32_t timerlist_expire(struct timerlist *timerlist)
{
	struct timerlist_timer *timer;
	uint64_t current_time_from_epoch;
	uint64_t current_monotonic_time;
	uint64_t current_time;
	int res;

	current_monotonic_time = qb_util_nano_current_get();
	current_time_from_epoch = qb_util_nano_from_epoch_get();

	if ( (res=pthread_mutex_lock(&timerlist->list_mutex))) {
		return -res;
	}

	while (timerlist->size > 0) {
		timer = timerlist_heap_entry_get(timerlist, 0);

		current_time =
		    (timer->
		     is_absolute_timer ? current_time_from_epoch :
		     current_monotonic_time);

		if (timer->expire_time < current_time) {

			timerlist_pre_dispatch(timerlist, timer);

			timer->timer_fn(timer->data);

			timerlist_post_dispatch(timerlist, timer);
		} else {
			break;	/* for timer iteration */
		}
	}

	pthread_mutex_unlock(&timerlist->list_mutex);

	return (0);
}
#endif /* QB_TLIST_H_DEFINED */