Enhance test cases
[urcu.git] / urcu.c
... / ...
CommitLineData
1/*
2 * urcu.c
3 *
4 * Userspace RCU library
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * Distributed under GPLv2
9 */
10
11#include <stdio.h>
12#include <pthread.h>
13#include <signal.h>
14#include <assert.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "urcu.h"
19
20pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
21
22/*
23 * Global grace period counter.
24 * Contains the current RCU_GP_CTR_BIT.
25 * Also has a RCU_GP_CTR_BIT of 1, to accelerate the reader fast path.
26 */
27long urcu_gp_ctr = RCU_GP_COUNT;
28
29long __thread urcu_active_readers;
30
31/* Thread IDs of registered readers */
32#define INIT_NUM_THREADS 4
33
34struct reader_data {
35 pthread_t tid;
36 long *urcu_active_readers;
37};
38
39#ifdef DEBUG_YIELD
40unsigned int yield_active;
41unsigned int __thread rand_yield;
42#endif
43
44static struct reader_data *reader_data;
45static int num_readers, alloc_readers;
46static int sig_done;
47
48void internal_urcu_lock(void)
49{
50 int ret;
51 ret = pthread_mutex_lock(&urcu_mutex);
52 if (ret) {
53 perror("Error in pthread mutex lock");
54 exit(-1);
55 }
56}
57
58void internal_urcu_unlock(void)
59{
60 int ret;
61
62 ret = pthread_mutex_unlock(&urcu_mutex);
63 if (ret) {
64 perror("Error in pthread mutex unlock");
65 exit(-1);
66 }
67}
68
69/*
70 * called with urcu_mutex held.
71 */
72static void switch_next_urcu_qparity(void)
73{
74 urcu_gp_ctr ^= RCU_GP_CTR_BIT;
75}
76
77#ifdef DEBUG_FULL_MB
78static void force_mb_all_threads(void)
79{
80 mb();
81}
82#else
83static void force_mb_all_threads(void)
84{
85 struct reader_data *index;
86 /*
87 * Ask for each threads to execute a mb() so we can consider the
88 * compiler barriers around rcu read lock as real memory barriers.
89 */
90 if (!reader_data)
91 return;
92 debug_yield_write();
93 sig_done = 0;
94 debug_yield_write();
95 mb(); /* write sig_done before sending the signals */
96 debug_yield_write();
97 for (index = reader_data; index < reader_data + num_readers; index++) {
98 pthread_kill(index->tid, SIGURCU);
99 debug_yield_write();
100 }
101 /*
102 * Wait for sighandler (and thus mb()) to execute on every thread.
103 * BUSY-LOOP.
104 */
105 while (sig_done < num_readers)
106 barrier();
107 debug_yield_write();
108 mb(); /* read sig_done before ending the barrier */
109 debug_yield_write();
110}
111#endif
112
113void wait_for_quiescent_state(void)
114{
115 struct reader_data *index;
116
117 if (!reader_data)
118 return;
119 /* Wait for each thread urcu_active_readers count to become 0.
120 */
121 for (index = reader_data; index < reader_data + num_readers; index++) {
122 /*
123 * BUSY-LOOP.
124 */
125 while (rcu_old_gp_ongoing(index->urcu_active_readers))
126 barrier();
127 }
128 /*
129 * Locally : read *index->urcu_active_readers before freeing old
130 * pointer.
131 * Remote (reader threads) : Order urcu_qparity update and other
132 * thread's quiescent state counter read.
133 */
134 force_mb_all_threads();
135}
136
137static void switch_qparity(void)
138{
139 /* All threads should read qparity before accessing data structure. */
140 /* Write ptr before changing the qparity */
141 force_mb_all_threads();
142 debug_yield_write();
143 switch_next_urcu_qparity();
144 debug_yield_write();
145
146 /*
147 * Wait for previous parity to be empty of readers.
148 */
149 wait_for_quiescent_state();
150}
151
152void synchronize_rcu(void)
153{
154 debug_yield_write();
155 internal_urcu_lock();
156 debug_yield_write();
157 switch_qparity();
158 debug_yield_write();
159 switch_qparity();
160 debug_yield_write();
161 internal_urcu_unlock();
162 debug_yield_write();
163}
164
165void urcu_add_reader(pthread_t id)
166{
167 struct reader_data *oldarray;
168
169 if (!reader_data) {
170 alloc_readers = INIT_NUM_THREADS;
171 num_readers = 0;
172 reader_data =
173 malloc(sizeof(struct reader_data) * alloc_readers);
174 }
175 if (alloc_readers < num_readers + 1) {
176 oldarray = reader_data;
177 reader_data = malloc(sizeof(struct reader_data)
178 * (alloc_readers << 1));
179 memcpy(reader_data, oldarray,
180 sizeof(struct reader_data) * alloc_readers);
181 alloc_readers <<= 1;
182 free(oldarray);
183 }
184 reader_data[num_readers].tid = id;
185 /* reference to the TLS of _this_ reader thread. */
186 reader_data[num_readers].urcu_active_readers = &urcu_active_readers;
187 num_readers++;
188}
189
190/*
191 * Never shrink (implementation limitation).
192 * This is O(nb threads). Eventually use a hash table.
193 */
194void urcu_remove_reader(pthread_t id)
195{
196 struct reader_data *index;
197
198 assert(reader_data != NULL);
199 for (index = reader_data; index < reader_data + num_readers; index++) {
200 if (pthread_equal(index->tid, id)) {
201 memcpy(index, &reader_data[num_readers - 1],
202 sizeof(struct reader_data));
203 reader_data[num_readers - 1].tid = 0;
204 reader_data[num_readers - 1].urcu_active_readers = NULL;
205 num_readers--;
206 return;
207 }
208 }
209 /* Hrm not found, forgot to register ? */
210 assert(0);
211}
212
213void urcu_register_thread(void)
214{
215 internal_urcu_lock();
216 urcu_add_reader(pthread_self());
217 internal_urcu_unlock();
218}
219
220void urcu_unregister_thread(void)
221{
222 internal_urcu_lock();
223 urcu_remove_reader(pthread_self());
224 internal_urcu_unlock();
225}
226
227#ifndef DEBUG_FULL_MB
228void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
229{
230 mb();
231 atomic_inc(&sig_done);
232}
233
234void __attribute__((constructor)) urcu_init(void)
235{
236 struct sigaction act;
237 int ret;
238
239 act.sa_sigaction = sigurcu_handler;
240 ret = sigaction(SIGURCU, &act, NULL);
241 if (ret) {
242 perror("Error in sigaction");
243 exit(-1);
244 }
245}
246
247void __attribute__((destructor)) urcu_exit(void)
248{
249 struct sigaction act;
250 int ret;
251
252 ret = sigaction(SIGURCU, NULL, &act);
253 if (ret) {
254 perror("Error in sigaction");
255 exit(-1);
256 }
257 assert(act.sa_sigaction == sigurcu_handler);
258 free(reader_data);
259}
260#endif
This page took 0.027791 seconds and 4 git commands to generate.