update Makefile, -Wall
[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 "urcu.h"
21
22 struct test_array {
23 int a;
24 int b;
25 char c[200];
26 };
27
28 static struct test_array *test_rcu_pointer;
29
30 #define NR_READ 10
31 #define NR_WRITE 9
32
33
34 void *thr_reader(void *arg)
35 {
36 int qparity, i, j;
37 struct test_array *local_ptr;
38
39 printf("thread %s, thread id : %lu, pid %lu\n",
40 "reader", pthread_self(), (unsigned long)getpid());
41 sleep(2);
42
43 urcu_register_thread();
44
45 for (i = 0; i < 1000; i++) {
46 for (j = 0; j < 100000000; j++) {
47 qparity = rcu_read_lock();
48 local_ptr = rcu_dereference(test_rcu_pointer);
49 if (local_ptr) {
50 assert(local_ptr->a == 8);
51 assert(local_ptr->b == 12);
52 assert(local_ptr->c[55] == 2);
53 }
54 rcu_read_unlock(qparity);
55 }
56 }
57
58 urcu_unregister_thread();
59
60 return ((void*)1);
61
62 }
63
64 void *thr_writer(void *arg)
65 {
66 int i;
67 struct test_array *new, *old;
68
69 printf("thread %s, thread id : %lu, pid %lu\n",
70 "writer", pthread_self(), (unsigned long)getpid());
71 sleep(2);
72
73 for (i = 0; i < 100000; i++) {
74 rcu_write_lock();
75 new = malloc(sizeof(struct test_array));
76 old = test_rcu_pointer;
77 if (old) {
78 assert(old->a == 8);
79 assert(old->b == 12);
80 assert(old->c[55] == 2);
81 }
82 new->a = 8;
83 new->b = 12;
84 new->c[55] = 2;
85 old = urcu_publish_content((void **)&test_rcu_pointer, new);
86 rcu_write_unlock();
87 /* can be done after unlock */
88 if (old) {
89 old->a = 0;
90 old->b = 0;
91 old->c[55] = 0;
92 }
93 free(old);
94 usleep(1);
95 }
96
97 return ((void*)2);
98 }
99
100 int main()
101 {
102 int err;
103 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
104 void *tret;
105 int i;
106
107 for (i = 0; i < NR_READ; i++) {
108 err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL);
109 if (err != 0)
110 exit(1);
111 }
112 for (i = 0; i < NR_WRITE; i++) {
113 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
114 if (err != 0)
115 exit(1);
116 }
117
118 sleep(10);
119
120 for (i = 0; i < NR_WRITE; i++) {
121 err = pthread_join(tid_reader[i], &tret);
122 if (err != 0)
123 exit(1);
124 }
125 for (i = 0; i < NR_WRITE; i++) {
126 err = pthread_join(tid_writer[i], &tret);
127 if (err != 0)
128 exit(1);
129 }
130
131 return 0;
132 }
This page took 0.042485 seconds and 4 git commands to generate.