4 * Userspace RCU library - test program (fork)
6 * Copyright February 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
35 #include <urcu/arch.h>
36 #include <urcu/tls-compat.h>
38 #ifndef DYNAMIC_LINK_TEST
41 #define rcu_debug_yield_read()
47 /* We generate children 3 levels deep */
49 /* Each generation spawns 10 children */
52 #define NR_TESTS NR_FORK
54 static int fork_generation
;
57 * Only print diagnostic for top level parent process, else the console
58 * has trouble formatting the tap output.
60 #define diag_gen0(...) \
62 if (!fork_generation) \
71 static void cb(struct rcu_head
*head
)
73 struct test_node
*node
;
75 diag_gen0("rcu callback invoked in pid: %d", (int) getpid());
76 node
= caa_container_of(head
, struct test_node
, head
);
80 static void test_rcu(void)
82 struct test_node
*node
;
84 rcu_register_thread();
91 node
= malloc(sizeof(*node
));
94 call_rcu(&node
->head
, cb
);
98 rcu_unregister_thread();
102 * Return 0 if child, > 0 if parent, < 0 on error.
104 static int do_fork(const char *execname
)
108 diag_gen0("%s parent pid: %d, before fork",
109 execname
, (int) getpid());
111 call_rcu_before_fork();
118 call_rcu_after_fork_child();
119 diag_gen0("%s child pid: %d, after fork",
120 execname
, (int) getpid());
122 diag_gen0("%s child pid: %d, after rcu test",
123 execname
, (int) getpid());
124 if (fork_generation
>= FORK_DEPTH
)
127 } else if (pid
> 0) {
131 call_rcu_after_fork_parent();
132 diag_gen0("%s parent pid: %d, after fork",
133 execname
, (int) getpid());
135 diag_gen0("%s parent pid: %d, after rcu test",
136 execname
, (int) getpid());
140 if (!fork_generation
)
144 if (WIFEXITED(status
)) {
145 diag_gen0("child %u exited normally with status %u",
146 pid
, WEXITSTATUS(status
));
147 if (WEXITSTATUS(status
))
150 } else if (WIFSIGNALED(status
)) {
151 diag_gen0("child %u was terminated by signal %u",
152 pid
, WTERMSIG(status
));
160 if (!fork_generation
)
166 int main(int argc
, char **argv
)
170 plan_tests(NR_TESTS
);
173 /* pthread_atfork does not work with malloc/free in callbacks */
174 ret
= pthread_atfork(call_rcu_before_fork
,
175 call_rcu_after_fork_parent
,
176 call_rcu_after_fork_child
);
179 perror("pthread_atfork");
185 for (i
= 0; i
< NR_FORK
; i
++) {
190 ret
= do_fork(argv
[0]);
191 if (!fork_generation
) {
192 ok(ret
>= 0, "child status %d", ret
);
194 if (ret
== 0) { /* child */
196 } else if (ret
< 0) {
199 /* else parent, continue. */
202 if (!fork_generation
) {
203 return exit_status();
209 if (!fork_generation
) {
210 return exit_status();