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