ea5a784f785dbf0f57911074fc3dee4f4d906b3f
[urcu.git] / test_urcu.c
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
11 #include <stdio.h>
12 #include <pthread.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <assert.h>
20 #include <sys/syscall.h>
21
22 #if defined(_syscall0)
23 _syscall0(pid_t, gettid)
24 #elif defined(__NR_gettid)
25 static inline pid_t gettid(void)
26 {
27 return syscall(__NR_gettid);
28 }
29 #else
30 #warning "use pid as tid"
31 static inline pid_t gettid(void)
32 {
33 return getpid();
34 }
35 #endif
36
37 #include "urcu.h"
38
39 struct test_array {
40 int a;
41 int b;
42 char c[200];
43 };
44
45 static struct test_array *test_rcu_pointer;
46
47 #define NR_READ 10
48 #define NR_WRITE 9
49
50
51 void *thr_reader(void *arg)
52 {
53 int qparity, i, j;
54 struct test_array *local_ptr;
55
56 printf("thread %s, thread id : %lx, tid %lu\n",
57 "reader", pthread_self(), (unsigned long)gettid());
58 sleep(2);
59
60 urcu_register_thread();
61
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);
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);
72 }
73 }
74
75 urcu_unregister_thread();
76
77 return ((void*)1);
78
79 }
80
81 void *thr_writer(void *arg)
82 {
83 int i;
84 struct test_array *new, *old;
85
86 printf("thread %s, thread id : %lx, tid %lu\n",
87 "writer", pthread_self(), (unsigned long)gettid());
88 sleep(2);
89
90 for (i = 0; i < 10000000; i++) {
91 new = malloc(sizeof(struct test_array));
92 rcu_write_lock();
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 }
99 new->c[55] = 2;
100 new->b = 12;
101 new->a = 8;
102 old = urcu_publish_content((void **)&test_rcu_pointer, new);
103 rcu_write_unlock();
104 /* can be done after unlock */
105 if (old) {
106 old->a = 0;
107 old->b = 0;
108 old->c[55] = 0;
109 }
110 free(old);
111 usleep(1);
112 }
113
114 return ((void*)2);
115 }
116
117 int main()
118 {
119 int err;
120 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
121 void *tret;
122 int i;
123
124 printf("thread %-6s, thread id : %lx, tid %lu\n",
125 "main", pthread_self(), (unsigned long)gettid());
126
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);
139
140 for (i = 0; i < NR_WRITE; i++) {
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 }
150
151 return 0;
152 }
This page took 0.031176 seconds and 3 git commands to generate.