Change API
[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
22/* Global quiescent period parity */
23int urcu_qparity;
24
25int __thread urcu_active_readers[2];
26
27/* Thread IDs of registered readers */
28#define INIT_NUM_THREADS 4
29
30struct reader_data {
31 pthread_t tid;
f69f195a 32 int *urcu_active_readers;
27b012e2
MD
33};
34
35static struct reader_data *reader_data;
36static int num_readers, alloc_readers;
37static int sig_done;
38
c265818b 39void internal_urcu_lock(void)
41718ff9
MD
40{
41 int ret;
42 ret = pthread_mutex_lock(&urcu_mutex);
43 if (ret) {
44 perror("Error in pthread mutex lock");
45 exit(-1);
46 }
47}
48
c265818b 49void internal_urcu_unlock(void)
41718ff9
MD
50{
51 int ret;
52
53 ret = pthread_mutex_unlock(&urcu_mutex);
54 if (ret) {
55 perror("Error in pthread mutex unlock");
56 exit(-1);
57 }
58}
59
27b012e2
MD
60/*
61 * called with urcu_mutex held.
62 */
63static int switch_next_urcu_qparity(void)
64{
65 int old_parity = urcu_qparity;
66 urcu_qparity = 1 - old_parity;
67 return old_parity;
68}
69
70static void force_mb_all_threads(void)
71{
f69f195a 72 struct reader_data *index;
27b012e2
MD
73 /*
74 * Ask for each threads to execute a mb() so we can consider the
75 * compiler barriers around rcu read lock as real memory barriers.
76 */
77 if (!reader_data)
78 return;
27b012e2 79 sig_done = 0;
f69f195a 80 mb(); /* write sig_done before sending the signals */
27b012e2 81 for (index = reader_data; index < reader_data + num_readers; index++)
f69f195a 82 pthread_kill(index->tid, SIGURCU);
27b012e2
MD
83 /*
84 * Wait for sighandler (and thus mb()) to execute on every thread.
85 * BUSY-LOOP.
86 */
87 while (sig_done < num_readers)
88 barrier();
f69f195a 89 mb(); /* read sig_done before ending the barrier */
27b012e2
MD
90}
91
92void wait_for_quiescent_state(int parity)
93{
f69f195a 94 struct reader_data *index;
27b012e2
MD
95
96 if (!reader_data)
97 return;
98 /* Wait for each thread urcu_active_readers count to become 0.
99 */
f69f195a 100 for (index = reader_data; index < reader_data + num_readers; index++) {
27b012e2
MD
101 /*
102 * BUSY-LOOP.
103 */
f858d07a 104 while (index->urcu_active_readers[parity] != 0)
27b012e2
MD
105 barrier();
106 }
107 /*
108 * Locally : read *index->urcu_active_readers before freeing old
109 * pointer.
110 * Remote (reader threads) : Order urcu_qparity update and other
111 * thread's quiescent state counter read.
112 */
113 force_mb_all_threads();
114}
115
2bc59bd7
PM
116static void switch_qparity(void)
117{
118 int prev_parity;
119
120 /* All threads should read qparity before accessing data structure. */
121 /* Write ptr before changing the qparity */
122 force_mb_all_threads();
123 prev_parity = switch_next_urcu_qparity();
124
125 /*
126 * Wait for previous parity to be empty of readers.
127 */
128 wait_for_quiescent_state(prev_parity);
129}
130
131void synchronize_rcu(void)
132{
c265818b 133 internal_urcu_lock();
2bc59bd7
PM
134 switch_qparity();
135 switch_qparity();
c265818b 136 internal_urcu_lock();
2bc59bd7
PM
137}
138
27b012e2
MD
139/*
140 * Return old pointer, OK to free, no more reference exist.
41718ff9 141 * Called under rcu_write_lock.
27b012e2 142 */
cdcb92bb 143void *urcu_publish_content(void **ptr, void *new)
27b012e2 144{
27b012e2
MD
145 void *oldptr;
146
c265818b 147 internal_urcu_lock();
27b012e2
MD
148 /*
149 * We can publish the new pointer before we change the current qparity.
150 * Readers seeing the new pointer while being in the previous qparity
151 * window will make us wait until the end of the quiescent state before
152 * we release the unrelated memory area. However, given we hold the
153 * urcu_mutex, we are making sure that no further garbage collection can
154 * occur until we release the mutex, therefore we guarantee that this
155 * given reader will have completed its execution using the new pointer
156 * when the next quiescent state window will be over.
157 */
158 oldptr = *ptr;
159 *ptr = new;
27b012e2 160
2bc59bd7
PM
161 switch_qparity();
162 switch_qparity();
c265818b 163 internal_urcu_unlock();
2bc59bd7 164
27b012e2
MD
165 return oldptr;
166}
167
168void urcu_add_reader(pthread_t id)
169{
f69f195a
MD
170 struct reader_data *oldarray;
171
27b012e2
MD
172 if (!reader_data) {
173 alloc_readers = INIT_NUM_THREADS;
f69f195a 174 num_readers = 0;
27b012e2
MD
175 reader_data =
176 malloc(sizeof(struct reader_data) * alloc_readers);
27b012e2
MD
177 }
178 if (alloc_readers < num_readers + 1) {
27b012e2
MD
179 oldarray = reader_data;
180 reader_data = malloc(sizeof(struct reader_data)
181 * (alloc_readers << 1));
182 memcpy(reader_data, oldarray,
183 sizeof(struct reader_data) * alloc_readers);
184 alloc_readers <<= 1;
185 free(oldarray);
186 }
187 reader_data[num_readers].tid = id;
188 /* reference to the TLS of _this_ reader thread. */
189 reader_data[num_readers].urcu_active_readers = urcu_active_readers;
190 num_readers++;
191}
192
193/*
194 * Never shrink (implementation limitation).
195 * This is O(nb threads). Eventually use a hash table.
196 */
197void urcu_remove_reader(pthread_t id)
198{
199 struct reader_data *index;
200
201 assert(reader_data != NULL);
202 for (index = reader_data; index < reader_data + num_readers; index++) {
e6d6e2dc 203 if (pthread_equal(index->tid, id)) {
27b012e2
MD
204 memcpy(index, &reader_data[num_readers - 1],
205 sizeof(struct reader_data));
206 reader_data[num_readers - 1].tid = 0;
207 reader_data[num_readers - 1].urcu_active_readers = NULL;
208 num_readers--;
209 return;
210 }
211 }
212 /* Hrm not found, forgot to register ? */
213 assert(0);
214}
215
216void urcu_register_thread(void)
217{
c265818b 218 internal_urcu_lock();
41718ff9 219 urcu_add_reader(pthread_self());
c265818b 220 internal_urcu_unlock();
27b012e2
MD
221}
222
f69f195a 223void urcu_unregister_thread(void)
27b012e2 224{
c265818b 225 internal_urcu_lock();
41718ff9 226 urcu_remove_reader(pthread_self());
c265818b 227 internal_urcu_unlock();
27b012e2
MD
228}
229
f69f195a 230void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
27b012e2
MD
231{
232 mb();
233 atomic_inc(&sig_done);
234}
235
236void __attribute__((constructor)) urcu_init(void)
237{
238 struct sigaction act;
239 int ret;
240
241 act.sa_sigaction = sigurcu_handler;
242 ret = sigaction(SIGURCU, &act, NULL);
f69f195a
MD
243 if (ret) {
244 perror("Error in sigaction");
27b012e2
MD
245 exit(-1);
246 }
247}
248
249void __attribute__((destructor)) urcu_exit(void)
250{
251 struct sigaction act;
252 int ret;
253
254 ret = sigaction(SIGURCU, NULL, &act);
f69f195a
MD
255 if (ret) {
256 perror("Error in sigaction");
27b012e2
MD
257 exit(-1);
258 }
259 assert(act.sa_sigaction == sigurcu_handler);
260 free(reader_data);
261}
This page took 0.049457 seconds and 4 git commands to generate.