From: Mathieu Desnoyers Date: Sat, 19 Sep 2009 21:28:06 +0000 (-0400) Subject: Add rcu-reclaim.so library X-Git-Tag: v0.1~68 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=90075a50c3d4ab810f7d9ac218b53cda0ed67fe5;p=userspace-rcu.git Add rcu-reclaim.so library Add "automated" RCU memory reclamation. Signed-off-by: Mathieu Desnoyers --- diff --git a/Makefile.inc b/Makefile.inc index 2eb0da8..d328de1 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -6,6 +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 \ urcu-yield.o \ subdirs @@ -34,6 +35,9 @@ 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 + $(CC) -fPIC ${CFLAGS} $(LDFLAGS) -c -o $@ $(SRC_DEP) + liburcu.so: urcu.o $(CC) ${LDFLAGS} -fPIC -shared -o $@ $< @@ -43,6 +47,9 @@ liburcu-qsbr.so: urcu-qsbr.o liburcu-mb.so: urcu-mb.o $(CC) ${LDFLAGS} -fPIC -shared -o $@ $< +liburcu-reclaim.so: urcu-reclaim.o + $(CC) ${LDFLAGS} -fPIC -shared -o $@ $< + urcu-yield.o: urcu.c urcu.h $(CC) -DDEBUG_YIELD ${CFLAGS} $(LDFLAGS) -c -o $@ $(SRC_DEP) diff --git a/tests/Makefile.inc b/tests/Makefile.inc index 56953f0..ddcc515 100644 --- a/tests/Makefile.inc +++ b/tests/Makefile.inc @@ -15,6 +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 all: test_urcu test_urcu_dynamic_link test_urcu_timing \ test_rwlock_timing test_rwlock test_perthreadlock_timing \ @@ -22,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_lgc_mb test_qsbr_dynamic_link test_urcu_mb_reclaim api.h: ${APIHEADER} cp -f ${APIHEADER} api.h @@ -50,6 +51,9 @@ 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} + $(CC) -DURCU_MB ${CFLAGS} $(LDFLAGS) -o $@ $(SRC_DEP) + test_qsbr: test_qsbr.c ${URCU_QSBR} $(CC) ${CFLAGS} $(LDFLAGS) -o $@ $(SRC_DEP) diff --git a/tests/test_urcu_reclaim.c b/tests/test_urcu_reclaim.c new file mode 100644 index 0000000..77abea0 --- /dev/null +++ b/tests/test_urcu_reclaim.c @@ -0,0 +1,398 @@ +/* + * test_urcu_reclaim.c + * + * Userspace RCU library - test program (with automatic reclamation) + * + * Copyright February 2009 - Mathieu Desnoyers + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../arch.h" + +/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */ +#define CACHE_LINE_SIZE 4096 + +/* hardcoded number of CPUs */ +#define NR_CPUS 16384 + +#if defined(_syscall0) +_syscall0(pid_t, gettid) +#elif defined(__NR_gettid) +static inline pid_t gettid(void) +{ + return syscall(__NR_gettid); +} +#else +#warning "use pid as tid" +static inline pid_t gettid(void) +{ + return getpid(); +} +#endif + +#ifndef DYNAMIC_LINK_TEST +#define _LGPL_SOURCE +#else +#define debug_yield_read() +#endif +#include "../urcu.h" +#include "../urcu-reclaim.h" + +struct test_array { + int a; +}; + +static volatile int test_go, test_stop; + +static unsigned long wdelay; + +static struct test_array *test_rcu_pointer; + +static unsigned long duration; + +/* read-side C.S. duration, in loops */ +static unsigned long rduration; + +static inline void loop_sleep(unsigned long l) +{ + while(l-- != 0) + cpu_relax(); +} + +static int verbose_mode; + +#define printf_verbose(fmt, args...) \ + do { \ + if (verbose_mode) \ + printf(fmt, args); \ + } while (0) + +static unsigned int cpu_affinities[NR_CPUS]; +static unsigned int next_aff = 0; +static int use_affinity = 0; + +pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void set_affinity(void) +{ + cpu_set_t mask; + int cpu; + int ret; + + if (!use_affinity) + return; + + ret = pthread_mutex_lock(&affinity_mutex); + if (ret) { + perror("Error in pthread mutex lock"); + exit(-1); + } + cpu = cpu_affinities[next_aff++]; + ret = pthread_mutex_unlock(&affinity_mutex); + if (ret) { + perror("Error in pthread mutex unlock"); + exit(-1); + } + CPU_ZERO(&mask); + CPU_SET(cpu, &mask); + sched_setaffinity(0, sizeof(mask), &mask); +} + +/* + * returns 0 if test should end. + */ +static int test_duration_write(void) +{ + return !test_stop; +} + +static int test_duration_read(void) +{ + return !test_stop; +} + +static unsigned long long __thread nr_writes; +static unsigned long long __thread nr_reads; + +static +unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes; + +static unsigned int nr_readers; +static unsigned int nr_writers; + +pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; + +void rcu_copy_mutex_lock(void) +{ + int ret; + ret = pthread_mutex_lock(&rcu_copy_mutex); + if (ret) { + perror("Error in pthread mutex lock"); + exit(-1); + } +} + +void rcu_copy_mutex_unlock(void) +{ + int ret; + + ret = pthread_mutex_unlock(&rcu_copy_mutex); + if (ret) { + perror("Error in pthread mutex unlock"); + exit(-1); + } +} + +void *thr_reader(void *_count) +{ + unsigned long long *count = _count; + struct test_array *local_ptr; + + printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", + "reader", pthread_self(), (unsigned long)gettid()); + + set_affinity(); + + rcu_register_thread(); + + while (!test_go) + { + } + smp_mb(); + + for (;;) { + rcu_read_lock(); + local_ptr = rcu_dereference(test_rcu_pointer); + debug_yield_read(); + if (local_ptr) + assert(local_ptr->a == 8); + if (unlikely(rduration)) + loop_sleep(rduration); + rcu_read_unlock(); + nr_reads++; + if (unlikely(!test_duration_read())) + break; + } + + rcu_unregister_thread(); + + *count = nr_reads; + printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", + "reader", pthread_self(), (unsigned long)gettid()); + return ((void*)1); + +} + +void *thr_writer(void *data) +{ + unsigned long wtidx = (unsigned long)data; + struct test_array *new, *old = NULL; + + printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", + "writer", pthread_self(), (unsigned long)gettid()); + + set_affinity(); + + rcu_reclaim_register_thread(); + + while (!test_go) + { + } + smp_mb(); + + for (;;) { + new = malloc(sizeof(*new)); + new->a = 8; + old = rcu_xchg_pointer(&test_rcu_pointer, new); + rcu_reclaim_queue(old); + nr_writes++; + if (unlikely(!test_duration_write())) + break; + if (unlikely(wdelay)) + loop_sleep(wdelay); + } + + rcu_reclaim_unregister_thread(); + + printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", + "writer", pthread_self(), (unsigned long)gettid()); + tot_nr_writes[wtidx] = nr_writes; + return ((void*)2); +} + +void show_usage(int argc, char **argv) +{ + printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); +#ifdef DEBUG_YIELD + printf(" [-r] [-w] (yield reader and/or writer)"); +#endif + printf(" [-d delay] (writer period (us))"); + printf(" [-c duration] (reader C.S. duration (in loops))"); + printf(" [-v] (verbose output)"); + printf(" [-a cpu#] [-a cpu#]... (affinity)"); + printf("\n"); +} + +int main(int argc, char **argv) +{ + int err; + pthread_t *tid_reader, *tid_writer; + void *tret; + unsigned long long *count_reader; + unsigned long long tot_reads = 0, tot_writes = 0; + int i, a; + + if (argc < 4) { + show_usage(argc, argv); + return -1; + } + + err = sscanf(argv[1], "%u", &nr_readers); + if (err != 1) { + show_usage(argc, argv); + return -1; + } + + err = sscanf(argv[2], "%u", &nr_writers); + if (err != 1) { + show_usage(argc, argv); + return -1; + } + + err = sscanf(argv[3], "%lu", &duration); + if (err != 1) { + show_usage(argc, argv); + return -1; + } + + for (i = 4; i < argc; i++) { + if (argv[i][0] != '-') + continue; + switch (argv[i][1]) { +#ifdef DEBUG_YIELD + case 'r': + yield_active |= YIELD_READ; + break; + case 'w': + yield_active |= YIELD_WRITE; + break; +#endif + case 'a': + if (argc < i + 2) { + show_usage(argc, argv); + return -1; + } + a = atoi(argv[++i]); + cpu_affinities[next_aff++] = a; + use_affinity = 1; + printf_verbose("Adding CPU %d affinity\n", a); + break; + case 'c': + if (argc < i + 2) { + show_usage(argc, argv); + return -1; + } + rduration = atol(argv[++i]); + break; + case 'd': + if (argc < i + 2) { + show_usage(argc, argv); + return -1; + } + wdelay = atol(argv[++i]); + break; + case 'v': + verbose_mode = 1; + break; + } + } + + printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", + duration, nr_readers, nr_writers); + printf_verbose("Writer delay : %lu loops.\n", wdelay); + printf_verbose("Reader duration : %lu loops.\n", rduration); + printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", + "main", pthread_self(), (unsigned long)gettid()); + + tid_reader = malloc(sizeof(*tid_reader) * nr_readers); + tid_writer = malloc(sizeof(*tid_writer) * nr_writers); + count_reader = malloc(sizeof(*count_reader) * nr_readers); + tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers); + + next_aff = 0; + + for (i = 0; i < nr_readers; i++) { + err = pthread_create(&tid_reader[i], NULL, thr_reader, + &count_reader[i]); + if (err != 0) + exit(1); + } + for (i = 0; i < nr_writers; i++) { + err = pthread_create(&tid_writer[i], NULL, thr_writer, + (void *)(long)i); + if (err != 0) + exit(1); + } + + smp_mb(); + + test_go = 1; + + sleep(duration); + + test_stop = 1; + + for (i = 0; i < nr_readers; i++) { + err = pthread_join(tid_reader[i], &tret); + if (err != 0) + exit(1); + tot_reads += count_reader[i]; + } + for (i = 0; i < nr_writers; i++) { + err = pthread_join(tid_writer[i], &tret); + if (err != 0) + exit(1); + tot_writes += tot_nr_writes[i]; + } + + printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, + tot_writes); + printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " + "nr_writers %3u " + "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", + argv[0], duration, nr_readers, rduration, + nr_writers, wdelay, tot_reads, tot_writes, + tot_reads + tot_writes); + free(tid_reader); + free(tid_writer); + free(count_reader); + free(tot_nr_writes); + + return 0; +} diff --git a/urcu-reclaim-static.h b/urcu-reclaim-static.h new file mode 100644 index 0000000..d23828e --- /dev/null +++ b/urcu-reclaim-static.h @@ -0,0 +1,128 @@ +#ifndef _URCU_RECLAIM_STATIC_H +#define _URCU_RECLAIM_STATIC_H + +/* + * urcu-static.h + * + * Userspace RCU header. + * + * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu.h for linking + * dynamically with the userspace rcu library. + * + * Copyright (c) 2009 Mathieu Desnoyers + * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. + * + * 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 + * + * IBM's contributions to this file may be relicensed under LGPLv2 or later. + */ + +#include +#include + +#include +#include + + +/* + * Number of entries in the per-thread reclaim queue. Must be power of 2. + */ +#define RECLAIM_QUEUE_SIZE (1 << 12) +#define RECLAIM_QUEUE_MASK (RECLAIM_QUEUE_SIZE - 1) + +/* + * Identify a shared load. A smp_rmc() or smp_mc() should come before the load. + */ +#define _LOAD_SHARED(p) ACCESS_ONCE(p) + +/* + * Load a data from shared memory, doing a cache flush if required. + */ +#define LOAD_SHARED(p) \ + ({ \ + smp_rmc(); \ + _LOAD_SHARED(p); \ + }) + +/* + * Identify a shared store. A smp_wmc() or smp_mc() should follow the store. + */ +#define _STORE_SHARED(x, v) ({ ACCESS_ONCE(x) = (v); }) + +/* + * Store v into x, where x is located in shared memory. Performs the required + * cache flush after writing. Returns v. + */ +#define STORE_SHARED(x, v) \ + ({ \ + _STORE_SHARED(x, v); \ + smp_wmc(); \ + (v); \ + }) + +/* + * This code section can only be included in LGPL 2.1 compatible source code. + * See below for the function call wrappers which can be used in code meant to + * be only linked with the Userspace RCU library. This comes with a small + * performance degradation on the read-side due to the added function calls. + * This is required to permit relinking with newer versions of the library. + */ + +#ifdef DEBUG_RCU +#define rcu_assert(args...) assert(args) +#else +#define rcu_assert(args...) +#endif + +struct reclaim_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 void rcu_reclaim_barrier_thread(void); + +/* + * not signal-safe. + */ +static inline void _rcu_reclaim_queue(void *p) +{ + unsigned long head, tail; + + /* + * Head is only modified by ourself. Tail can be modified by reclamation + * thread. + */ + head = reclaim_queue.head; + tail = LOAD_SHARED(reclaim_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); + } + + smp_wmb(); /* Publish new pointer before write q[] */ + _STORE_SHARED(reclaim_queue.q[head & RECLAIM_QUEUE_MASK], p); + smp_wmb(); /* Write q[] before head. */ + STORE_SHARED(reclaim_queue.head, head + 1); +} + +#endif /* _URCU_RECLAIM_STATIC_H */ diff --git a/urcu-reclaim.c b/urcu-reclaim.c new file mode 100644 index 0000000..b2d5894 --- /dev/null +++ b/urcu-reclaim.c @@ -0,0 +1,300 @@ +/* + * 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__((constructor)) urcu_reclaim_init(void); +void __attribute__((destructor)) urcu_reclaim_exit(void); + +extern void synchronize_rcu(void); + +static int init_done; + +/* + * 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 reader. Read by both the reader and the + * writers. + */ +struct reclaim_queue __thread reclaim_queue; + +/* Thread IDs of registered readers */ +#define INIT_NUM_THREADS 4 + +struct reader_registry { + pthread_t tid; + struct reclaim_queue *reclaim_queue; + unsigned long last_head; +}; + +static struct reader_registry *registry; +static int num_readers, alloc_readers; + +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 reader_registry *index; + + if (!registry) + return; + + internal_urcu_lock(&urcu_reclaim_mutex); + for (index = registry; index < registry + num_readers; index++) + index->last_head = LOAD_SHARED(index->reclaim_queue->head); + synchronize_rcu(); + for (index = registry; index < registry + num_readers; 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_reader(pthread_t id) +{ + struct reader_registry *oldarray; + + if (!registry) { + alloc_readers = INIT_NUM_THREADS; + num_readers = 0; + registry = + malloc(sizeof(struct reader_registry) * alloc_readers); + } + if (alloc_readers < num_readers + 1) { + oldarray = registry; + registry = malloc(sizeof(struct reader_registry) + * (alloc_readers << 1)); + memcpy(registry, oldarray, + sizeof(struct reader_registry) * alloc_readers); + alloc_readers <<= 1; + free(oldarray); + } + registry[num_readers].tid = id; + /* reference to the TLS of _this_ reader thread. */ + registry[num_readers].reclaim_queue = &reclaim_queue; + num_readers++; +} + +/* + * Never shrink (implementation limitation). + * This is O(nb threads). Eventually use a hash table. + */ +static void rcu_remove_reader(pthread_t id) +{ + struct reader_registry *index; + + assert(registry != NULL); + for (index = registry; index < registry + num_readers; index++) { + if (pthread_equal(index->tid, id)) { + memcpy(index, ®istry[num_readers - 1], + sizeof(struct reader_registry)); + registry[num_readers - 1].tid = 0; + registry[num_readers - 1].reclaim_queue = NULL; + num_readers--; + 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 readers; + + internal_urcu_lock(&reclaim_thread_mutex); + internal_urcu_lock(&urcu_reclaim_mutex); + /* In case gcc does not support constructor attribute */ + urcu_reclaim_init(); + reclaim_queue.q = malloc(sizeof(void *) * RECLAIM_QUEUE_SIZE); + rcu_add_reader(pthread_self()); + readers = num_readers; + internal_urcu_unlock(&urcu_reclaim_mutex); + + if (readers == 1) + start_reclaim_thread(); + internal_urcu_unlock(&reclaim_thread_mutex); +} + +void rcu_reclaim_unregister_thread(void) +{ + int readers; + + internal_urcu_lock(&reclaim_thread_mutex); + internal_urcu_lock(&urcu_reclaim_mutex); + rcu_remove_reader(pthread_self()); + _rcu_reclaim_barrier_thread(); + free(reclaim_queue.q); + reclaim_queue.q = NULL; + readers = num_readers; + internal_urcu_unlock(&urcu_reclaim_mutex); + + if (readers == 0) + stop_reclaim_thread(); + internal_urcu_unlock(&reclaim_thread_mutex); +} + +/* + * urcu_init constructor. Called when the library is linked, but also when + * reader threads are calling rcu_register_thread(). Should only be called by a + * single thread at a given time. This is ensured by holing the + * internal_urcu_lock(&urcu_reclaim_mutex) from rcu_register_thread() or by + * running at library load time, which should not be executed by multiple + * threads nor concurrently with rcu_register_thread() anyway. + */ +void urcu_reclaim_init(void) +{ + if (init_done) + return; + init_done = 1; +} + +void urcu_reclaim_exit(void) +{ + free(registry); +} diff --git a/urcu-reclaim.h b/urcu-reclaim.h new file mode 100644 index 0000000..adca25b --- /dev/null +++ b/urcu-reclaim.h @@ -0,0 +1,72 @@ +#ifndef _URCU_RECLAIM_H +#define _URCU_RECLAIM_H + +/* + * urcu-reclaim.h + * + * Userspace RCU header - memory reclamation + * + * Copyright (c) 2009 Mathieu Desnoyers + * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. + * + * LGPL-compatible code should include this header with : + * + * #define _LGPL_SOURCE + * #include + * + * 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 + +/* + * Important ! + * + * Each thread queuing memory reclamation must be registered with + * rcu_reclaim_register_thread(). rcu_reclaim_unregister_thread() should be + * called before the thread exits. + */ + +#ifdef _LGPL_SOURCE + +#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 + +#else /* !_LGPL_SOURCE */ + +/* + * library wrappers to be used by non-LGPL compatible source code. + */ + +extern void rcu_reclaim_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); + +#endif /* _URCU_RECLAIM_H */