Browse code

core: added support for basic timer + interval sync

- new functions to start basic timers that will sync the interval after
executing the task. Useful if the task is taking long, the process
will skip the delay in the sleep value

Daniel-Constantin Mierla authored on 19/04/2012 08:52:02
Showing 2 changed files
... ...
@@ -181,5 +181,106 @@ error:
181 181
 	return -1;
182 182
 }
183 183
 
184
+/**
185
+ * \brief update internal counters for running new sync sec. timers
186
+ * @param timers number of basic timer processes
187
+ * @return 0 on success; -1 on error
188
+ */
189
+int register_sync_timers(int timers)
190
+{
191
+	if(register_procs(timers)<0)
192
+		return -1;
193
+	cfg_register_child(timers);
194
+	return 0;
195
+}
196
+
197
+/**
198
+ * \brief Forks a separate simple sleep() -&- sync periodic timer
199
+ *
200
+ * Forks a very basic periodic timer process, that just sleep()s for 
201
+ * the specified interval and then calls the timer function.
202
+ * The new "sync timer" process execution start immediately, the sleep()
203
+ * is called first (so the first call to the timer function will happen
204
+ * \<interval\> seconds after the call to fork_sync_timer)
205
+ * @param child_id  @see fork_process()
206
+ * @param desc      @see fork_process()
207
+ * @param make_sock @see fork_process()
208
+ * @param f         timer function/callback
209
+ * @param param     parameter passed to the timer function
210
+ * @param interval  interval in seconds.
211
+ * @return pid of the new process on success, -1 on error
212
+ * (doesn't return anything in the child process)
213
+ */
214
+int fork_sync_timer(int child_id, char* desc, int make_sock,
215
+						timer_function* f, void* param, int interval)
216
+{
217
+	int pid;
218
+	ticks_t ts1 = 0;
219
+	ticks_t ts2 = 0;
220
+
221
+	pid=fork_process(child_id, desc, make_sock);
222
+	if (pid<0) return -1;
223
+	if (pid==0){
224
+		/* child */
225
+		ts2 = interval;
226
+		if (cfg_child_init()) return -1;
227
+		for(;;){
228
+			if(ts2>0) sleep(ts2);
229
+			else sleep(1);
230
+			ts1 = get_ticks();
231
+			cfg_update();
232
+			f(get_ticks(), param); /* ticks in s for compatibility with old
233
+									  timers */
234
+			ts2 = interval - get_ticks() + ts1;
235
+		}
236
+	}
237
+	/* parent */
238
+	return pid;
239
+}
240
+
241
+
242
+/**
243
+ * \brief Forks a separate simple milisecond-sleep() -&- sync periodic timer
244
+ *
245
+ * Forks a very basic periodic timer process, that just ms-sleep()s for 
246
+ * the specified interval and then calls the timer function.
247
+ * The new "sync timer" process execution start immediately, the ms-sleep()
248
+ * is called first (so the first call to the timer function will happen
249
+ * \<interval\> seconds after the call to fork_basic_utimer)
250
+ * @param child_id  @see fork_process()
251
+ * @param desc      @see fork_process()
252
+ * @param make_sock @see fork_process()
253
+ * @param f         timer function/callback
254
+ * @param param     parameter passed to the timer function
255
+ * @param uinterval  interval in mili-seconds.
256
+ * @return pid of the new process on success, -1 on error
257
+ * (doesn't return anything in the child process)
258
+ */
259
+int fork_sync_utimer(int child_id, char* desc, int make_sock,
260
+						utimer_function* f, void* param, int uinterval)
261
+{
262
+	int pid;
263
+	ticks_t ts1 = 0;
264
+	ticks_t ts2 = 0;
265
+
266
+	pid=fork_process(child_id, desc, make_sock);
267
+	if (pid<0) return -1;
268
+	if (pid==0){
269
+		/* child */
270
+		ts2 = uinterval;
271
+		if (cfg_child_init()) return -1;
272
+		for(;;){
273
+			if(ts2>0) sleep_us(uinterval);
274
+			else sleep_us(1);
275
+			ts1 = get_ticks_raw();
276
+			cfg_update();
277
+			f(TICKS_TO_MS(ts1), param); /* ticks in mili-seconds */
278
+			ts2 = uinterval - get_ticks_raw() + ts1;
279
+		}
280
+	}
281
+	/* parent */
282
+	return pid;
283
+}
284
+
184 285
 
185 286
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */
... ...
@@ -117,6 +117,17 @@ int fork_basic_utimer(int child_id, char* desc, int make_sock,
117 117
 int fork_local_timer_process(int child_id, char* desc, int make_sock,
118 118
 						struct local_timer** lt_h);
119 119
 
120
+/**
121
+ * sync timers
122
+ */
123
+int register_sync_timers(int timers);
124
+
125
+int fork_sync_timer(int child_id, char* desc, int make_sock,
126
+						timer_function* f, void* param, int interval);
127
+
128
+int fork_sync_utimer(int child_id, char* desc, int make_sock,
129
+						utimer_function* f, void* param, int uinterval);
130
+
120 131
 #endif /*__timer_proc_h*/
121 132
 
122 133
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */