File: tlist.h

package info (click to toggle)
libqb 0.11.1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,804 kB
  • ctags: 1,689
  • sloc: ansic: 15,630; sh: 11,355; makefile: 271
file content (230 lines) | stat: -rw-r--r-- 6,143 bytes parent folder | download
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
/*
 * Copyright (c) 2006-2007, 2009 Red Hat, Inc.
 *
 * Author: 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 = HZ;

struct timerlist {
	struct qb_list_head timer_head;
	struct qb_list_head *timer_iter;
};

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

static inline void timerlist_init(struct timerlist *timerlist)
{
	qb_list_init(&timerlist->timer_head);
	timerlist_hertz = qb_util_nano_monotonic_hz();
}

static inline void timerlist_add(struct timerlist *timerlist,
				 struct timerlist_timer *timer)
{
	struct qb_list_head *timer_list = 0;
	struct timerlist_timer *timer_from_list;
	int32_t found;

	for (found = 0, timer_list = timerlist->timer_head.next;
	     timer_list != &timerlist->timer_head;
	     timer_list = timer_list->next) {

		timer_from_list = qb_list_entry(timer_list,
						struct timerlist_timer, list);

		if (timer_from_list->expire_time > timer->expire_time) {
			qb_list_add(&timer->list, timer_list->prev);
			found = 1;
			break;	/* for timer iteration */
		}
	}
	if (found == 0) {
		qb_list_add(&timer->list, timerlist->timer_head.prev);
	}
}

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

	timer =
	    (struct timerlist_timer *)malloc(sizeof(struct timerlist_timer));
	if (timer == 0) {
		return -ENOMEM;
	}

	timer->expire_time = qb_util_nano_current_get() + nano_duration;
	timer->is_absolute_timer = 0;
	timer->data = data;
	timer->timer_fn = timer_fn;
	timer->handle_addr = handle;
	timerlist_add(timerlist, timer);

	*handle = timer;
	return (0);
}

static inline void timerlist_del(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 *));
	/*
	 * If the next timer after the currently expiring timer because
	 * timerlist_del is called from a timer handler, get to the next
	 * timer
	 */
	if (timerlist->timer_iter == &timer->list) {
		timerlist->timer_iter = timerlist->timer_iter->next;
	}
	qb_list_del(&timer->list);
	qb_list_init(&timer->list);
	free(timer);
}

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 *));
	qb_list_del(&timer->list);
	qb_list_init(&timer->list);
}

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;

	/*
	 * empty list, no expire
	 */
	if (timerlist->timer_head.next == &timerlist->timer_head) {
		return (-1);
	}

	timer_from_list = qb_list_entry(timerlist->timer_head.next,
					struct timerlist_timer, list);

	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 void timerlist_expire(struct timerlist *timerlist)
{
	struct timerlist_timer *timer_from_list;
	uint64_t current_time_from_epoch;
	uint64_t current_monotonic_time;
	uint64_t current_time;

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

	for (timerlist->timer_iter = timerlist->timer_head.next;
	     timerlist->timer_iter != &timerlist->timer_head;) {

		timer_from_list = qb_list_entry(timerlist->timer_iter,
						struct timerlist_timer, list);

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

		if (timer_from_list->expire_time < current_time) {
			timerlist->timer_iter = timerlist->timer_iter->next;

			timerlist_pre_dispatch(timerlist, timer_from_list);

			timer_from_list->timer_fn(timer_from_list->data);

			timerlist_post_dispatch(timerlist, timer_from_list);
		} else {
			break;	/* for timer iteration */
		}
	}
	timerlist->timer_iter = 0;
}
#endif /* QB_TLIST_H_DEFINED */