Add timing tests
[urcu.git] / test_urcu.c
CommitLineData
b257a10b
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * Distributed under GPLv2
9 */
10
ac260fd9 11#include <stdio.h>
f69f195a
MD
12#include <pthread.h>
13#include <stdlib.h>
41718ff9 14#include <string.h>
f69f195a
MD
15#include <sys/types.h>
16#include <sys/wait.h>
17#include <unistd.h>
18#include <stdio.h>
41718ff9 19#include <assert.h>
87bd15cd
BW
20#include <sys/syscall.h>
21
22#if defined(_syscall0)
23_syscall0(pid_t, gettid)
24#elif defined(__NR_gettid)
25static inline pid_t gettid(void)
26{
27 return syscall(__NR_gettid);
28}
29#else
30#warning "use pid as tid"
31static inline pid_t gettid(void)
32{
33 return getpid();
34}
35#endif
36
ac260fd9
MD
37#include "urcu.h"
38
41718ff9
MD
39struct test_array {
40 int a;
41 int b;
42 char c[200];
43};
44
45static struct test_array *test_rcu_pointer;
46
b1b5ce8f 47#define NR_READ 10
8c8eed97 48#define NR_WRITE 9
f69f195a
MD
49
50
51void *thr_reader(void *arg)
52{
8c8eed97 53 int qparity, i, j;
41718ff9
MD
54 struct test_array *local_ptr;
55
87bd15cd
BW
56 printf("thread %s, thread id : %lx, tid %lu\n",
57 "reader", pthread_self(), (unsigned long)gettid());
f69f195a
MD
58 sleep(2);
59
60 urcu_register_thread();
61
ad6ce6ae 62 for (i = 0; i < 100000; i++) {
8c8eed97
MD
63 for (j = 0; j < 100000000; j++) {
64 qparity = rcu_read_lock();
65 local_ptr = rcu_dereference(test_rcu_pointer);
66 if (local_ptr) {
67 assert(local_ptr->a == 8);
68 assert(local_ptr->b == 12);
69 assert(local_ptr->c[55] == 2);
70 }
71 rcu_read_unlock(qparity);
41718ff9 72 }
41718ff9 73 }
f69f195a
MD
74
75 urcu_unregister_thread();
41718ff9 76
f69f195a
MD
77 return ((void*)1);
78
79}
80
81void *thr_writer(void *arg)
82{
83 int i;
41718ff9 84 struct test_array *new, *old;
f69f195a 85
87bd15cd
BW
86 printf("thread %s, thread id : %lx, tid %lu\n",
87 "writer", pthread_self(), (unsigned long)gettid());
f69f195a
MD
88 sleep(2);
89
ad6ce6ae 90 for (i = 0; i < 10000000; i++) {
41718ff9 91 new = malloc(sizeof(struct test_array));
ad6ce6ae 92 rcu_write_lock();
41718ff9
MD
93 old = test_rcu_pointer;
94 if (old) {
95 assert(old->a == 8);
96 assert(old->b == 12);
97 assert(old->c[55] == 2);
98 }
8c8eed97 99 new->c[55] = 2;
ad6ce6ae
MD
100 new->b = 12;
101 new->a = 8;
cdcb92bb 102 old = urcu_publish_content((void **)&test_rcu_pointer, new);
41718ff9
MD
103 rcu_write_unlock();
104 /* can be done after unlock */
8c8eed97
MD
105 if (old) {
106 old->a = 0;
107 old->b = 0;
108 old->c[55] = 0;
109 }
41718ff9 110 free(old);
8c8eed97 111 usleep(1);
f69f195a
MD
112 }
113
114 return ((void*)2);
115}
ac260fd9
MD
116
117int main()
118{
f69f195a
MD
119 int err;
120 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
121 void *tret;
122 int i;
123
87bd15cd
BW
124 printf("thread %-6s, thread id : %lx, tid %lu\n",
125 "main", pthread_self(), (unsigned long)gettid());
126
f69f195a
MD
127 for (i = 0; i < NR_READ; i++) {
128 err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL);
129 if (err != 0)
130 exit(1);
131 }
132 for (i = 0; i < NR_WRITE; i++) {
133 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
134 if (err != 0)
135 exit(1);
136 }
137
138 sleep(10);
ac260fd9 139
20bf310a 140 for (i = 0; i < NR_READ; i++) {
f69f195a
MD
141 err = pthread_join(tid_reader[i], &tret);
142 if (err != 0)
143 exit(1);
144 }
145 for (i = 0; i < NR_WRITE; i++) {
146 err = pthread_join(tid_writer[i], &tret);
147 if (err != 0)
148 exit(1);
149 }
20bf310a 150 free(test_rcu_pointer);
ac260fd9 151
f69f195a 152 return 0;
ac260fd9 153}
This page took 0.028853 seconds and 4 git commands to generate.