Browse code

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

- renamed second-based timer functions from dummy to basic

Daniel-Constantin Mierla authored on 14/12/2011 22:18:36
Showing 3 changed files
... ...
@@ -76,6 +76,8 @@ typedef void (timer_function)(unsigned int ticks, void* param);
76 76
 	timer_ticks.h (.e.g TICKS_TO_S(tick) to convert to s or ms )*/
77 77
 #define TIMER_TICK 1 /* 1 s, kept for compatibility */
78 78
 
79
+/*function prototype to execute on mili-second based basic timers */
80
+typedef void (utimer_function)(unsigned int uticks, void* param);
79 81
 
80 82
 struct timer_ln; /* forward decl */
81 83
 /* new 
... ...
@@ -38,17 +38,18 @@
38 38
 #include "timer_proc.h"
39 39
 #include "cfg/cfg_struct.h"
40 40
 #include "pt.h"
41
+#include "ut.h"
41 42
 #include "mem/shm_mem.h"
42 43
 
43 44
 #include <unistd.h>
44 45
 
45 46
 
46 47
 /**
47
- * \brief update internal counters for running new dummy timers
48
- * @param timers number of dummy timer processes
48
+ * \brief update internal counters for running new basic sec. timers
49
+ * @param timers number of basic timer processes
49 50
  * @return 0 on success; -1 on error
50 51
  */
51
-int register_dummy_timers(int timers)
52
+int register_basic_timers(int timers)
52 53
 {
53 54
 	if(register_procs(timers)<0)
54 55
 		return -1;
... ...
@@ -61,9 +62,9 @@ int register_dummy_timers(int timers)
61 61
  * 
62 62
  * Forks a very basic periodic timer process, that just sleep()s for 
63 63
  * the specified interval and then calls the timer function.
64
- * The new "dummy timer" process execution start immediately, the sleep()
64
+ * The new "basic timer" process execution start immediately, the sleep()
65 65
  * is called first (so the first call to the timer function will happen
66
- * \<interval\> seconds after the call to fork_dummy_timer)
66
+ * \<interval\> seconds after the call to fork_basic_timer)
67 67
  * @param child_id  @see fork_process()
68 68
  * @param desc      @see fork_process()
69 69
  * @param make_sock @see fork_process()
... ...
@@ -73,7 +74,7 @@ int register_dummy_timers(int timers)
73 73
  * @return pid of the new process on success, -1 on error
74 74
  * (doesn't return anything in the child process)
75 75
  */
76
-int fork_dummy_timer(int child_id, char* desc, int make_sock,
76
+int fork_basic_timer(int child_id, char* desc, int make_sock,
77 77
 						timer_function* f, void* param, int interval)
78 78
 {
79 79
 	int pid;
... ...
@@ -94,6 +95,44 @@ int fork_dummy_timer(int child_id, char* desc, int make_sock,
94 94
 	return pid;
95 95
 }
96 96
 
97
+/**
98
+ * \brief Forks a separate simple milisecond-sleep() periodic timer
99
+ * 
100
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
101
+ * the specified interval and then calls the timer function.
102
+ * The new "basic timer" process execution start immediately, the ms-sleep()
103
+ * is called first (so the first call to the timer function will happen
104
+ * \<interval\> seconds after the call to fork_basic_utimer)
105
+ * @param child_id  @see fork_process()
106
+ * @param desc      @see fork_process()
107
+ * @param make_sock @see fork_process()
108
+ * @param f         timer function/callback
109
+ * @param param     parameter passed to the timer function
110
+ * @param uinterval  interval in mili-seconds.
111
+ * @return pid of the new process on success, -1 on error
112
+ * (doesn't return anything in the child process)
113
+ */
114
+int fork_basic_utimer(int child_id, char* desc, int make_sock,
115
+						utimer_function* f, void* param, int uinterval)
116
+{
117
+	int pid;
118
+	ticks_t ts;
119
+	
120
+	pid=fork_process(child_id, desc, make_sock);
121
+	if (pid<0) return -1;
122
+	if (pid==0){
123
+		/* child */
124
+		if (cfg_child_init()) return -1;
125
+		for(;;){
126
+			sleep_us(uinterval);
127
+			cfg_update();
128
+			ts = get_ticks_raw();
129
+			f(TICKS_TO_MS(ts), param); /* ticks in mili-seconds */
130
+		}
131
+	}
132
+	/* parent */
133
+	return pid;
134
+}
97 135
 
98 136
 
99 137
 /**
... ...
@@ -35,21 +35,24 @@
35 35
 #include "local_timer.h"
36 36
 
37 37
 /**
38
- * \brief update internal counters for running new dummy timers
39
- * @param timers number of dummy timer processes
38
+ * \brief update internal counters for running new basic sec. timers
39
+ * @param timers number of basic timer processes
40 40
  * @return 0 on success; -1 on error
41 41
  */
42
-int register_dummy_timers(int timers);
42
+int register_basic_timers(int timers);
43 43
 
44
+#define register_dummy_timers register_basic_timers
45
+
46
+#define register_basic_utimers register_basic_utimers
44 47
 
45 48
 /**
46 49
  * \brief Forks a separate simple sleep() periodic timer
47 50
  * 
48 51
  * Forks a very basic periodic timer process, that just sleep()s for 
49 52
  * the specified interval and then calls the timer function.
50
- * The new "dummy timer" process execution start immediately, the sleep()
53
+ * The new "basic timer" process execution start immediately, the sleep()
51 54
  * is called first (so the first call to the timer function will happen
52
- * \<interval\> seconds after the call to fork_dummy_timer)
55
+ * \<interval\> seconds after the call to fork_basic_timer)
53 56
  * @param child_id  @see fork_process()
54 57
  * @param desc      @see fork_process()
55 58
  * @param make_sock @see fork_process()
... ...
@@ -59,11 +62,31 @@ int register_dummy_timers(int timers);
59 59
  * @return pid of the new process on success, -1 on error
60 60
  * (doesn't return anything in the child process)
61 61
  */
62
-int fork_dummy_timer(int child_id, char* desc, int make_sock,
62
+int fork_basic_timer(int child_id, char* desc, int make_sock,
63 63
 						timer_function* f, void* param, int interval);
64 64
 
65
+#define fork_dummy_timer fork_basic_timer
65 66
 
66 67
 /**
68
+ * \brief Forks a separate simple milisecond-sleep() periodic timer
69
+ * 
70
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
71
+ * the specified interval and then calls the timer function.
72
+ * The new "basic timer" process execution start immediately, the ms-sleep()
73
+ * is called first (so the first call to the timer function will happen
74
+ * \<interval\> seconds after the call to fork_basic_utimer)
75
+ * @param child_id  @see fork_process()
76
+ * @param desc      @see fork_process()
77
+ * @param make_sock @see fork_process()
78
+ * @param f         timer function/callback
79
+ * @param param     parameter passed to the timer function
80
+ * @param uinterval  interval in mili-seconds.
81
+ * @return pid of the new process on success, -1 on error
82
+ * (doesn't return anything in the child process)
83
+ */
84
+int fork_basic_utimer(int child_id, char* desc, int make_sock,
85
+						timer_function* f, void* param, int uinterval);
86
+/**
67 87
  * \brief Forks a timer process based on the local timer
68 88
  * 
69 89
  * Forks a separate timer process running a local_timer.h type of timer