4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
15 #include <sys/types.h>
20 #include <sys/syscall.h>
22 #if defined(_syscall0)
23 _syscall0(pid_t
, gettid
)
24 #elif defined(__NR_gettid)
25 static inline pid_t
gettid(void)
27 return syscall(__NR_gettid
);
30 #warning "use pid as tid"
31 static inline pid_t
gettid(void)
45 static struct test_array
*test_rcu_pointer
;
50 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
52 void rcu_copy_mutex_lock(void)
55 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
57 perror("Error in pthread mutex lock");
62 void rcu_copy_mutex_unlock(void)
66 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
68 perror("Error in pthread mutex unlock");
73 void *thr_reader(void *arg
)
76 struct test_array
*local_ptr
;
78 printf("thread %s, thread id : %lx, tid %lu\n",
79 "reader", pthread_self(), (unsigned long)gettid());
82 urcu_register_thread();
84 for (i
= 0; i
< 100000; i
++) {
85 for (j
= 0; j
< 100000000; j
++) {
86 rcu_read_lock(&qparity
);
87 local_ptr
= rcu_dereference(test_rcu_pointer
);
89 assert(local_ptr
->a
== 8);
90 assert(local_ptr
->b
== 12);
91 assert(local_ptr
->c
[55] == 2);
93 rcu_read_unlock(&qparity
);
97 urcu_unregister_thread();
103 void *thr_writer(void *arg
)
106 struct test_array
*new, *old
;
108 printf("thread %s, thread id : %lx, tid %lu\n",
109 "writer", pthread_self(), (unsigned long)gettid());
112 for (i
= 0; i
< 10000000; i
++) {
113 new = malloc(sizeof(struct test_array
));
114 rcu_copy_mutex_lock();
115 old
= test_rcu_pointer
;
118 assert(old
->b
== 12);
119 assert(old
->c
[55] == 2);
124 old
= urcu_publish_content((void **)&test_rcu_pointer
, new);
125 rcu_copy_mutex_unlock();
126 /* can be done after unlock */
142 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
146 printf("thread %-6s, thread id : %lx, tid %lu\n",
147 "main", pthread_self(), (unsigned long)gettid());
149 for (i
= 0; i
< NR_READ
; i
++) {
150 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
, NULL
);
154 for (i
= 0; i
< NR_WRITE
; i
++) {
155 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
162 for (i
= 0; i
< NR_READ
; i
++) {
163 err
= pthread_join(tid_reader
[i
], &tret
);
167 for (i
= 0; i
< NR_WRITE
; i
++) {
168 err
= pthread_join(tid_writer
[i
], &tret
);
172 free(test_rcu_pointer
);