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