X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=test_urcu.c;h=ea5a784f785dbf0f57911074fc3dee4f4d906b3f;hp=f9b0e86c375ccdad4c71752e494810627908ba90;hb=87bd15cdb3e77c3d0a616c57ae3876ddea48a54f;hpb=f69f195a06af55b7501ed2f59ed719970727ce5b diff --git a/test_urcu.c b/test_urcu.c index f9b0e86..ea5a784 100644 --- a/test_urcu.c +++ b/test_urcu.c @@ -1,27 +1,79 @@ +/* + * 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 + +#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 + #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 4 +#define NR_WRITE 9 void *thr_reader(void *arg) { - printf("thread %s, thread id : %lu, pid %lu\n", - "reader", pthread_self(), getpid()); + int qparity, i, j; + struct test_array *local_ptr; + + printf("thread %s, thread id : %lx, tid %lu\n", + "reader", pthread_self(), (unsigned long)gettid()); sleep(2); urcu_register_thread(); - + for (i = 0; i < 100000; 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); } @@ -29,12 +81,34 @@ 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()); + printf("thread %s, thread id : %lx, tid %lu\n", + "writer", pthread_self(), (unsigned long)gettid()); sleep(2); - for (i = 0; i < 1000; i++) { + for (i = 0; i < 10000000; i++) { + new = malloc(sizeof(struct test_array)); + rcu_write_lock(); + old = test_rcu_pointer; + if (old) { + assert(old->a == 8); + assert(old->b == 12); + assert(old->c[55] == 2); + } + new->c[55] = 2; + new->b = 12; + new->a = 8; + 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); @@ -47,6 +121,9 @@ int main() void *tret; int i; + printf("thread %-6s, thread id : %lx, tid %lu\n", + "main", pthread_self(), (unsigned long)gettid()); + for (i = 0; i < NR_READ; i++) { err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL); if (err != 0)