4 * Userspace RCU library
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
20 pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
22 /* Global quiescent period parity */
25 int __thread urcu_active_readers
[2];
27 /* Thread IDs of registered readers */
28 #define INIT_NUM_THREADS 4
32 int *urcu_active_readers
;
35 static struct reader_data
*reader_data
;
36 static int num_readers
, alloc_readers
;
39 void internal_urcu_lock(void)
42 ret
= pthread_mutex_lock(&urcu_mutex
);
44 perror("Error in pthread mutex lock");
49 void internal_urcu_unlock(void)
53 ret
= pthread_mutex_unlock(&urcu_mutex
);
55 perror("Error in pthread mutex unlock");
61 * called with urcu_mutex held.
63 static int switch_next_urcu_qparity(void)
65 int old_parity
= urcu_qparity
;
66 urcu_qparity
= 1 - old_parity
;
70 static void force_mb_all_threads(void)
72 struct reader_data
*index
;
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.
80 mb(); /* write sig_done before sending the signals */
81 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++)
82 pthread_kill(index
->tid
, SIGURCU
);
84 * Wait for sighandler (and thus mb()) to execute on every thread.
87 while (sig_done
< num_readers
)
89 mb(); /* read sig_done before ending the barrier */
92 void wait_for_quiescent_state(int parity
)
94 struct reader_data
*index
;
98 /* Wait for each thread urcu_active_readers count to become 0.
100 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
104 while (index
->urcu_active_readers
[parity
] != 0)
108 * Locally : read *index->urcu_active_readers before freeing old
110 * Remote (reader threads) : Order urcu_qparity update and other
111 * thread's quiescent state counter read.
113 force_mb_all_threads();
116 static void switch_qparity(void)
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();
126 * Wait for previous parity to be empty of readers.
128 wait_for_quiescent_state(prev_parity
);
131 void synchronize_rcu(void)
133 internal_urcu_lock();
136 internal_urcu_lock();
140 * Return old pointer, OK to free, no more reference exist.
141 * Called under rcu_write_lock.
143 void *urcu_publish_content(void **ptr
, void *new)
147 internal_urcu_lock();
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.
163 internal_urcu_unlock();
168 void urcu_add_reader(pthread_t id
)
170 struct reader_data
*oldarray
;
173 alloc_readers
= INIT_NUM_THREADS
;
176 malloc(sizeof(struct reader_data
) * alloc_readers
);
178 if (alloc_readers
< num_readers
+ 1) {
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
);
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
;
194 * Never shrink (implementation limitation).
195 * This is O(nb threads). Eventually use a hash table.
197 void urcu_remove_reader(pthread_t id
)
199 struct reader_data
*index
;
201 assert(reader_data
!= NULL
);
202 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
203 if (pthread_equal(index
->tid
, id
)) {
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
;
212 /* Hrm not found, forgot to register ? */
216 void urcu_register_thread(void)
218 internal_urcu_lock();
219 urcu_add_reader(pthread_self());
220 internal_urcu_unlock();
223 void urcu_unregister_thread(void)
225 internal_urcu_lock();
226 urcu_remove_reader(pthread_self());
227 internal_urcu_unlock();
230 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
233 atomic_inc(&sig_done
);
236 void __attribute__((constructor
)) urcu_init(void)
238 struct sigaction act
;
241 act
.sa_sigaction
= sigurcu_handler
;
242 ret
= sigaction(SIGURCU
, &act
, NULL
);
244 perror("Error in sigaction");
249 void __attribute__((destructor
)) urcu_exit(void)
251 struct sigaction act
;
254 ret
= sigaction(SIGURCU
, NULL
, &act
);
256 perror("Error in sigaction");
259 assert(act
.sa_sigaction
== sigurcu_handler
);