X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=test_urcu.c;h=8212dfc241748684e4bba7d287ffcb72afe6c406;hp=f9b0e86c375ccdad4c71752e494810627908ba90;hb=41718ff94c4a07cb5f56d68084267798e471d1b1;hpb=c777f0b12ffe55408365a9a29340c2060e1ffda3 diff --git a/test_urcu.c b/test_urcu.c index f9b0e86..8212dfc 100644 --- a/test_urcu.c +++ b/test_urcu.c @@ -1,27 +1,50 @@ #include #include #include +#include #include #include #include #include +#include #include "urcu.h" -#define NR_READ 10 -#define NR_WRITE 4 +struct test_array { + int a; + int b; + char c[200]; +}; + +static struct test_array *test_rcu_pointer; + +#define NR_READ 1000 +#define NR_WRITE 50 void *thr_reader(void *arg) { + int qparity, i; + struct test_array *local_ptr; + printf("thread %s, thread id : %lu, pid %lu\n", "reader", pthread_self(), getpid()); sleep(2); urcu_register_thread(); - + for (i = 0; i < 1000; i++) { + qparity = rcu_read_lock(); + local_ptr = rcu_dereference(test_rcu_pointer); + if (local_ptr) { + assert(local_ptr->a == 8); + assert(local_ptr->b == 12); + assert(local_ptr->c[55] == 2); + } + rcu_read_unlock(qparity); + } urcu_unregister_thread(); + return ((void*)1); } @@ -29,12 +52,28 @@ void *thr_reader(void *arg) void *thr_writer(void *arg) { int i; + struct test_array *new, *old; printf("thread %s, thread id : %lu, pid %lu\n", "writer", pthread_self(), getpid()); sleep(2); for (i = 0; i < 1000; i++) { + rcu_write_lock(); + new = malloc(sizeof(struct test_array)); + old = test_rcu_pointer; + if (old) { + assert(old->a == 8); + assert(old->b == 12); + assert(old->c[55] == 2); + } + assert(new->a = 8); + assert(new->b = 12); + assert(new->c[55] = 2); + old = urcu_publish_content(&test_rcu_pointer, new); + rcu_write_unlock(); + /* can be done after unlock */ + free(old); } return ((void*)2);