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
;
51 void *thr_reader(void *arg
)
54 struct test_array
*local_ptr
;
56 printf("thread %s, thread id : %lx, tid %lu\n",
57 "reader", pthread_self(), (unsigned long)gettid());
60 urcu_register_thread();
62 for (i
= 0; i
< 100000; i
++) {
63 for (j
= 0; j
< 100000000; j
++) {
64 qparity
= rcu_read_lock();
65 local_ptr
= rcu_dereference(test_rcu_pointer
);
67 assert(local_ptr
->a
== 8);
68 assert(local_ptr
->b
== 12);
69 assert(local_ptr
->c
[55] == 2);
71 rcu_read_unlock(qparity
);
75 urcu_unregister_thread();
81 void *thr_writer(void *arg
)
84 struct test_array
*new, *old
;
86 printf("thread %s, thread id : %lx, tid %lu\n",
87 "writer", pthread_self(), (unsigned long)gettid());
90 for (i
= 0; i
< 10000000; i
++) {
91 new = malloc(sizeof(struct test_array
));
93 old
= test_rcu_pointer
;
97 assert(old
->c
[55] == 2);
102 old
= urcu_publish_content((void **)&test_rcu_pointer
, new);
104 /* can be done after unlock */
120 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
124 printf("thread %-6s, thread id : %lx, tid %lu\n",
125 "main", pthread_self(), (unsigned long)gettid());
127 for (i
= 0; i
< NR_READ
; i
++) {
128 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
, NULL
);
132 for (i
= 0; i
< NR_WRITE
; i
++) {
133 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
140 for (i
= 0; i
< NR_READ
; i
++) {
141 err
= pthread_join(tid_reader
[i
], &tret
);
145 for (i
= 0; i
< NR_WRITE
; i
++) {
146 err
= pthread_join(tid_writer
[i
], &tret
);
150 free(test_rcu_pointer
);
This page took 0.046302 seconds and 4 git commands to generate.