Browse code

- added some kind of primitive timer interface in core ser

Andrei Pelinescu-Onciul authored on 06/12/2001 18:28:30
Showing 5 changed files
... ...
@@ -55,10 +55,13 @@ CC=gcc
55 55
 LD=gcc
56 56
 
57 57
 ifeq ($(mode), release)
58
-	CFLAGS=-O2 -fPIC -DPIC -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
58
+	CFLAGS=-O2 -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
59 59
 	LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
60
+	# we need -fPIC -DPIC only for shared objects, we don't need them for the 
61
+	# executable file, because it's always loaded at a fixed address
62
+	# -andrei
60 63
 else
61
-	CFLAGS=-g -fPIC -DPIC -Wcast-align -Winline
64
+	CFLAGS=-g -Wcast-align -Winline
62 65
 	LDFLAGS=-g -Wl,-E
63 66
 endif
64 67
 
... ...
@@ -50,5 +50,8 @@
50 50
 /*used is SH_MEM is defined*/
51 51
 #define SHM_MEM_SIZE 1024*1024
52 52
 
53
+#define TIMER_TICK 1
54
+#define LONG_SLEEP	3600
55
+
53 56
 
54 57
 #endif
... ...
@@ -28,6 +28,7 @@
28 28
 #include "shm_mem.h"
29 29
 #endif
30 30
 #include "sr_module.h"
31
+#include "timer.h"
31 32
 
32 33
 
33 34
 #include <signal.h>
... ...
@@ -230,6 +231,22 @@ int main_loop()
230 230
 #endif
231 231
 		/* only one address */
232 232
 		if (udp_init(addresses[0],port_no)==-1) goto error;
233
+
234
+		/* we need another process to act as the timer*/
235
+		if (timer_list){
236
+				if ((pid=fork())<0){
237
+					LOG(L_CRIT,  "main_loop: Cannot fork\n");
238
+					goto error;
239
+				}
240
+				if (pid==0){
241
+					/* child */
242
+					/* timer!*/
243
+					for(;;){
244
+						sleep(TIMER_TICK);
245
+						timer_ticker();
246
+					}
247
+				}
248
+		}
233 249
 		/* receive loop */
234 250
 		udp_rcv_loop();
235 251
 	}else{
... ...
@@ -252,11 +269,16 @@ int main_loop()
252 252
 			close(udp_sock); /*parent*/
253 253
 		}
254 254
 	}
255
-		
256
-	for(;;){
257
-		/* debug:  instead of doing something usefull */
258
-		/* (placeholder for timers, etc.) */
259
-		sleep(10);
255
+	if (timer_list){
256
+		for(;;){
257
+			/* debug:  instead of doing something usefull */
258
+			/* (placeholder for timers, etc.) */
259
+			sleep(TIMER_TICK);
260
+			/* if we received a signal => TIMER_TICK may have not elapsed*/
261
+			timer_ticker();
262
+		}
263
+	}else{
264
+		for(;;) sleep(LONG_SLEEP);
260 265
 	}
261 266
 	
262 267
 	return 0;
... ...
@@ -309,8 +331,9 @@ static void sig_usr(int signo)
309 309
 #endif
310 310
 	}
311 311
 }
312
-	
313
-	
312
+
313
+
314
+
314 315
 int main(int argc, char** argv)
315 316
 {
316 317
 
... ...
@@ -332,6 +355,7 @@ int main(int argc, char** argv)
332 332
 		goto error;
333 333
 	}
334 334
 
335
+
335 336
 	/* process command line (get port no, cfg. file path etc) */
336 337
 	opterr=0;
337 338
 	options=
... ...
@@ -543,7 +567,6 @@ int main(int argc, char** argv)
543 543
 		if ( daemonize(argv[0]) <0 ) goto error;
544 544
 	}
545 545
 
546
-
547 546
 	return main_loop();
548 547
 
549 548
 
550 549
new file mode 100644
... ...
@@ -0,0 +1,69 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+#include "timer.h"
5
+#include "dprint.h"
6
+#include "error.h"
7
+#include "config.h"
8
+
9
+#include <stdlib.h>
10
+
11
+
12
+struct sr_timer* timer_list=0;
13
+
14
+static int jiffies=0;
15
+static int timer_id=0;
16
+
17
+/*register a periodic timer;
18
+ * ret: <0 on error*/
19
+int register_timer(timer_function f, void* param, unsigned int interval)
20
+{
21
+	struct sr_timer* t;
22
+
23
+	t=malloc(sizeof(struct sr_timer));
24
+	if (t==0){
25
+		LOG(L_ERR, "ERROR: register_timer: out of memory\n");
26
+		goto error;
27
+	}
28
+	t->id=timer_id++;
29
+	t->timer_f=f;
30
+	t->t_param=param;
31
+	t->interval=interval;
32
+	t->expires=jiffies+interval;
33
+	/* insert it into the list*/
34
+	t->next=timer_list;
35
+	timer_list=t;
36
+	return t->id;
37
+
38
+error:
39
+	return E_OUT_OF_MEM;
40
+}
41
+
42
+
43
+
44
+void timer_ticker()
45
+{
46
+	struct sr_timer* t;
47
+	unsigned int prev_jiffies;
48
+	
49
+	prev_jiffies=jiffies;
50
+	jiffies+=TIMER_TICK;
51
+	/* test for overflow (if tick= 1s =>overflow in 136 years)*/
52
+	if (jiffies<prev_jiffies){ 
53
+		/*force expire & update every timer, a little buggy but it 
54
+		 * happens once in 136 years :) */
55
+		for(t=timer_list;t;t=t->next){
56
+			t->expires=jiffies+t->interval;
57
+			t->timer_f(jiffies, t->t_param);
58
+		}
59
+		return;
60
+	}
61
+	
62
+	for (t=timer_list;t; t=t->next){
63
+		if (jiffies>=t->expires){
64
+			t->expires=jiffies+t->interval;
65
+			t->timer_f(jiffies, t->t_param);
66
+		}
67
+	}
68
+}
0 69
new file mode 100644
... ...
@@ -0,0 +1,37 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ *
4
+ * timer related functions
5
+ */
6
+
7
+
8
+#ifndef timer_h
9
+#define timer_h
10
+
11
+typedef void (timer_function)(unsigned int ticks, void* param);
12
+
13
+
14
+struct sr_timer{
15
+	int id;
16
+	timer_function* timer_f;
17
+	void* t_param;
18
+	unsigned int interval;
19
+	
20
+	unsigned int expires;
21
+	
22
+	struct sr_timer* next;
23
+};
24
+
25
+
26
+
27
+extern struct sr_timer* timer_list;
28
+
29
+
30
+
31
+/*register a periodic timer;
32
+ * ret: <0 on errror*/
33
+int register_timer(timer_function f, void* param, unsigned int interval);
34
+void timer_ticker();
35
+
36
+#endif