10 pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
12 /* Global quiescent period parity */
15 int __thread urcu_active_readers
[2];
17 /* Thread IDs of registered readers */
18 #define INIT_NUM_THREADS 4
22 int *urcu_active_readers
;
25 static struct reader_data
*reader_data
;
26 static int num_readers
, alloc_readers
;
29 void rcu_write_lock(void)
32 ret
= pthread_mutex_lock(&urcu_mutex
);
34 perror("Error in pthread mutex lock");
39 void rcu_write_unlock(void)
43 ret
= pthread_mutex_unlock(&urcu_mutex
);
45 perror("Error in pthread mutex unlock");
51 * called with urcu_mutex held.
53 static int switch_next_urcu_qparity(void)
55 int old_parity
= urcu_qparity
;
56 urcu_qparity
= 1 - old_parity
;
60 static void force_mb_all_threads(void)
62 struct reader_data
*index
;
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.
70 mb(); /* write sig_done before sending the signals */
71 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++)
72 pthread_kill(index
->tid
, SIGURCU
);
74 * Wait for sighandler (and thus mb()) to execute on every thread.
77 while (sig_done
< num_readers
)
79 mb(); /* read sig_done before ending the barrier */
82 void wait_for_quiescent_state(int parity
)
84 struct reader_data
*index
;
88 /* Wait for each thread urcu_active_readers count to become 0.
90 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
94 while (*index
->urcu_active_readers
!= 0)
98 * Locally : read *index->urcu_active_readers before freeing old
100 * Remote (reader threads) : Order urcu_qparity update and other
101 * thread's quiescent state counter read.
103 force_mb_all_threads();
107 * Return old pointer, OK to free, no more reference exist.
108 * Called under rcu_write_lock.
110 void *urcu_publish_content(void **ptr
, void *new)
112 int ret
, prev_parity
;
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.
127 wmb(); /* Write ptr before changing the qparity */
128 /* All threads should read qparity before ptr */
129 force_mb_all_threads();
130 prev_parity
= switch_next_urcu_qparity();
133 * Wait for previous parity to be empty of readers.
135 wait_for_quiescent_state(prev_parity
);
137 * Deleting old data is ok !
143 void urcu_add_reader(pthread_t id
)
145 struct reader_data
*oldarray
;
148 alloc_readers
= INIT_NUM_THREADS
;
151 malloc(sizeof(struct reader_data
) * alloc_readers
);
153 if (alloc_readers
< num_readers
+ 1) {
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
);
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
;
169 * Never shrink (implementation limitation).
170 * This is O(nb threads). Eventually use a hash table.
172 void urcu_remove_reader(pthread_t id
)
174 struct reader_data
*index
;
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
;
187 /* Hrm not found, forgot to register ? */
191 void urcu_register_thread(void)
194 urcu_add_reader(pthread_self());
198 void urcu_unregister_thread(void)
200 pthread_t self
= pthread_self();
202 urcu_remove_reader(pthread_self());
206 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
209 atomic_inc(&sig_done
);
212 void __attribute__((constructor
)) urcu_init(void)
214 struct sigaction act
;
217 act
.sa_sigaction
= sigurcu_handler
;
218 ret
= sigaction(SIGURCU
, &act
, NULL
);
220 perror("Error in sigaction");
225 void __attribute__((destructor
)) urcu_exit(void)
227 struct sigaction act
;
230 ret
= sigaction(SIGURCU
, NULL
, &act
);
232 perror("Error in sigaction");
235 assert(act
.sa_sigaction
== sigurcu_handler
);