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