Test fix: 0.6 branch does not have tls-compat.h
[userspace-rcu.git] / tests / test_urcu_fork.c
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
24 #include "../config.h"
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
39 #ifndef DYNAMIC_LINK_TEST
40 #define _LGPL_SOURCE
41 #else
42 #define rcu_debug_yield_read()
43 #endif
44 #include <urcu.h>
45
46 struct test_node {
47 int somedata;
48 struct rcu_head head;
49 };
50
51 static void cb(struct rcu_head *head)
52 {
53 struct test_node *node;
54
55 fprintf(stderr, "rcu callback invoked in pid: %d\n",
56 (int) getpid());
57 node = caa_container_of(head, struct test_node, head);
58 free(node);
59 }
60
61 static void test_rcu(void)
62 {
63 struct test_node *node;
64
65 rcu_register_thread();
66
67 synchronize_rcu();
68
69 rcu_read_lock();
70 rcu_read_unlock();
71
72 node = malloc(sizeof(*node));
73 assert(node);
74
75 call_rcu(&node->head, cb);
76
77 synchronize_rcu();
78
79 rcu_unregister_thread();
80 }
81
82 int main(int argc, char **argv)
83 {
84 pid_t pid;
85 int ret;
86
87 #if 0
88 /* pthread_atfork does not work with malloc/free in callbacks */
89 ret = pthread_atfork(call_rcu_before_fork,
90 call_rcu_after_fork_parent,
91 call_rcu_after_fork_child);
92 if (ret) {
93 errno = ret;
94 perror("pthread_atfork");
95 exit(EXIT_FAILURE);
96 }
97 #endif
98
99 test_rcu();
100
101 synchronize_rcu();
102
103 fprintf(stderr, "%s parent pid: %d, before fork\n",
104 argv[0], (int) getpid());
105
106 call_rcu_before_fork();
107 pid = fork();
108
109 if (pid == 0) {
110 /* child */
111 call_rcu_after_fork_child();
112 fprintf(stderr, "%s child pid: %d, after fork\n",
113 argv[0], (int) getpid());
114 test_rcu();
115 fprintf(stderr, "%s child pid: %d, after rcu test\n",
116 argv[0], (int) getpid());
117 } else if (pid > 0) {
118 int status;
119
120 /* parent */
121 call_rcu_after_fork_parent();
122 fprintf(stderr, "%s parent pid: %d, after fork\n",
123 argv[0], (int) getpid());
124 test_rcu();
125 fprintf(stderr, "%s parent pid: %d, after rcu test\n",
126 argv[0], (int) getpid());
127 for (;;) {
128 pid = wait(&status);
129 if (WIFEXITED(status)) {
130 fprintf(stderr, "child %u exited normally with status %u\n",
131 pid, WEXITSTATUS(status));
132 break;
133 } else if (WIFSIGNALED(status)) {
134 fprintf(stderr, "child %u was terminated by signal %u\n",
135 pid, WTERMSIG(status));
136 break;
137 } else {
138 continue;
139 }
140 }
141 } else {
142 perror("fork");
143 exit(EXIT_FAILURE);
144 }
145 exit(EXIT_SUCCESS);
146 }
This page took 0.031436 seconds and 4 git commands to generate.