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.
29 #include <sys/types.h>
37 #include <urcu/arch.h>
38 #include <urcu/tls-compat.h>
40 #ifndef DYNAMIC_LINK_TEST
43 #define rcu_debug_yield_read()
49 /* We generate children 3 levels deep */
51 /* Each generation spawns 10 children */
54 #define NR_TESTS NR_FORK
56 static int fork_generation
;
59 * Only print diagnostic for top level parent process, else the console
60 * has trouble formatting the tap output.
62 #define diag_gen0(...) \
64 if (!fork_generation) \
73 static void cb(struct rcu_head
*head
)
75 struct test_node
*node
;
77 diag_gen0("rcu callback invoked in pid: %d", (int) getpid());
78 node
= caa_container_of(head
, struct test_node
, head
);
82 static void test_rcu(void)
84 struct test_node
*node
;
86 rcu_register_thread();
93 node
= malloc(sizeof(*node
));
96 call_rcu(&node
->head
, cb
);
100 rcu_unregister_thread();
104 * Return 0 if child, > 0 if parent, < 0 on error.
106 static int do_fork(const char *execname
)
110 diag_gen0("%s parent pid: %d, before fork",
111 execname
, (int) getpid());
113 call_rcu_before_fork();
120 call_rcu_after_fork_child();
121 diag_gen0("%s child pid: %d, after fork",
122 execname
, (int) getpid());
124 diag_gen0("%s child pid: %d, after rcu test",
125 execname
, (int) getpid());
126 if (fork_generation
>= FORK_DEPTH
)
129 } else if (pid
> 0) {
133 call_rcu_after_fork_parent();
134 diag_gen0("%s parent pid: %d, after fork",
135 execname
, (int) getpid());
137 diag_gen0("%s parent pid: %d, after rcu test",
138 execname
, (int) getpid());
142 if (!fork_generation
)
146 if (WIFEXITED(status
)) {
147 diag_gen0("child %u exited normally with status %u",
148 pid
, WEXITSTATUS(status
));
149 if (WEXITSTATUS(status
))
152 } else if (WIFSIGNALED(status
)) {
153 diag_gen0("child %u was terminated by signal %u",
154 pid
, WTERMSIG(status
));
162 if (!fork_generation
)
168 int main(int argc
, char **argv
)
172 plan_tests(NR_TESTS
);
175 /* pthread_atfork does not work with malloc/free in callbacks */
176 ret
= pthread_atfork(call_rcu_before_fork
,
177 call_rcu_after_fork_parent
,
178 call_rcu_after_fork_child
);
181 perror("pthread_atfork");
187 for (i
= 0; i
< NR_FORK
; i
++) {
192 ret
= do_fork(argv
[0]);
193 if (!fork_generation
) {
194 ok(ret
>= 0, "child status %d", ret
);
196 if (ret
== 0) { /* child */
198 } else if (ret
< 0) {
201 /* else parent, continue. */
204 if (!fork_generation
) {
205 return exit_status();
211 if (!fork_generation
) {
212 return exit_status();