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 62
  * 
62 63
  * Forks a very basic periodic timer process, that just sleep()s for 
63 64
  * the specified interval and then calls the timer function.
64
- * The new "dummy timer" process execution start immediately, the sleep()
65
+ * The new "basic timer" process execution start immediately, the sleep()
65 66
  * is called first (so the first call to the timer function will happen
66
- * \<interval\> seconds after the call to fork_dummy_timer)
67
+ * \<interval\> seconds after the call to fork_basic_timer)
67 68
  * @param child_id  @see fork_process()
68 69
  * @param desc      @see fork_process()
69 70
  * @param make_sock @see fork_process()
... ...
@@ -73,7 +74,7 @@ int register_dummy_timers(int timers)
73 74
  * @return pid of the new process on success, -1 on error
74 75
  * (doesn't return anything in the child process)
75 76
  */
76
-int fork_dummy_timer(int child_id, char* desc, int make_sock,
77
+int fork_basic_timer(int child_id, char* desc, int make_sock,
77 78
 						timer_function* f, void* param, int interval)
78 79
 {
79 80
 	int pid;
... ...
@@ -94,6 +95,44 @@ int fork_dummy_timer(int child_id, char* desc, int make_sock,
94 95
 	return pid;
95 96
 }
96 97
 
98
+/**
99
+ * \brief Forks a separate simple milisecond-sleep() periodic timer
100
+ * 
101
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
102
+ * the specified interval and then calls the timer function.
103
+ * The new "basic timer" process execution start immediately, the ms-sleep()
104
+ * is called first (so the first call to the timer function will happen
105
+ * \<interval\> seconds after the call to fork_basic_utimer)
106
+ * @param child_id  @see fork_process()
107
+ * @param desc      @see fork_process()
108
+ * @param make_sock @see fork_process()
109
+ * @param f         timer function/callback
110
+ * @param param     parameter passed to the timer function
111
+ * @param uinterval  interval in mili-seconds.
112
+ * @return pid of the new process on success, -1 on error
113
+ * (doesn't return anything in the child process)
114
+ */
115
+int fork_basic_utimer(int child_id, char* desc, int make_sock,
116
+						utimer_function* f, void* param, int uinterval)
117
+{
118
+	int pid;
119
+	ticks_t ts;
120
+	
121
+	pid=fork_process(child_id, desc, make_sock);
122
+	if (pid<0) return -1;
123
+	if (pid==0){
124
+		/* child */
125
+		if (cfg_child_init()) return -1;
126
+		for(;;){
127
+			sleep_us(uinterval);
128
+			cfg_update();
129
+			ts = get_ticks_raw();
130
+			f(TICKS_TO_MS(ts), param); /* ticks in mili-seconds */
131
+		}
132
+	}
133
+	/* parent */
134
+	return pid;
135
+}
97 136
 
98 137
 
99 138
 /**
... ...
@@ -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,10 +62,30 @@ int register_dummy_timers(int timers);
59 62
  * @return pid of the new process on success, -1 on error
60 63
  * (doesn't return anything in the child process)
61 64
  */
62
-int fork_dummy_timer(int child_id, char* desc, int make_sock,
65
+int fork_basic_timer(int child_id, char* desc, int make_sock,
63 66
 						timer_function* f, void* param, int interval);
64 67
 
68
+#define fork_dummy_timer fork_basic_timer
65 69
 
70
+/**
71
+ * \brief Forks a separate simple milisecond-sleep() periodic timer
72
+ * 
73
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
74
+ * the specified interval and then calls the timer function.
75
+ * The new "basic timer" process execution start immediately, the ms-sleep()
76
+ * is called first (so the first call to the timer function will happen
77
+ * \<interval\> seconds after the call to fork_basic_utimer)
78
+ * @param child_id  @see fork_process()
79
+ * @param desc      @see fork_process()
80
+ * @param make_sock @see fork_process()
81
+ * @param f         timer function/callback
82
+ * @param param     parameter passed to the timer function
83
+ * @param uinterval  interval in mili-seconds.
84
+ * @return pid of the new process on success, -1 on error
85
+ * (doesn't return anything in the child process)
86
+ */
87
+int fork_basic_utimer(int child_id, char* desc, int make_sock,
88
+						timer_function* f, void* param, int uinterval);
66 89
 /**
67 90
  * \brief Forks a timer process based on the local timer
68 91
  *