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
;
30 * called with urcu_mutex held.
32 static int switch_next_urcu_qparity(void)
34 int old_parity
= urcu_qparity
;
35 urcu_qparity
= 1 - old_parity
;
39 static void force_mb_all_threads(void)
41 struct reader_data
*index
;
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.
49 mb(); /* write sig_done before sending the signals */
50 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++)
51 pthread_kill(index
->tid
, SIGURCU
);
53 * Wait for sighandler (and thus mb()) to execute on every thread.
56 while (sig_done
< num_readers
)
58 mb(); /* read sig_done before ending the barrier */
61 void wait_for_quiescent_state(int parity
)
63 struct reader_data
*index
;
67 /* Wait for each thread urcu_active_readers count to become 0.
69 for (index
= reader_data
; index
< reader_data
+ num_readers
; index
++) {
73 while (*index
->urcu_active_readers
!= 0)
77 * Locally : read *index->urcu_active_readers before freeing old
79 * Remote (reader threads) : Order urcu_qparity update and other
80 * thread's quiescent state counter read.
82 force_mb_all_threads();
86 * Return old pointer, OK to free, no more reference exist.
88 void *urcu_publish_content(void **ptr
, void *new)
93 ret
= pthread_mutex_lock(&urcu_mutex
);
95 perror("Error in pthread mutex lock");
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.
111 wmb(); /* Write ptr before changing the qparity */
112 /* All threads should read qparity before ptr */
113 force_mb_all_threads();
114 prev_parity
= switch_next_urcu_qparity();
117 * Wait for previous parity to be empty of readers.
119 wait_for_quiescent_state(prev_parity
);
121 * Deleting old data is ok !
124 ret
= pthread_mutex_unlock(&urcu_mutex
);
126 perror("Error in pthread mutex lock");
132 void urcu_add_reader(pthread_t id
)
134 struct reader_data
*oldarray
;
137 alloc_readers
= INIT_NUM_THREADS
;
140 malloc(sizeof(struct reader_data
) * alloc_readers
);
142 if (alloc_readers
< num_readers
+ 1) {
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
);
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
;
158 * Never shrink (implementation limitation).
159 * This is O(nb threads). Eventually use a hash table.
161 void urcu_remove_reader(pthread_t id
)
163 struct reader_data
*index
;
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
;
176 /* Hrm not found, forgot to register ? */
180 void urcu_register_thread(void)
182 pthread_t self
= pthread_self();
185 ret
= pthread_mutex_lock(&urcu_mutex
);
187 perror("Error in pthread mutex lock");
191 urcu_add_reader(self
);
194 ret
= pthread_mutex_unlock(&urcu_mutex
);
196 perror("Error in pthread mutex unlock");
201 void urcu_unregister_thread(void)
203 pthread_t self
= pthread_self();
206 ret
= pthread_mutex_lock(&urcu_mutex
);
208 perror("Error in pthread mutex lock");
212 urcu_remove_reader(self
);
214 ret
= pthread_mutex_unlock(&urcu_mutex
);
216 perror("Error in pthread mutex unlock");
222 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
225 atomic_inc(&sig_done
);
228 void __attribute__((constructor
)) urcu_init(void)
230 struct sigaction act
;
233 act
.sa_sigaction
= sigurcu_handler
;
234 ret
= sigaction(SIGURCU
, &act
, NULL
);
236 perror("Error in sigaction");
241 void __attribute__((destructor
)) urcu_exit(void)
243 struct sigaction act
;
246 ret
= sigaction(SIGURCU
, NULL
, &act
);
248 perror("Error in sigaction");
251 assert(act
.sa_sigaction
== sigurcu_handler
);