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