X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=test_urcu.c;h=f6be45b1e2769a9f700e4bb0cf0968046db5a2e1;hb=c265818b951224bcde407b1efa14f9daf44949b3;hp=db0b68ca2d18cc58cc0bd163a938375773ec660c;hpb=ad6ce6ae8c2ac890aff362a641019eb8a7473625;p=urcu.git diff --git a/test_urcu.c b/test_urcu.c index db0b68c..f6be45b 100644 --- a/test_urcu.c +++ b/test_urcu.c @@ -17,6 +17,23 @@ #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 { @@ -30,28 +47,50 @@ static struct test_array *test_rcu_pointer; #define NR_READ 10 #define NR_WRITE 9 +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 *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()); + 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(); + rcu_read_lock(&qparity); 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); + rcu_read_unlock(&qparity); } } @@ -66,13 +105,13 @@ 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()); + printf("thread %s, thread id : %lx, tid %lu\n", + "writer", pthread_self(), (unsigned long)gettid()); sleep(2); for (i = 0; i < 10000000; i++) { new = malloc(sizeof(struct test_array)); - rcu_write_lock(); + rcu_copy_mutex_lock(); old = test_rcu_pointer; if (old) { assert(old->a == 8); @@ -83,7 +122,7 @@ void *thr_writer(void *arg) new->b = 12; new->a = 8; old = urcu_publish_content((void **)&test_rcu_pointer, new); - rcu_write_unlock(); + rcu_copy_mutex_unlock(); /* can be done after unlock */ if (old) { old->a = 0; @@ -104,6 +143,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) @@ -117,7 +159,7 @@ int main() sleep(10); - for (i = 0; i < NR_WRITE; i++) { + for (i = 0; i < NR_READ; i++) { err = pthread_join(tid_reader[i], &tret); if (err != 0) exit(1); @@ -127,6 +169,7 @@ int main() if (err != 0) exit(1); } + free(test_rcu_pointer); return 0; }