Parcourir la source

- added some kind of primitive timer interface in core ser

Andrei Pelinescu-Onciul il y a 24 ans
Parent
commit
cd57180a8b
5 fichiers modifiés avec 145 ajouts et 10 suppressions
  1. 5 2
      Makefile
  2. 3 0
      config.h
  3. 31 8
      main.c
  4. 69 0
      timer.c
  5. 37 0
      timer.h

+ 5 - 2
Makefile

@@ -55,10 +55,13 @@ CC=gcc
 LD=gcc
 LD=gcc
 
 
 ifeq ($(mode), release)
 ifeq ($(mode), release)
-	CFLAGS=-O2 -fPIC -DPIC -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
+	CFLAGS=-O2 -Wcast-align $(PROFILE) -Winline#-Wmissing-prototypes 
 	LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
 	LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
+	# we need -fPIC -DPIC only for shared objects, we don't need them for the 
+	# executable file, because it's always loaded at a fixed address
+	# -andrei
 else
 else
-	CFLAGS=-g -fPIC -DPIC -Wcast-align -Winline
+	CFLAGS=-g -Wcast-align -Winline
 	LDFLAGS=-g -Wl,-E
 	LDFLAGS=-g -Wl,-E
 endif
 endif
 
 

+ 3 - 0
config.h

@@ -50,5 +50,8 @@
 /*used is SH_MEM is defined*/
 /*used is SH_MEM is defined*/
 #define SHM_MEM_SIZE 1024*1024
 #define SHM_MEM_SIZE 1024*1024
 
 
+#define TIMER_TICK 1
+#define LONG_SLEEP	3600
+
 
 
 #endif
 #endif

+ 31 - 8
main.c

@@ -28,6 +28,7 @@
 #include "shm_mem.h"
 #include "shm_mem.h"
 #endif
 #endif
 #include "sr_module.h"
 #include "sr_module.h"
+#include "timer.h"
 
 
 
 
 #include <signal.h>
 #include <signal.h>
@@ -230,6 +231,22 @@ int main_loop()
 #endif
 #endif
 		/* only one address */
 		/* only one address */
 		if (udp_init(addresses[0],port_no)==-1) goto error;
 		if (udp_init(addresses[0],port_no)==-1) goto error;
+
+		/* we need another process to act as the timer*/
+		if (timer_list){
+				if ((pid=fork())<0){
+					LOG(L_CRIT,  "main_loop: Cannot fork\n");
+					goto error;
+				}
+				if (pid==0){
+					/* child */
+					/* timer!*/
+					for(;;){
+						sleep(TIMER_TICK);
+						timer_ticker();
+					}
+				}
+		}
 		/* receive loop */
 		/* receive loop */
 		udp_rcv_loop();
 		udp_rcv_loop();
 	}else{
 	}else{
@@ -252,11 +269,16 @@ int main_loop()
 			close(udp_sock); /*parent*/
 			close(udp_sock); /*parent*/
 		}
 		}
 	}
 	}
-		
-	for(;;){
-		/* debug:  instead of doing something usefull */
-		/* (placeholder for timers, etc.) */
-		sleep(10);
+	if (timer_list){
+		for(;;){
+			/* debug:  instead of doing something usefull */
+			/* (placeholder for timers, etc.) */
+			sleep(TIMER_TICK);
+			/* if we received a signal => TIMER_TICK may have not elapsed*/
+			timer_ticker();
+		}
+	}else{
+		for(;;) sleep(LONG_SLEEP);
 	}
 	}
 	
 	
 	return 0;
 	return 0;
@@ -309,8 +331,9 @@ static void sig_usr(int signo)
 #endif
 #endif
 	}
 	}
 }
 }
-	
-	
+
+
+
 int main(int argc, char** argv)
 int main(int argc, char** argv)
 {
 {
 
 
@@ -332,6 +355,7 @@ int main(int argc, char** argv)
 		goto error;
 		goto error;
 	}
 	}
 
 
+
 	/* process command line (get port no, cfg. file path etc) */
 	/* process command line (get port no, cfg. file path etc) */
 	opterr=0;
 	opterr=0;
 	options=
 	options=
@@ -543,7 +567,6 @@ int main(int argc, char** argv)
 		if ( daemonize(argv[0]) <0 ) goto error;
 		if ( daemonize(argv[0]) <0 ) goto error;
 	}
 	}
 
 
-
 	return main_loop();
 	return main_loop();
 
 
 
 

+ 69 - 0
timer.c

@@ -0,0 +1,69 @@
+/*
+ * $Id$
+ */
+
+#include "timer.h"
+#include "dprint.h"
+#include "error.h"
+#include "config.h"
+
+#include <stdlib.h>
+
+
+struct sr_timer* timer_list=0;
+
+static int jiffies=0;
+static int timer_id=0;
+
+/*register a periodic timer;
+ * ret: <0 on error*/
+int register_timer(timer_function f, void* param, unsigned int interval)
+{
+	struct sr_timer* t;
+
+	t=malloc(sizeof(struct sr_timer));
+	if (t==0){
+		LOG(L_ERR, "ERROR: register_timer: out of memory\n");
+		goto error;
+	}
+	t->id=timer_id++;
+	t->timer_f=f;
+	t->t_param=param;
+	t->interval=interval;
+	t->expires=jiffies+interval;
+	/* insert it into the list*/
+	t->next=timer_list;
+	timer_list=t;
+	return t->id;
+
+error:
+	return E_OUT_OF_MEM;
+}
+
+
+
+void timer_ticker()
+{
+	struct sr_timer* t;
+	unsigned int prev_jiffies;
+	
+	prev_jiffies=jiffies;
+	jiffies+=TIMER_TICK;
+	/* test for overflow (if tick= 1s =>overflow in 136 years)*/
+	if (jiffies<prev_jiffies){ 
+		/*force expire & update every timer, a little buggy but it 
+		 * happens once in 136 years :) */
+		for(t=timer_list;t;t=t->next){
+			t->expires=jiffies+t->interval;
+			t->timer_f(jiffies, t->t_param);
+		}
+		return;
+	}
+	
+	for (t=timer_list;t; t=t->next){
+		if (jiffies>=t->expires){
+			t->expires=jiffies+t->interval;
+			t->timer_f(jiffies, t->t_param);
+		}
+	}
+}

+ 37 - 0
timer.h

@@ -0,0 +1,37 @@
+/*
+ * $Id$
+ *
+ *
+ * timer related functions
+ */
+
+
+#ifndef timer_h
+#define timer_h
+
+typedef void (timer_function)(unsigned int ticks, void* param);
+
+
+struct sr_timer{
+	int id;
+	timer_function* timer_f;
+	void* t_param;
+	unsigned int interval;
+	
+	unsigned int expires;
+	
+	struct sr_timer* next;
+};
+
+
+
+extern struct sr_timer* timer_list;
+
+
+
+/*register a periodic timer;
+ * ret: <0 on errror*/
+int register_timer(timer_function f, void* param, unsigned int interval);
+void timer_ticker();
+
+#endif