Просмотр исходного кода

core/timer: added support for basic mili-second timers

- renamed second-based timer functions from dummy to basic
Daniel-Constantin Mierla 13 лет назад
Родитель
Сommit
2f38f2c316
3 измененных файлов с 76 добавлено и 12 удалено
  1. 2 0
      timer.h
  2. 45 6
      timer_proc.c
  3. 29 6
      timer_proc.h

+ 2 - 0
timer.h

@@ -76,6 +76,8 @@ typedef void (timer_function)(unsigned int ticks, void* param);
 	timer_ticks.h (.e.g TICKS_TO_S(tick) to convert to s or ms )*/
 #define TIMER_TICK 1 /* 1 s, kept for compatibility */
 
+/*function prototype to execute on mili-second based basic timers */
+typedef void (utimer_function)(unsigned int uticks, void* param);
 
 struct timer_ln; /* forward decl */
 /* new 

+ 45 - 6
timer_proc.c

@@ -38,17 +38,18 @@
 #include "timer_proc.h"
 #include "cfg/cfg_struct.h"
 #include "pt.h"
+#include "ut.h"
 #include "mem/shm_mem.h"
 
 #include <unistd.h>
 
 
 /**
- * \brief update internal counters for running new dummy timers
- * @param timers number of dummy timer processes
+ * \brief update internal counters for running new basic sec. timers
+ * @param timers number of basic timer processes
  * @return 0 on success; -1 on error
  */
-int register_dummy_timers(int timers)
+int register_basic_timers(int timers)
 {
 	if(register_procs(timers)<0)
 		return -1;
@@ -61,9 +62,9 @@ int register_dummy_timers(int timers)
  * 
  * Forks a very basic periodic timer process, that just sleep()s for 
  * the specified interval and then calls the timer function.
- * The new "dummy timer" process execution start immediately, the sleep()
+ * The new "basic timer" process execution start immediately, the sleep()
  * is called first (so the first call to the timer function will happen
- * \<interval\> seconds after the call to fork_dummy_timer)
+ * \<interval\> seconds after the call to fork_basic_timer)
  * @param child_id  @see fork_process()
  * @param desc      @see fork_process()
  * @param make_sock @see fork_process()
@@ -73,7 +74,7 @@ int register_dummy_timers(int timers)
  * @return pid of the new process on success, -1 on error
  * (doesn't return anything in the child process)
  */
-int fork_dummy_timer(int child_id, char* desc, int make_sock,
+int fork_basic_timer(int child_id, char* desc, int make_sock,
 						timer_function* f, void* param, int interval)
 {
 	int pid;
@@ -94,6 +95,44 @@ int fork_dummy_timer(int child_id, char* desc, int make_sock,
 	return pid;
 }
 
+/**
+ * \brief Forks a separate simple milisecond-sleep() periodic timer
+ * 
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
+ * the specified interval and then calls the timer function.
+ * The new "basic timer" process execution start immediately, the ms-sleep()
+ * is called first (so the first call to the timer function will happen
+ * \<interval\> seconds after the call to fork_basic_utimer)
+ * @param child_id  @see fork_process()
+ * @param desc      @see fork_process()
+ * @param make_sock @see fork_process()
+ * @param f         timer function/callback
+ * @param param     parameter passed to the timer function
+ * @param uinterval  interval in mili-seconds.
+ * @return pid of the new process on success, -1 on error
+ * (doesn't return anything in the child process)
+ */
+int fork_basic_utimer(int child_id, char* desc, int make_sock,
+						utimer_function* f, void* param, int uinterval)
+{
+	int pid;
+	ticks_t ts;
+	
+	pid=fork_process(child_id, desc, make_sock);
+	if (pid<0) return -1;
+	if (pid==0){
+		/* child */
+		if (cfg_child_init()) return -1;
+		for(;;){
+			sleep_us(uinterval);
+			cfg_update();
+			ts = get_ticks_raw();
+			f(TICKS_TO_MS(ts), param); /* ticks in mili-seconds */
+		}
+	}
+	/* parent */
+	return pid;
+}
 
 
 /**

+ 29 - 6
timer_proc.h

@@ -35,21 +35,24 @@
 #include "local_timer.h"
 
 /**
- * \brief update internal counters for running new dummy timers
- * @param timers number of dummy timer processes
+ * \brief update internal counters for running new basic sec. timers
+ * @param timers number of basic timer processes
  * @return 0 on success; -1 on error
  */
-int register_dummy_timers(int timers);
+int register_basic_timers(int timers);
 
+#define register_dummy_timers register_basic_timers
+
+#define register_basic_utimers register_basic_utimers
 
 /**
  * \brief Forks a separate simple sleep() periodic timer
  * 
  * Forks a very basic periodic timer process, that just sleep()s for 
  * the specified interval and then calls the timer function.
- * The new "dummy timer" process execution start immediately, the sleep()
+ * The new "basic timer" process execution start immediately, the sleep()
  * is called first (so the first call to the timer function will happen
- * \<interval\> seconds after the call to fork_dummy_timer)
+ * \<interval\> seconds after the call to fork_basic_timer)
  * @param child_id  @see fork_process()
  * @param desc      @see fork_process()
  * @param make_sock @see fork_process()
@@ -59,10 +62,30 @@ int register_dummy_timers(int timers);
  * @return pid of the new process on success, -1 on error
  * (doesn't return anything in the child process)
  */
-int fork_dummy_timer(int child_id, char* desc, int make_sock,
+int fork_basic_timer(int child_id, char* desc, int make_sock,
 						timer_function* f, void* param, int interval);
 
+#define fork_dummy_timer fork_basic_timer
 
+/**
+ * \brief Forks a separate simple milisecond-sleep() periodic timer
+ * 
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
+ * the specified interval and then calls the timer function.
+ * The new "basic timer" process execution start immediately, the ms-sleep()
+ * is called first (so the first call to the timer function will happen
+ * \<interval\> seconds after the call to fork_basic_utimer)
+ * @param child_id  @see fork_process()
+ * @param desc      @see fork_process()
+ * @param make_sock @see fork_process()
+ * @param f         timer function/callback
+ * @param param     parameter passed to the timer function
+ * @param uinterval  interval in mili-seconds.
+ * @return pid of the new process on success, -1 on error
+ * (doesn't return anything in the child process)
+ */
+int fork_basic_utimer(int child_id, char* desc, int make_sock,
+						timer_function* f, void* param, int uinterval);
 /**
  * \brief Forks a timer process based on the local timer
  *