4 * Userspace RCU library - batch memory reclamation
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "urcu-reclaim-static.h"
33 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
34 #include "urcu-reclaim.h"
36 void __attribute__((constructor
)) urcu_reclaim_init(void);
37 void __attribute__((destructor
)) urcu_reclaim_exit(void);
39 extern void synchronize_rcu(void);
44 * urcu_reclaim_mutex nests inside reclaim_thread_mutex.
46 static pthread_mutex_t urcu_reclaim_mutex
= PTHREAD_MUTEX_INITIALIZER
;
47 static pthread_mutex_t reclaim_thread_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 * Written to only by each individual reader. Read by both the reader and the
53 struct reclaim_queue __thread reclaim_queue
;
55 /* Thread IDs of registered readers */
56 #define INIT_NUM_THREADS 4
58 struct reader_registry
{
60 struct reclaim_queue
*reclaim_queue
;
61 unsigned long last_head
;
64 static struct reader_registry
*registry
;
65 static int num_readers
, alloc_readers
;
67 static pthread_t tid_reclaim
;
68 static int exit_reclaim
;
70 static void internal_urcu_lock(pthread_mutex_t
*mutex
)
74 #ifndef DISTRUST_SIGNALS_EXTREME
75 ret
= pthread_mutex_lock(mutex
);
77 perror("Error in pthread mutex lock");
80 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
81 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
82 if (ret
!= EBUSY
&& ret
!= EINTR
) {
83 printf("ret = %d, errno = %d\n", ret
, errno
);
84 perror("Error in pthread mutex lock");
89 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
92 static void internal_urcu_unlock(pthread_mutex_t
*mutex
)
96 ret
= pthread_mutex_unlock(mutex
);
98 perror("Error in pthread mutex unlock");
104 * Must be called after Q.S. is reached.
106 static void rcu_reclaim_barrier_queue(struct reclaim_queue
*queue
,
112 * Tail is only modified when lock is held.
113 * Head is only modified by owner thread.
116 for (i
= queue
->tail
; i
!= head
; i
++) {
117 smp_rmb(); /* read head before q[]. */
118 free(LOAD_SHARED(queue
->q
[i
& RECLAIM_QUEUE_MASK
]));
120 smp_mb(); /* push tail after having used q[] */
121 STORE_SHARED(queue
->tail
, i
);
124 static void _rcu_reclaim_barrier_thread(void)
128 head
= reclaim_queue
.head
;
130 rcu_reclaim_barrier_queue(&reclaim_queue
, head
);
134 void rcu_reclaim_barrier_thread(void)
136 internal_urcu_lock(&urcu_reclaim_mutex
);
137 _rcu_reclaim_barrier_thread();
138 internal_urcu_unlock(&urcu_reclaim_mutex
);
141 void rcu_reclaim_barrier(void)
143 struct reader_registry
*index
;
148 internal_urcu_lock(&urcu_reclaim_mutex
);
149 for (index
= registry
; index
< registry
+ num_readers
; index
++)
150 index
->last_head
= LOAD_SHARED(index
->reclaim_queue
->head
);
152 for (index
= registry
; index
< registry
+ num_readers
; index
++)
153 rcu_reclaim_barrier_queue(index
->reclaim_queue
,
155 internal_urcu_unlock(&urcu_reclaim_mutex
);
158 void *thr_reclaim(void *args
)
161 if (LOAD_SHARED(exit_reclaim
))
163 poll(NULL
,0,100); /* wait for 100ms */
164 rcu_reclaim_barrier();
171 * library wrappers to be used by non-LGPL compatible source code.
174 void rcu_reclaim_queue(void *p
)
176 _rcu_reclaim_queue(p
);
179 static void rcu_add_reader(pthread_t id
)
181 struct reader_registry
*oldarray
;
184 alloc_readers
= INIT_NUM_THREADS
;
187 malloc(sizeof(struct reader_registry
) * alloc_readers
);
189 if (alloc_readers
< num_readers
+ 1) {
191 registry
= malloc(sizeof(struct reader_registry
)
192 * (alloc_readers
<< 1));
193 memcpy(registry
, oldarray
,
194 sizeof(struct reader_registry
) * alloc_readers
);
198 registry
[num_readers
].tid
= id
;
199 /* reference to the TLS of _this_ reader thread. */
200 registry
[num_readers
].reclaim_queue
= &reclaim_queue
;
205 * Never shrink (implementation limitation).
206 * This is O(nb threads). Eventually use a hash table.
208 static void rcu_remove_reader(pthread_t id
)
210 struct reader_registry
*index
;
212 assert(registry
!= NULL
);
213 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
214 if (pthread_equal(index
->tid
, id
)) {
215 memcpy(index
, ®istry
[num_readers
- 1],
216 sizeof(struct reader_registry
));
217 registry
[num_readers
- 1].tid
= 0;
218 registry
[num_readers
- 1].reclaim_queue
= NULL
;
223 /* Hrm not found, forgot to register ? */
227 static void start_reclaim_thread(void)
231 ret
= pthread_create(&tid_reclaim
, NULL
, thr_reclaim
,
236 static void stop_reclaim_thread(void)
241 STORE_SHARED(exit_reclaim
, 1);
242 ret
= pthread_join(tid_reclaim
, &tret
);
246 void rcu_reclaim_register_thread(void)
250 internal_urcu_lock(&reclaim_thread_mutex
);
251 internal_urcu_lock(&urcu_reclaim_mutex
);
252 /* In case gcc does not support constructor attribute */
254 reclaim_queue
.q
= malloc(sizeof(void *) * RECLAIM_QUEUE_SIZE
);
255 rcu_add_reader(pthread_self());
256 readers
= num_readers
;
257 internal_urcu_unlock(&urcu_reclaim_mutex
);
260 start_reclaim_thread();
261 internal_urcu_unlock(&reclaim_thread_mutex
);
264 void rcu_reclaim_unregister_thread(void)
268 internal_urcu_lock(&reclaim_thread_mutex
);
269 internal_urcu_lock(&urcu_reclaim_mutex
);
270 rcu_remove_reader(pthread_self());
271 _rcu_reclaim_barrier_thread();
272 free(reclaim_queue
.q
);
273 reclaim_queue
.q
= NULL
;
274 readers
= num_readers
;
275 internal_urcu_unlock(&urcu_reclaim_mutex
);
278 stop_reclaim_thread();
279 internal_urcu_unlock(&reclaim_thread_mutex
);
283 * urcu_init constructor. Called when the library is linked, but also when
284 * reader threads are calling rcu_register_thread(). Should only be called by a
285 * single thread at a given time. This is ensured by holing the
286 * internal_urcu_lock(&urcu_reclaim_mutex) from rcu_register_thread() or by
287 * running at library load time, which should not be executed by multiple
288 * threads nor concurrently with rcu_register_thread() anyway.
290 void urcu_reclaim_init(void)
297 void urcu_reclaim_exit(void)