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()
47 /* We generate children 3 levels deep */
49 /* Each generation spawns 10 children */
52 static int fork_generation
;
59 static void cb(struct rcu_head
*head
)
61 struct test_node
*node
;
63 fprintf(stderr
, "rcu callback invoked in pid: %d\n",
65 node
= caa_container_of(head
, struct test_node
, head
);
69 static void test_rcu(void)
71 struct test_node
*node
;
73 rcu_register_thread();
80 node
= malloc(sizeof(*node
));
83 call_rcu(&node
->head
, cb
);
87 rcu_unregister_thread();
91 * Return 0 if child, > 0 if parent, < 0 on error.
93 static int do_fork(const char *execname
)
97 fprintf(stderr
, "%s parent pid: %d, before fork\n",
98 execname
, (int) getpid());
100 call_rcu_before_fork();
106 call_rcu_after_fork_child();
107 fprintf(stderr
, "%s child pid: %d, after fork\n",
108 execname
, (int) getpid());
110 fprintf(stderr
, "%s child pid: %d, after rcu test\n",
111 execname
, (int) getpid());
112 if (fork_generation
>= FORK_DEPTH
)
115 } else if (pid
> 0) {
119 call_rcu_after_fork_parent();
120 fprintf(stderr
, "%s parent pid: %d, after fork\n",
121 execname
, (int) getpid());
123 fprintf(stderr
, "%s parent pid: %d, after rcu test\n",
124 execname
, (int) getpid());
131 if (WIFEXITED(status
)) {
132 fprintf(stderr
, "child %u exited normally with status %u\n",
133 pid
, WEXITSTATUS(status
));
134 if (WEXITSTATUS(status
))
137 } else if (WIFSIGNALED(status
)) {
138 fprintf(stderr
, "child %u was terminated by signal %u\n",
139 pid
, WTERMSIG(status
));
152 int main(int argc
, char **argv
)
157 /* pthread_atfork does not work with malloc/free in callbacks */
158 ret
= pthread_atfork(call_rcu_before_fork
,
159 call_rcu_after_fork_parent
,
160 call_rcu_after_fork_child
);
163 perror("pthread_atfork");
169 for (i
= 0; i
< NR_FORK
; i
++) {
174 ret
= do_fork(argv
[0]);
175 if (ret
== 0) /* child */
179 /* else parent, continue. */