Fix: cast caa_cycles_t to unsigned long long
[urcu.git] / tests / regression / test_urcu_fork.c
CommitLineData
390f0a26
MD
1/*
2 * test_urcu_fork.c
3 *
4 * Userspace RCU library - test program (fork)
5 *
6 * Copyright February 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23#define _GNU_SOURCE
f5ab766e 24#include "config.h"
390f0a26
MD
25#include <stdio.h>
26#include <pthread.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
32#include <stdio.h>
33#include <assert.h>
34#include <sched.h>
35#include <errno.h>
36
37#include <urcu/arch.h>
38#include <urcu/tls-compat.h>
39
40#ifndef DYNAMIC_LINK_TEST
41#define _LGPL_SOURCE
42#else
43#define rcu_debug_yield_read()
44#endif
45#include <urcu.h>
46
d6aefcd1
MD
47/* We generate children 3 levels deep */
48#define FORK_DEPTH 3
49/* Each generation spawns 10 children */
50#define NR_FORK 10
51
52static int fork_generation;
53
390f0a26
MD
54struct test_node {
55 int somedata;
56 struct rcu_head head;
57};
58
59static void cb(struct rcu_head *head)
60{
61 struct test_node *node;
62
63 fprintf(stderr, "rcu callback invoked in pid: %d\n",
64 (int) getpid());
65 node = caa_container_of(head, struct test_node, head);
66 free(node);
67}
68
69static void test_rcu(void)
70{
71 struct test_node *node;
72
73 rcu_register_thread();
74
75 synchronize_rcu();
76
77 rcu_read_lock();
78 rcu_read_unlock();
79
80 node = malloc(sizeof(*node));
81 assert(node);
82
83 call_rcu(&node->head, cb);
84
85 synchronize_rcu();
86
87 rcu_unregister_thread();
88}
89
d6aefcd1
MD
90/*
91 * Return 0 if child, > 0 if parent, < 0 on error.
92 */
93static int do_fork(const char *execname)
390f0a26
MD
94{
95 pid_t pid;
390f0a26
MD
96
97 fprintf(stderr, "%s parent pid: %d, before fork\n",
d6aefcd1 98 execname, (int) getpid());
390f0a26
MD
99
100 call_rcu_before_fork();
101 pid = fork();
390f0a26
MD
102 if (pid == 0) {
103 /* child */
d6aefcd1
MD
104 fork_generation++;
105
390f0a26
MD
106 call_rcu_after_fork_child();
107 fprintf(stderr, "%s child pid: %d, after fork\n",
d6aefcd1 108 execname, (int) getpid());
390f0a26
MD
109 test_rcu();
110 fprintf(stderr, "%s child pid: %d, after rcu test\n",
d6aefcd1
MD
111 execname, (int) getpid());
112 if (fork_generation >= FORK_DEPTH)
113 exit(EXIT_SUCCESS);
114 return 0;
390f0a26
MD
115 } else if (pid > 0) {
116 int status;
117
118 /* parent */
119 call_rcu_after_fork_parent();
120 fprintf(stderr, "%s parent pid: %d, after fork\n",
d6aefcd1 121 execname, (int) getpid());
390f0a26
MD
122 test_rcu();
123 fprintf(stderr, "%s parent pid: %d, after rcu test\n",
d6aefcd1 124 execname, (int) getpid());
390f0a26
MD
125 for (;;) {
126 pid = wait(&status);
d6aefcd1
MD
127 if (pid < 0) {
128 perror("wait");
129 return -1;
130 }
390f0a26
MD
131 if (WIFEXITED(status)) {
132 fprintf(stderr, "child %u exited normally with status %u\n",
133 pid, WEXITSTATUS(status));
d6aefcd1
MD
134 if (WEXITSTATUS(status))
135 return -1;
390f0a26
MD
136 break;
137 } else if (WIFSIGNALED(status)) {
138 fprintf(stderr, "child %u was terminated by signal %u\n",
139 pid, WTERMSIG(status));
d6aefcd1 140 return -1;
390f0a26
MD
141 } else {
142 continue;
143 }
144 }
d6aefcd1 145 return 1;
390f0a26
MD
146 } else {
147 perror("fork");
d6aefcd1
MD
148 return -1;
149 }
150}
151
152int main(int argc, char **argv)
153{
154 unsigned int i;
155
156#if 0
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);
161 if (ret) {
162 errno = ret;
163 perror("pthread_atfork");
390f0a26
MD
164 exit(EXIT_FAILURE);
165 }
d6aefcd1
MD
166#endif
167
168restart:
169 for (i = 0; i < NR_FORK; i++) {
170 int ret;
171
172 test_rcu();
173 synchronize_rcu();
174 ret = do_fork(argv[0]);
175 if (ret == 0) /* child */
176 goto restart;
177 else if (ret < 0)
178 goto error;
179 /* else parent, continue. */
180 }
390f0a26 181 exit(EXIT_SUCCESS);
d6aefcd1
MD
182
183error:
184 exit(EXIT_FAILURE);
390f0a26 185}
This page took 0.035166 seconds and 4 git commands to generate.