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