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)
39 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
41 void rcu_copy_mutex_lock(void)
44 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
46 perror("Error in pthread mutex lock");
51 void rcu_copy_mutex_unlock(void)
55 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
57 perror("Error in pthread mutex unlock");
66 static struct test_array
*test_rcu_pointer
;
68 #define OUTER_READ_LOOP 2000U
69 #define INNER_READ_LOOP 100000U
70 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
72 #define WRITE_LOOP 2000U
77 static cycles_t reader_time
[NR_READ
] __attribute__((aligned(128)));
79 void *thr_reader(void *arg
)
82 struct test_array
*local_ptr
;
83 cycles_t time1
, time2
;
85 printf("thread_begin %s, thread id : %lx, tid %lu\n",
86 "reader", pthread_self(), (unsigned long)gettid());
89 urcu_register_thread();
92 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
93 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
95 local_ptr
= rcu_dereference(test_rcu_pointer
);
97 assert(local_ptr
->a
== 8);
102 time2
= get_cycles();
104 urcu_unregister_thread();
106 reader_time
[(unsigned long)arg
] = time2
- time1
;
109 printf("thread_end %s, thread id : %lx, tid %lu\n",
110 "reader", pthread_self(), (unsigned long)gettid());
115 void *thr_writer(void *arg
)
118 struct test_array
*new, *old
;
120 printf("thread_begin %s, thread id : %lx, tid %lu\n",
121 "writer", pthread_self(), (unsigned long)gettid());
124 for (i
= 0; i
< WRITE_LOOP
; i
++) {
125 new = malloc(sizeof(struct test_array
));
126 rcu_copy_mutex_lock();
127 old
= test_rcu_pointer
;
132 old
= urcu_publish_content(&test_rcu_pointer
, new);
133 rcu_copy_mutex_unlock();
134 /* can be done after unlock */
142 printf("thread_end %s, thread id : %lx, tid %lu\n",
143 "writer", pthread_self(), (unsigned long)gettid());
150 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
153 cycles_t tot_time
= 0;
155 printf("thread %-6s, thread id : %lx, tid %lu\n",
156 "main", pthread_self(), (unsigned long)gettid());
158 for (i
= 0; i
< NR_READ
; i
++) {
159 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
164 for (i
= 0; i
< NR_WRITE
; i
++) {
165 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
172 for (i
= 0; i
< NR_READ
; i
++) {
173 err
= pthread_join(tid_reader
[i
], &tret
);
176 tot_time
+= reader_time
[i
];
178 for (i
= 0; i
< NR_WRITE
; i
++) {
179 err
= pthread_join(tid_writer
[i
], &tret
);
183 free(test_rcu_pointer
);
184 printf("Time per read : %g cycles\n",
185 (double)tot_time
/ ((double)NR_READ
* (double)READ_LOOP
));