Browse code

timers: helpers for forking separate timer procs

Helper functions for forking simple separate timer processes:

fork_dummy_timer(...) - forks a process that will just sleep()
for the required interval and then will call the provided
timer function, all that in a loop.
Care must be taken to use it only when fork_process() is
allowed (e.g.: module child_init function, when
rank==PROC_MAIN; see doc/modules_init.txt for more details)
It can be used to replace kamailio register_timer_process()
(but note that it's not a "drop-in" replacement).

fork_local_timer_process() - forks a process, initializes a
local_timer for it, and returns the local_timer handler both
to the parent and to the child process, so both of them can
add/del timers (if appropriate locking is used).
Example usage:
struct local_timer* lt_h;

pid=fork_local_timer_process(...., &lt_h);
if (pid==0){
timer_init(&my_timer1, my_timer_f1, 0, 0);
...
local_timer_add(&lt_h, &my_timer, S_TO_TICKS(10), get_ticks_raw());
...
while(1) { sleep(1); local_timer_run(lt, get_ticks_raw()); }
}
(note that instead of sleep()-ing the process can do some useful work,
like polling some fds a.s.o.)

Andrei Pelinescu-Onciul authored on 10/03/2009 21:35:33
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,116 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2009 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * timer_proc.c  - separate process timers
19
+ * (unrelated to the main fast and slow timers)
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2009-03-10  initial version (andrei)
25
+*/
26
+
27
+#include "timer_proc.h"
28
+#include "pt.h"
29
+#include "mem/shm_mem.h"
30
+
31
+#include <unistd.h>
32
+
33
+
34
+/** forks a separate simple sleep() periodic timer.
35
+  * Forks a very basic periodic timer process, that just sleep()s for 
36
+  * the specified interval and then calls the timer function.
37
+  * The new "dummy timer" process execution start immediately, the sleep()
38
+  * is called first (so the first call to the timer function will happen
39
+  * <interval> seconds after the call to fork_dummy_timer)
40
+  * @param child_id - @see fork_process()
41
+  * @param desc     - @see fork_process()
42
+  * @param make_sock - @see fork_process()
43
+  * @param f         - timer function/callback
44
+  * @param param     - parameter passed to the timer function
45
+  * @param interval  - interval in seconds.
46
+  * @return - pid of the new process on success, -1 on error
47
+  *           (doesn't return anything in the child process)
48
+  */
49
+int fork_dummy_timer(int child_id, char* desc, int make_sock,
50
+						timer_function* f, void* param, int interval)
51
+{
52
+	int pid;
53
+	
54
+	pid=fork_process(child_id, desc, make_sock);
55
+	if (pid<0) return -1;
56
+	if (pid==0){
57
+		/* child */
58
+		for(;;){
59
+			sleep(interval);
60
+			f(get_ticks(), param); /* ticks in s for compatibility with old
61
+									  timers */
62
+		}
63
+	}
64
+	/* parent */
65
+	return pid;
66
+}
67
+
68
+
69
+
70
+/** forks a timer process based on the local timer.
71
+ *  Forks a separate timer process running a local_timer.h type of timer
72
+ *  A pointer to the local_timer handle (allocated in shared memory) is
73
+ *  returned in lt_h. It can be used to add/delete more timers at runtime
74
+ *  (via local_timer_add()/local_timer_del() a.s.o).
75
+ *  If timers are added from separate processes, some form of locking must be
76
+ *  used (all the calls to local_timer* must be enclosed by locks if it
77
+ *  cannot be guaranteed that they cannot execute in the same time)
78
+ *  The timer "engine" must be run manually from the child process. For
79
+ *  example a very simple local timer process that just runs a single 
80
+ *  periodic timer can be started in the following way:
81
+ *      struct local_timer* lt_h;
82
+ *
83
+ *      pid=fork_local_timer_process(...., &lt_h);
84
+ *      if (pid==0){
85
+ *          timer_init(&my_timer, my_timer_f, 0, 0);
86
+ *          local_timer_add(&lt_h, &my_timer, S_TO_TICKS(10), get_ticks_raw());
87
+ *          while(1) { sleep(1); local_timer_run(lt, get_ticks_raw()); }
88
+ *      }
89
+ *
90
+ * @param child_id - @see fork_process()
91
+ * @param desc     - @see fork_process()
92
+ * @param make_sock - @see fork_process()
93
+ * @param lt_h      - local_timer handler
94
+ * @return - pid to the parent, 0 to the child, -1 if error.
95
+ */
96
+int fork_local_timer_process(int child_id, char* desc, int make_sock,
97
+						struct local_timer** lt_h)
98
+{
99
+	int pid;
100
+	struct local_timer* lt;
101
+	
102
+	lt=shm_malloc(sizeof(*lt));
103
+	if (lt==0) goto error;
104
+	if (init_local_timer(lt, get_ticks_raw())<0) goto error;
105
+	pid=fork_process(child_id, desc, make_sock);
106
+	if (pid<0) goto error;
107
+	*lt_h=lt;
108
+	return pid;
109
+error:
110
+	if (lt) shm_free(lt);
111
+	return -1;
112
+}
113
+
114
+
115
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
0 116
new file mode 100644
... ...
@@ -0,0 +1,43 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2009 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * timer_proc.h - separate process timers
19
+ * (unrelated to the main fast and slow timers)
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2009-03-10  initial version (andrei)
25
+*/
26
+
27
+#ifndef __timer_proc_h
28
+#define __timer_proc_h
29
+
30
+#include "local_timer.h"
31
+
32
+/* forks a separate simple sleep() periodic timer */
33
+int fork_dummy_timer(int child_id, char* desc, int make_sock,
34
+						timer_function* f, void* param, int interval);
35
+
36
+/* forks a timer process based on the local timer */
37
+int fork_local_timer_process(int child_id, char* desc, int make_sock,
38
+						struct local_timer** lt_h);
39
+
40
+#endif /*__timer_proc_h*/
41
+
42
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */