From 786ee85b1676d2a1865b2db7120c45bafdf954d6 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Sat, 19 Sep 2009 20:05:10 -0400 Subject: [PATCH] Rename liburcu-reclaim to liburcu-defer Signed-off-by: Mathieu Desnoyers --- Makefile.inc | 6 +- tests/Makefile.inc | 6 +- ...{test_urcu_reclaim.c => test_urcu_defer.c} | 10 +- urcu-reclaim-static.h => urcu-defer-static.h | 40 +-- urcu-defer.c | 280 ++++++++++++++++++ urcu-reclaim.h => urcu-defer.h | 28 +- urcu-reclaim.c | 280 ------------------ 7 files changed, 325 insertions(+), 325 deletions(-) rename tests/{test_urcu_reclaim.c => test_urcu_defer.c} (98%) rename urcu-reclaim-static.h => urcu-defer-static.h (75%) create mode 100644 urcu-defer.c rename urcu-reclaim.h => urcu-defer.h (72%) delete mode 100644 urcu-reclaim.c diff --git a/Makefile.inc b/Makefile.inc index d328de1..6d83ae7 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -6,7 +6,7 @@ DIRS=tests all: checkarch liburcu.so urcu.o \ liburcu-qsbr.so urcu-qsbr.o \ liburcu-mb.so urcu-mb.o \ - liburcu-reclaim.so urcu-reclaim.o \ + liburcu-defer.so urcu-defer.o \ urcu-yield.o \ subdirs @@ -35,7 +35,7 @@ urcu-mb.o: urcu.c urcu.h urcu-qsbr.o: urcu-qsbr.c urcu-qsbr.h $(CC) -fPIC ${CFLAGS} $(LDFLAGS) -c -o $@ $(SRC_DEP) -urcu-reclaim.o: urcu-reclaim.c urcu-reclaim.h +urcu-defer.o: urcu-defer.c urcu-defer.h $(CC) -fPIC ${CFLAGS} $(LDFLAGS) -c -o $@ $(SRC_DEP) liburcu.so: urcu.o @@ -47,7 +47,7 @@ liburcu-qsbr.so: urcu-qsbr.o liburcu-mb.so: urcu-mb.o $(CC) ${LDFLAGS} -fPIC -shared -o $@ $< -liburcu-reclaim.so: urcu-reclaim.o +liburcu-defer.so: urcu-defer.o $(CC) ${LDFLAGS} -fPIC -shared -o $@ $< urcu-yield.o: urcu.c urcu.h diff --git a/tests/Makefile.inc b/tests/Makefile.inc index ddcc515..ff422a6 100644 --- a/tests/Makefile.inc +++ b/tests/Makefile.inc @@ -15,7 +15,7 @@ URCU_SIGNAL=${LIBDIR}/urcu.o ${LIBDIR}/urcu.h URCU_SIGNAL_YIELD=${LIBDIR}/urcu-yield.o ${LIBDIR}/urcu.h URCU_MB=${LIBDIR}/urcu-mb.o ${LIBDIR}/urcu.h URCU_QSBR=${LIBDIR}/urcu-qsbr.o ${LIBDIR}/urcu-qsbr.h -URCU_MB_RECLAIM=${LIBDIR}/urcu-mb.o ${LIBDIR}/urcu-reclaim.o ${LIBDIR}/urcu.h +URCU_MB_DEFER=${LIBDIR}/urcu-mb.o ${LIBDIR}/urcu-defer.o ${LIBDIR}/urcu.h all: test_urcu test_urcu_dynamic_link test_urcu_timing \ test_rwlock_timing test_rwlock test_perthreadlock_timing \ @@ -23,7 +23,7 @@ all: test_urcu test_urcu_dynamic_link test_urcu_timing \ urcu-asm.S test_qsbr_timing test_qsbr urcu-asm.o urcutorture \ urcutorture-yield test_mutex test_looplen test_urcu_gc \ test_urcu_gc_mb test_qsbr_gc test_qsbr_lgc test_urcu_lgc \ - test_urcu_lgc_mb test_qsbr_dynamic_link test_urcu_mb_reclaim + test_urcu_lgc_mb test_qsbr_dynamic_link test_urcu_mb_defer api.h: ${APIHEADER} cp -f ${APIHEADER} api.h @@ -51,7 +51,7 @@ test_urcu_gc_mb: test_urcu_gc.c ${URCU_MB} test_urcu_lgc_mb: test_urcu_gc.c ${URCU_MB} $(CC) -DTEST_LOCAL_GC -DURCU_MB ${CFLAGS} $(LDFLAGS) -o $@ $(SRC_DEP) -test_urcu_mb_reclaim: test_urcu_reclaim.c ${URCU_MB_RECLAIM} +test_urcu_mb_defer: test_urcu_defer.c ${URCU_MB_DEFER} $(CC) -DURCU_MB ${CFLAGS} $(LDFLAGS) -o $@ $(SRC_DEP) diff --git a/tests/test_urcu_reclaim.c b/tests/test_urcu_defer.c similarity index 98% rename from tests/test_urcu_reclaim.c rename to tests/test_urcu_defer.c index 77abea0..e56dffe 100644 --- a/tests/test_urcu_reclaim.c +++ b/tests/test_urcu_defer.c @@ -1,5 +1,5 @@ /* - * test_urcu_reclaim.c + * test_urcu_defer.c * * Userspace RCU library - test program (with automatic reclamation) * @@ -62,7 +62,7 @@ static inline pid_t gettid(void) #define debug_yield_read() #endif #include "../urcu.h" -#include "../urcu-reclaim.h" +#include "../urcu-defer.h" struct test_array { int a; @@ -219,7 +219,7 @@ void *thr_writer(void *data) set_affinity(); - rcu_reclaim_register_thread(); + rcu_defer_register_thread(); while (!test_go) { @@ -230,7 +230,7 @@ void *thr_writer(void *data) new = malloc(sizeof(*new)); new->a = 8; old = rcu_xchg_pointer(&test_rcu_pointer, new); - rcu_reclaim_queue(old); + rcu_defer_queue(old); nr_writes++; if (unlikely(!test_duration_write())) break; @@ -238,7 +238,7 @@ void *thr_writer(void *data) loop_sleep(wdelay); } - rcu_reclaim_unregister_thread(); + rcu_defer_unregister_thread(); printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", "writer", pthread_self(), (unsigned long)gettid()); diff --git a/urcu-reclaim-static.h b/urcu-defer-static.h similarity index 75% rename from urcu-reclaim-static.h rename to urcu-defer-static.h index 40de6fe..2513769 100644 --- a/urcu-reclaim-static.h +++ b/urcu-defer-static.h @@ -1,12 +1,12 @@ -#ifndef _URCU_RECLAIM_STATIC_H -#define _URCU_RECLAIM_STATIC_H +#ifndef _URCU_DEFER_STATIC_H +#define _URCU_DEFER_STATIC_H /* - * urcu-reclaim-static.h + * urcu-defer-static.h * * Userspace RCU header - memory reclamation. * - * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu-reclaim.h for linking + * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu-defer.h for linking * dynamically with the userspace rcu reclamation library. * * Copyright (c) 2009 Mathieu Desnoyers @@ -37,10 +37,10 @@ /* - * Number of entries in the per-thread reclaim queue. Must be power of 2. + * Number of entries in the per-thread defer queue. Must be power of 2. */ -#define RECLAIM_QUEUE_SIZE (1 << 12) -#define RECLAIM_QUEUE_MASK (RECLAIM_QUEUE_SIZE - 1) +#define DEFER_QUEUE_SIZE (1 << 12) +#define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1) /* * Identify a shared load. A smp_rmc() or smp_mc() should come before the load. @@ -86,20 +86,20 @@ #define rcu_assert(args...) #endif -struct reclaim_queue { +struct defer_queue { unsigned long head; /* add element at head */ unsigned long tail; /* next element to remove at tail */ void **q; }; -extern struct reclaim_queue __thread reclaim_queue; +extern struct defer_queue __thread defer_queue; -extern void rcu_reclaim_barrier_thread(void); +extern void rcu_defer_barrier_thread(void); /* * not signal-safe. */ -static inline void _rcu_reclaim_queue(void *p) +static inline void _rcu_defer_queue(void *p) { unsigned long head, tail; @@ -107,22 +107,22 @@ static inline void _rcu_reclaim_queue(void *p) * Head is only modified by ourself. Tail can be modified by reclamation * thread. */ - head = reclaim_queue.head; - tail = LOAD_SHARED(reclaim_queue.tail); + head = defer_queue.head; + tail = LOAD_SHARED(defer_queue.tail); /* * If queue is full, empty it ourself. */ - if (unlikely(head - tail >= RECLAIM_QUEUE_SIZE)) { - assert(head - tail == RECLAIM_QUEUE_SIZE); - rcu_reclaim_barrier_thread(); - assert(head - LOAD_SHARED(reclaim_queue.tail) == 0); + if (unlikely(head - tail >= DEFER_QUEUE_SIZE)) { + assert(head - tail == DEFER_QUEUE_SIZE); + rcu_defer_barrier_thread(); + assert(head - LOAD_SHARED(defer_queue.tail) == 0); } smp_wmb(); /* Publish new pointer before write q[] */ - _STORE_SHARED(reclaim_queue.q[head & RECLAIM_QUEUE_MASK], p); + _STORE_SHARED(defer_queue.q[head & DEFER_QUEUE_MASK], p); smp_wmb(); /* Write q[] before head. */ - STORE_SHARED(reclaim_queue.head, head + 1); + STORE_SHARED(defer_queue.head, head + 1); } -#endif /* _URCU_RECLAIM_STATIC_H */ +#endif /* _URCU_DEFER_STATIC_H */ diff --git a/urcu-defer.c b/urcu-defer.c new file mode 100644 index 0000000..1e5d2b6 --- /dev/null +++ b/urcu-defer.c @@ -0,0 +1,280 @@ +/* + * urcu-defer.c + * + * Userspace RCU library - batch memory reclamation + * + * Copyright (c) 2009 Mathieu Desnoyers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "urcu-defer-static.h" +/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ +#include "urcu-defer.h" + +void __attribute__((destructor)) urcu_defer_exit(void); + +extern void synchronize_rcu(void); + +/* + * urcu_defer_mutex nests inside defer_thread_mutex. + */ +static pthread_mutex_t urcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER; + +/* + * Written to only by each individual deferer. Read by both the deferer and + * the reclamation tread. + */ +struct defer_queue __thread defer_queue; + +/* Thread IDs of registered deferers */ +#define INIT_NUM_THREADS 4 + +struct deferer_registry { + pthread_t tid; + struct defer_queue *defer_queue; + unsigned long last_head; +}; + +static struct deferer_registry *registry; +static int num_deferers, alloc_deferers; + +static pthread_t tid_defer; +static int exit_defer; + +static void internal_urcu_lock(pthread_mutex_t *mutex) +{ + int ret; + +#ifndef DISTRUST_SIGNALS_EXTREME + ret = pthread_mutex_lock(mutex); + if (ret) { + perror("Error in pthread mutex lock"); + exit(-1); + } +#else /* #ifndef DISTRUST_SIGNALS_EXTREME */ + while ((ret = pthread_mutex_trylock(mutex)) != 0) { + if (ret != EBUSY && ret != EINTR) { + printf("ret = %d, errno = %d\n", ret, errno); + perror("Error in pthread mutex lock"); + exit(-1); + } + poll(NULL,0,10); + } +#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ +} + +static void internal_urcu_unlock(pthread_mutex_t *mutex) +{ + int ret; + + ret = pthread_mutex_unlock(mutex); + if (ret) { + perror("Error in pthread mutex unlock"); + exit(-1); + } +} + +/* + * Must be called after Q.S. is reached. + */ +static void rcu_defer_barrier_queue(struct defer_queue *queue, + unsigned long head) +{ + unsigned long i; + + /* + * Tail is only modified when lock is held. + * Head is only modified by owner thread. + */ + + for (i = queue->tail; i != head; i++) { + smp_rmb(); /* read head before q[]. */ + free(LOAD_SHARED(queue->q[i & DEFER_QUEUE_MASK])); + } + smp_mb(); /* push tail after having used q[] */ + STORE_SHARED(queue->tail, i); +} + +static void _rcu_defer_barrier_thread(void) +{ + unsigned long head; + + head = defer_queue.head; + synchronize_rcu(); + rcu_defer_barrier_queue(&defer_queue, head); +} + + +void rcu_defer_barrier_thread(void) +{ + internal_urcu_lock(&urcu_defer_mutex); + _rcu_defer_barrier_thread(); + internal_urcu_unlock(&urcu_defer_mutex); +} + +void rcu_defer_barrier(void) +{ + struct deferer_registry *index; + + if (!registry) + return; + + internal_urcu_lock(&urcu_defer_mutex); + for (index = registry; index < registry + num_deferers; index++) + index->last_head = LOAD_SHARED(index->defer_queue->head); + synchronize_rcu(); + for (index = registry; index < registry + num_deferers; index++) + rcu_defer_barrier_queue(index->defer_queue, + index->last_head); + internal_urcu_unlock(&urcu_defer_mutex); +} + +void *thr_defer(void *args) +{ + for (;;) { + if (LOAD_SHARED(exit_defer)) + break; + poll(NULL,0,100); /* wait for 100ms */ + rcu_defer_barrier(); + } + + return NULL; +} + +/* + * library wrappers to be used by non-LGPL compatible source code. + */ + +void rcu_defer_queue(void *p) +{ + _rcu_defer_queue(p); +} + +static void rcu_add_deferer(pthread_t id) +{ + struct deferer_registry *oldarray; + + if (!registry) { + alloc_deferers = INIT_NUM_THREADS; + num_deferers = 0; + registry = + malloc(sizeof(struct deferer_registry) * alloc_deferers); + } + if (alloc_deferers < num_deferers + 1) { + oldarray = registry; + registry = malloc(sizeof(struct deferer_registry) + * (alloc_deferers << 1)); + memcpy(registry, oldarray, + sizeof(struct deferer_registry) * alloc_deferers); + alloc_deferers <<= 1; + free(oldarray); + } + registry[num_deferers].tid = id; + /* reference to the TLS of _this_ deferer thread. */ + registry[num_deferers].defer_queue = &defer_queue; + num_deferers++; +} + +/* + * Never shrink (implementation limitation). + * This is O(nb threads). Eventually use a hash table. + */ +static void rcu_remove_deferer(pthread_t id) +{ + struct deferer_registry *index; + + assert(registry != NULL); + for (index = registry; index < registry + num_deferers; index++) { + if (pthread_equal(index->tid, id)) { + memcpy(index, ®istry[num_deferers - 1], + sizeof(struct deferer_registry)); + registry[num_deferers - 1].tid = 0; + registry[num_deferers - 1].defer_queue = NULL; + num_deferers--; + return; + } + } + /* Hrm not found, forgot to register ? */ + assert(0); +} + +static void start_defer_thread(void) +{ + int ret; + + ret = pthread_create(&tid_defer, NULL, thr_defer, + NULL); + assert(!ret); +} + +static void stop_defer_thread(void) +{ + int ret; + void *tret; + + STORE_SHARED(exit_defer, 1); + ret = pthread_join(tid_defer, &tret); + assert(!ret); +} + +void rcu_defer_register_thread(void) +{ + int deferers; + + internal_urcu_lock(&defer_thread_mutex); + internal_urcu_lock(&urcu_defer_mutex); + defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE); + rcu_add_deferer(pthread_self()); + deferers = num_deferers; + internal_urcu_unlock(&urcu_defer_mutex); + + if (deferers == 1) + start_defer_thread(); + internal_urcu_unlock(&defer_thread_mutex); +} + +void rcu_defer_unregister_thread(void) +{ + int deferers; + + internal_urcu_lock(&defer_thread_mutex); + internal_urcu_lock(&urcu_defer_mutex); + rcu_remove_deferer(pthread_self()); + _rcu_defer_barrier_thread(); + free(defer_queue.q); + defer_queue.q = NULL; + deferers = num_deferers; + internal_urcu_unlock(&urcu_defer_mutex); + + if (deferers == 0) + stop_defer_thread(); + internal_urcu_unlock(&defer_thread_mutex); +} + +void urcu_defer_exit(void) +{ + free(registry); +} diff --git a/urcu-reclaim.h b/urcu-defer.h similarity index 72% rename from urcu-reclaim.h rename to urcu-defer.h index adca25b..456b8b7 100644 --- a/urcu-reclaim.h +++ b/urcu-defer.h @@ -1,10 +1,10 @@ -#ifndef _URCU_RECLAIM_H -#define _URCU_RECLAIM_H +#ifndef _URCU_BATCH_H +#define _URCU_BATCH_H /* - * urcu-reclaim.h + * urcu-defer.h * - * Userspace RCU header - memory reclamation + * Userspace RCU header - deferred execution * * Copyright (c) 2009 Mathieu Desnoyers * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. @@ -12,7 +12,7 @@ * LGPL-compatible code should include this header with : * * #define _LGPL_SOURCE - * #include + * #include * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -36,20 +36,20 @@ * Important ! * * Each thread queuing memory reclamation must be registered with - * rcu_reclaim_register_thread(). rcu_reclaim_unregister_thread() should be + * rcu_defer_register_thread(). rcu_defer_unregister_thread() should be * called before the thread exits. */ #ifdef _LGPL_SOURCE -#include +#include /* * Mappings for static use of the userspace RCU library. * Should only be used in LGPL-compatible code. */ -#define rcu_reclaim_queue _rcu_reclaim_queue +#define rcu_defer_queue _rcu_defer_queue #else /* !_LGPL_SOURCE */ @@ -57,16 +57,16 @@ * library wrappers to be used by non-LGPL compatible source code. */ -extern void rcu_reclaim_queue(void *p); +extern void rcu_defer_queue(void *p); #endif /* !_LGPL_SOURCE */ /* * Thread registration for reclamation. */ -extern void rcu_reclaim_register_thread(void); -extern void rcu_reclaim_unregister_thread(void); -extern void rcu_reclaim_barrier(void); -extern void rcu_reclaim_barrier_thread(void); +extern void rcu_defer_register_thread(void); +extern void rcu_defer_unregister_thread(void); +extern void rcu_defer_barrier(void); +extern void rcu_defer_barrier_thread(void); -#endif /* _URCU_RECLAIM_H */ +#endif /* _URCU_BATCH_H */ diff --git a/urcu-reclaim.c b/urcu-reclaim.c deleted file mode 100644 index ff07aee..0000000 --- a/urcu-reclaim.c +++ /dev/null @@ -1,280 +0,0 @@ -/* - * urcu-reclaim.c - * - * Userspace RCU library - batch memory reclamation - * - * Copyright (c) 2009 Mathieu Desnoyers - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "urcu-reclaim-static.h" -/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ -#include "urcu-reclaim.h" - -void __attribute__((destructor)) urcu_reclaim_exit(void); - -extern void synchronize_rcu(void); - -/* - * urcu_reclaim_mutex nests inside reclaim_thread_mutex. - */ -static pthread_mutex_t urcu_reclaim_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t reclaim_thread_mutex = PTHREAD_MUTEX_INITIALIZER; - -/* - * Written to only by each individual reclaimer. Read by both the reclaimer and - * the reclamation tread. - */ -struct reclaim_queue __thread reclaim_queue; - -/* Thread IDs of registered reclaimers */ -#define INIT_NUM_THREADS 4 - -struct reclaimer_registry { - pthread_t tid; - struct reclaim_queue *reclaim_queue; - unsigned long last_head; -}; - -static struct reclaimer_registry *registry; -static int num_reclaimers, alloc_reclaimers; - -static pthread_t tid_reclaim; -static int exit_reclaim; - -static void internal_urcu_lock(pthread_mutex_t *mutex) -{ - int ret; - -#ifndef DISTRUST_SIGNALS_EXTREME - ret = pthread_mutex_lock(mutex); - if (ret) { - perror("Error in pthread mutex lock"); - exit(-1); - } -#else /* #ifndef DISTRUST_SIGNALS_EXTREME */ - while ((ret = pthread_mutex_trylock(mutex)) != 0) { - if (ret != EBUSY && ret != EINTR) { - printf("ret = %d, errno = %d\n", ret, errno); - perror("Error in pthread mutex lock"); - exit(-1); - } - poll(NULL,0,10); - } -#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ -} - -static void internal_urcu_unlock(pthread_mutex_t *mutex) -{ - int ret; - - ret = pthread_mutex_unlock(mutex); - if (ret) { - perror("Error in pthread mutex unlock"); - exit(-1); - } -} - -/* - * Must be called after Q.S. is reached. - */ -static void rcu_reclaim_barrier_queue(struct reclaim_queue *queue, - unsigned long head) -{ - unsigned long i; - - /* - * Tail is only modified when lock is held. - * Head is only modified by owner thread. - */ - - for (i = queue->tail; i != head; i++) { - smp_rmb(); /* read head before q[]. */ - free(LOAD_SHARED(queue->q[i & RECLAIM_QUEUE_MASK])); - } - smp_mb(); /* push tail after having used q[] */ - STORE_SHARED(queue->tail, i); -} - -static void _rcu_reclaim_barrier_thread(void) -{ - unsigned long head; - - head = reclaim_queue.head; - synchronize_rcu(); - rcu_reclaim_barrier_queue(&reclaim_queue, head); -} - - -void rcu_reclaim_barrier_thread(void) -{ - internal_urcu_lock(&urcu_reclaim_mutex); - _rcu_reclaim_barrier_thread(); - internal_urcu_unlock(&urcu_reclaim_mutex); -} - -void rcu_reclaim_barrier(void) -{ - struct reclaimer_registry *index; - - if (!registry) - return; - - internal_urcu_lock(&urcu_reclaim_mutex); - for (index = registry; index < registry + num_reclaimers; index++) - index->last_head = LOAD_SHARED(index->reclaim_queue->head); - synchronize_rcu(); - for (index = registry; index < registry + num_reclaimers; index++) - rcu_reclaim_barrier_queue(index->reclaim_queue, - index->last_head); - internal_urcu_unlock(&urcu_reclaim_mutex); -} - -void *thr_reclaim(void *args) -{ - for (;;) { - if (LOAD_SHARED(exit_reclaim)) - break; - poll(NULL,0,100); /* wait for 100ms */ - rcu_reclaim_barrier(); - } - - return NULL; -} - -/* - * library wrappers to be used by non-LGPL compatible source code. - */ - -void rcu_reclaim_queue(void *p) -{ - _rcu_reclaim_queue(p); -} - -static void rcu_add_reclaimer(pthread_t id) -{ - struct reclaimer_registry *oldarray; - - if (!registry) { - alloc_reclaimers = INIT_NUM_THREADS; - num_reclaimers = 0; - registry = - malloc(sizeof(struct reclaimer_registry) * alloc_reclaimers); - } - if (alloc_reclaimers < num_reclaimers + 1) { - oldarray = registry; - registry = malloc(sizeof(struct reclaimer_registry) - * (alloc_reclaimers << 1)); - memcpy(registry, oldarray, - sizeof(struct reclaimer_registry) * alloc_reclaimers); - alloc_reclaimers <<= 1; - free(oldarray); - } - registry[num_reclaimers].tid = id; - /* reference to the TLS of _this_ reclaimer thread. */ - registry[num_reclaimers].reclaim_queue = &reclaim_queue; - num_reclaimers++; -} - -/* - * Never shrink (implementation limitation). - * This is O(nb threads). Eventually use a hash table. - */ -static void rcu_remove_reclaimer(pthread_t id) -{ - struct reclaimer_registry *index; - - assert(registry != NULL); - for (index = registry; index < registry + num_reclaimers; index++) { - if (pthread_equal(index->tid, id)) { - memcpy(index, ®istry[num_reclaimers - 1], - sizeof(struct reclaimer_registry)); - registry[num_reclaimers - 1].tid = 0; - registry[num_reclaimers - 1].reclaim_queue = NULL; - num_reclaimers--; - return; - } - } - /* Hrm not found, forgot to register ? */ - assert(0); -} - -static void start_reclaim_thread(void) -{ - int ret; - - ret = pthread_create(&tid_reclaim, NULL, thr_reclaim, - NULL); - assert(!ret); -} - -static void stop_reclaim_thread(void) -{ - int ret; - void *tret; - - STORE_SHARED(exit_reclaim, 1); - ret = pthread_join(tid_reclaim, &tret); - assert(!ret); -} - -void rcu_reclaim_register_thread(void) -{ - int reclaimers; - - internal_urcu_lock(&reclaim_thread_mutex); - internal_urcu_lock(&urcu_reclaim_mutex); - reclaim_queue.q = malloc(sizeof(void *) * RECLAIM_QUEUE_SIZE); - rcu_add_reclaimer(pthread_self()); - reclaimers = num_reclaimers; - internal_urcu_unlock(&urcu_reclaim_mutex); - - if (reclaimers == 1) - start_reclaim_thread(); - internal_urcu_unlock(&reclaim_thread_mutex); -} - -void rcu_reclaim_unregister_thread(void) -{ - int reclaimers; - - internal_urcu_lock(&reclaim_thread_mutex); - internal_urcu_lock(&urcu_reclaim_mutex); - rcu_remove_reclaimer(pthread_self()); - _rcu_reclaim_barrier_thread(); - free(reclaim_queue.q); - reclaim_queue.q = NULL; - reclaimers = num_reclaimers; - internal_urcu_unlock(&urcu_reclaim_mutex); - - if (reclaimers == 0) - stop_reclaim_thread(); - internal_urcu_unlock(&reclaim_thread_mutex); -} - -void urcu_reclaim_exit(void) -{ - free(registry); -} -- 2.34.1