Browse code

- local per process timer support (optimized "main" timer version that works only inside one process). Several independent timers can be run in the same time (if intialized with different handles). They'll be used for the tcp connection timeout handling (near future).

Andrei Pelinescu-Onciul authored on 22/11/2007 13:43:09
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,268 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2007 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+/* local, per process timer routines
27
+ * WARNING: this should be used only within the same process, the timers
28
+ *  are not multi-process safe or multi-thread safe
29
+ *  (there are no locks)
30
+ *
31
+ * History:
32
+ * --------
33
+ *  2006-02-03  created by andrei
34
+ */
35
+
36
+
37
+#include "timer.h"
38
+#include "timer_funcs.h"
39
+#include "dprint.h"
40
+#include "tcp_conn.h"
41
+#include "mem/mem.h"
42
+#include "compiler_opt.h"
43
+
44
+#include "local_timer.h"
45
+
46
+
47
+
48
+/* init a local_timer handle
49
+ * returns 0 on success, -1 on error */
50
+int init_local_timer(struct local_timer *t, ticks_t crt_ticks)
51
+{
52
+	int r;
53
+	
54
+	/* initial values */
55
+	memset(t, 0, sizeof(*t));
56
+	t->prev_ticks=crt_ticks;
57
+	/* init timer structures */
58
+	for (r=0; r<H0_ENTRIES; r++)
59
+		_timer_init_list(&t->timer_lst.h0[r]);
60
+	for (r=0; r<H1_ENTRIES; r++)
61
+		_timer_init_list(&t->timer_lst.h1[r]);
62
+	for (r=0; r<H2_ENTRIES; r++)
63
+		_timer_init_list(&t->timer_lst.h2[r]);
64
+	_timer_init_list(&t->timer_lst.expired);
65
+	DBG("init_local_timer: timer_list between %p and %p\n",
66
+			&t->timer_lst.h0[0], &t->timer_lst.h2[H2_ENTRIES]);
67
+	return 0;
68
+}
69
+
70
+
71
+
72
+void destroy_local_timer(struct local_timer* lt)
73
+{
74
+}
75
+
76
+
77
+
78
+/* generic add timer entry to the timer lists function (see _timer_add)
79
+ * tl->expire must be set previously, delta is the difference in ticks
80
+ * from current time to the timer desired expire (should be tl->expire-*tick)
81
+ * If you don't know delta, you probably want to call _timer_add instead.
82
+ */
83
+static inline int _local_timer_dist_tl(struct local_timer* h, 
84
+										struct timer_ln* tl, ticks_t delta)
85
+{
86
+	if (likely(delta<H0_ENTRIES)){
87
+		if (unlikely(delta==0)){
88
+			LOG(L_WARN, "WARNING: local_timer: add_timeout: 0 expire timer"
89
+						" added\n");
90
+			_timer_add_list(&h->timer_lst.expired, tl);
91
+		}else{
92
+			_timer_add_list( &h->timer_lst.h0[tl->expire & H0_MASK], tl);
93
+		}
94
+	}else if (likely(delta<(H0_ENTRIES*H1_ENTRIES))){
95
+		_timer_add_list(&h->timer_lst.h1[(tl->expire & H1_H0_MASK)>>H0_BITS],
96
+							tl);
97
+	}else{
98
+		_timer_add_list(&h->timer_lst.h2[tl->expire>>(H1_BITS+H0_BITS)], tl);
99
+	}
100
+	return 0;
101
+}
102
+
103
+
104
+
105
+static inline void local_timer_redist(struct local_timer* l,
106
+										ticks_t t, struct timer_head *h)
107
+{
108
+	struct timer_ln* tl;
109
+	struct timer_ln* tmp;
110
+	
111
+	timer_foreach_safe(tl, tmp, h){
112
+		_local_timer_dist_tl(l, tl, tl->expire-t);
113
+	}
114
+	/* clear the current list */
115
+	_timer_init_list(h);
116
+}
117
+
118
+
119
+
120
+/* local timer add function (no lock, not multithread or multiprocess safe,
121
+ * designed for local process use only)
122
+ * t = current ticks
123
+ * tl must be filled (the intial_timeout and flags must be set)
124
+ * returns -1 on error, 0 on success */
125
+static inline int _local_timer_add(struct local_timer *h, ticks_t t,
126
+									struct timer_ln* tl)
127
+{
128
+	ticks_t delta;
129
+	
130
+	delta=tl->initial_timeout;
131
+	tl->expire=t+delta;
132
+	return _local_timer_dist_tl(h, tl, delta);
133
+}
134
+
135
+
136
+
137
+/* "public", safe timer add functions (local process use only)
138
+ * adds a timer at delta ticks from the current time
139
+ * returns -1 on error, 0 on success
140
+ * WARNING: to re-add a deleted or expired timer you must call
141
+ *          timer_reinit(tl) prior to timer_add
142
+ *          The default behaviour allows timer_add to add a timer only if it
143
+ *          has never been added before.*/
144
+int local_timer_add(struct local_timer* h, struct timer_ln* tl, ticks_t delta,
145
+						ticks_t crt_ticks)
146
+{
147
+	int ret;
148
+	
149
+	if (unlikely(tl->flags & F_TIMER_ACTIVE)){
150
+		DBG("timer_add called on an active timer %p (%p, %p),"
151
+					" flags %x\n", tl, tl->next, tl->prev, tl->flags);
152
+		ret=-1; /* refusing to add active or non-reinit. timer */
153
+		goto error;
154
+	}
155
+	tl->initial_timeout=delta;
156
+	if (unlikely((tl->next!=0) || (tl->prev!=0))){
157
+		LOG(L_CRIT, "BUG: tcp_timer_add: called with linked timer:"
158
+				" %p (%p, %p)\n", tl, tl->next, tl->prev);
159
+		ret=-1;
160
+		goto error;
161
+	}
162
+	tl->flags|=F_TIMER_ACTIVE;
163
+	ret=_local_timer_add(h, crt_ticks, tl);
164
+error:
165
+	return ret;
166
+}
167
+
168
+
169
+
170
+/* safe timer delete
171
+ * deletes tl and inits the list pointer to 0
172
+ * WARNING: to be able to reuse a deleted timer you must call
173
+ *          timer_reinit(tl) on it
174
+ * 
175
+ */
176
+void local_timer_del(struct local_timer* h, struct timer_ln* tl)
177
+{
178
+	/* quick exit if timer inactive */
179
+	if (unlikely(!(tl->flags & F_TIMER_ACTIVE))){
180
+		DBG("timer_del called on an inactive timer %p (%p, %p),"
181
+					" flags %x\n", tl, tl->next, tl->prev, tl->flags);
182
+		return;
183
+	}
184
+	if (likely((tl->next!=0)&&(tl->prev!=0))){
185
+		_timer_rm_list(tl); /* detach */
186
+		tl->next=tl->prev=0;
187
+	}else{
188
+		DBG("timer_del: (f) timer %p (%p, %p) flags %x "
189
+			"already detached\n",
190
+			tl, tl->next, tl->prev, tl->flags);
191
+	}
192
+}
193
+
194
+
195
+
196
+/* called from timer_handle*/
197
+inline static void local_timer_list_expire(struct local_timer* l, 
198
+											ticks_t t, struct timer_head* h)
199
+{
200
+	struct timer_ln * tl;
201
+	ticks_t ret;
202
+	
203
+	/*DBG("timer_list_expire @ ticks = %lu, list =%p\n",
204
+			(unsigned long) *ticks, h);
205
+	*/
206
+	while(h->next!=(struct timer_ln*)h){
207
+		tl=h->next;
208
+		_timer_rm_list(tl); /* detach */
209
+			tl->next=tl->prev=0; /* debugging */
210
+				/*FIXME: process tcpconn */
211
+				ret=tl->f(t, tl, tl->data);
212
+				if (ret!=0){
213
+					/* not one-shot, re-add it */
214
+					if (ret!=(ticks_t)-1) /* ! periodic */
215
+						tl->initial_timeout=ret;
216
+					_local_timer_add(l, t, tl);
217
+				}
218
+	}
219
+}
220
+
221
+
222
+
223
+/* run all the handler that expire at t ticks */
224
+static inline void local_timer_expire(struct local_timer* h, ticks_t t)
225
+{
226
+	/* trust the compiler for optimizing */
227
+	if (unlikely((t & H0_MASK)==0)){              /*r1*/
228
+		if (unlikely((t & H1_H0_MASK)==0)){        /*r2*/
229
+			local_timer_redist(h, t, &h->timer_lst.h2[t>>(H0_BITS+H1_BITS)]);
230
+		}
231
+		
232
+		local_timer_redist(h, t, &h->timer_lst.h1[(t & H1_H0_MASK)>>H0_BITS]);
233
+															/*r2 >> H0*/
234
+	}
235
+	/* run handler immediately, no need to move it to the expired list
236
+	 * (since no locks are used) */
237
+	local_timer_list_expire(h, t, &h->timer_lst.h0[t & H0_MASK]);
238
+}
239
+
240
+
241
+
242
+/* "main" local timer routine, should be called with a proper ticks value
243
+ * WARNING: it should never be called twice for the same ticks value
244
+ * (it could cause too fast expires for long timers), ticks must be also
245
+ *  always increasing */
246
+void local_timer_run(struct local_timer* lt, ticks_t saved_ticks)
247
+{
248
+	
249
+		/* protect against time running backwards */
250
+		if (unlikely(lt->prev_ticks>=saved_ticks)){
251
+			LOG(L_CRIT, "BUG: local_timer: backwards or still time\n");
252
+			/* try to continue */
253
+			lt->prev_ticks=saved_ticks-1;
254
+			return;
255
+		}
256
+		/* go through all the "missed" ticks, taking a possible overflow
257
+		 * into account */
258
+		for (lt->prev_ticks=lt->prev_ticks+1; lt->prev_ticks!=saved_ticks; 
259
+															lt->prev_ticks++)
260
+			local_timer_expire(lt, lt->prev_ticks);
261
+		local_timer_expire(lt, lt->prev_ticks); /* do it for saved_ticks too */
262
+	local_timer_list_expire(lt, saved_ticks, &lt->timer_lst.expired);
263
+	/* WARNING: add_timer(...,0) must go directly to expired list, since
264
+	 * otherwise there is a race between timer running and adding it
265
+	 * (it could expire it H0_ENTRIES ticks later instead of 'now')*/
266
+}
267
+
0 268
new file mode 100644
... ...
@@ -0,0 +1,57 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2007 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+/* local timer routines
27
+ * WARNING: this should be used only from within the same process.
28
+ * The local timers are not multi-process or multi-thread safe 
29
+ *  (there are no locks)
30
+ *
31
+ * History:
32
+ * --------
33
+ *  2007-11-22  created by andrei
34
+ */
35
+
36
+#ifndef _local_timer_h
37
+#define _local_timer_h
38
+
39
+
40
+struct local_timer {
41
+	/* private timer information */
42
+	ticks_t prev_ticks; /* last time we ran the timer */
43
+	struct timer_lists timer_lst; /* actual timer lists */
44
+};
45
+
46
+
47
+int init_local_timer(struct local_timer *lt_handle, ticks_t crt_ticks);
48
+void destroy_local_timer(struct local_timer* lt_handle);
49
+
50
+int local_timer_add(struct local_timer* h, struct timer_ln* tl, ticks_t delta,
51
+						ticks_t crt_ticks);
52
+
53
+void local_timer_del(struct local_timer* h, struct timer_ln* tl);
54
+void local_timer_run(struct local_timer* lt, ticks_t crt_ticks);
55
+
56
+#endif /* _local_timer_h */