X-Git-Url: https://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=test_urcu.c;h=17061f81a250d5f12f7e4836d7caf81835cd43d4;hp=d5d07fe52daf4d985266f5e2c896e9e4468a1771;hb=f5f51ac3ef3e0db8a0940c456bce22bc9000fdbf;hpb=ac260fd92d9a3c9170eae13a2766ce37ce9d7cf3 diff --git a/test_urcu.c b/test_urcu.c index d5d07fe..17061f8 100644 --- a/test_urcu.c +++ b/test_urcu.c @@ -1,9 +1,132 @@ +/* + * test_urcu.c + * + * Userspace RCU library - test program + * + * Copyright February 2009 - Mathieu Desnoyers + * + * Distributed under GPLv2 + */ + +#include +#include +#include +#include +#include +#include +#include #include +#include #include "urcu.h" +struct test_array { + int a; + int b; + char c[200]; +}; + +static struct test_array *test_rcu_pointer; + +#define NR_READ 10 +#define NR_WRITE 9 + + +void *thr_reader(void *arg) +{ + int qparity, i, j; + struct test_array *local_ptr; + + printf("thread %s, thread id : %lu, pid %lu\n", + "reader", pthread_self(), (unsigned long)getpid()); + sleep(2); + + urcu_register_thread(); + + for (i = 0; i < 1000; i++) { + for (j = 0; j < 100000000; j++) { + 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); + +} + +void *thr_writer(void *arg) +{ + int i; + struct test_array *new, *old; + + printf("thread %s, thread id : %lu, pid %lu\n", + "writer", pthread_self(), (unsigned long)getpid()); + sleep(2); + + for (i = 0; i < 100000; 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); + } + new->a = 8; + new->b = 12; + new->c[55] = 2; + old = urcu_publish_content((void **)&test_rcu_pointer, new); + rcu_write_unlock(); + /* can be done after unlock */ + if (old) { + old->a = 0; + old->b = 0; + old->c[55] = 0; + } + free(old); + usleep(1); + } + + return ((void*)2); +} int main() { + int err; + pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE]; + void *tret; + int i; + + for (i = 0; i < NR_READ; i++) { + err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL); + if (err != 0) + exit(1); + } + for (i = 0; i < NR_WRITE; i++) { + err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL); + if (err != 0) + exit(1); + } + + sleep(10); + for (i = 0; i < NR_WRITE; i++) { + err = pthread_join(tid_reader[i], &tret); + if (err != 0) + exit(1); + } + for (i = 0; i < NR_WRITE; i++) { + err = pthread_join(tid_writer[i], &tret); + if (err != 0) + exit(1); + } + return 0; }