remove ugly gcc warning removal ack, simply cast the caller parameter
[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>
ac260fd9
MD
20#include "urcu.h"
21
41718ff9
MD
22struct test_array {
23 int a;
24 int b;
25 char c[200];
26};
27
28static struct test_array *test_rcu_pointer;
29
b1b5ce8f 30#define NR_READ 10
8c8eed97 31#define NR_WRITE 9
f69f195a
MD
32
33
34void *thr_reader(void *arg)
35{
8c8eed97 36 int qparity, i, j;
41718ff9
MD
37 struct test_array *local_ptr;
38
f69f195a 39 printf("thread %s, thread id : %lu, pid %lu\n",
5e7e64b9 40 "reader", pthread_self(), (unsigned long)getpid());
f69f195a
MD
41 sleep(2);
42
43 urcu_register_thread();
44
8c8eed97
MD
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);
41718ff9 55 }
41718ff9 56 }
f69f195a
MD
57
58 urcu_unregister_thread();
41718ff9 59
f69f195a
MD
60 return ((void*)1);
61
62}
63
64void *thr_writer(void *arg)
65{
66 int i;
41718ff9 67 struct test_array *new, *old;
f69f195a
MD
68
69 printf("thread %s, thread id : %lu, pid %lu\n",
5e7e64b9 70 "writer", pthread_self(), (unsigned long)getpid());
f69f195a
MD
71 sleep(2);
72
8c8eed97 73 for (i = 0; i < 100000; i++) {
41718ff9
MD
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 }
8c8eed97
MD
82 new->a = 8;
83 new->b = 12;
84 new->c[55] = 2;
cdcb92bb 85 old = urcu_publish_content((void **)&test_rcu_pointer, new);
41718ff9
MD
86 rcu_write_unlock();
87 /* can be done after unlock */
8c8eed97
MD
88 if (old) {
89 old->a = 0;
90 old->b = 0;
91 old->c[55] = 0;
92 }
41718ff9 93 free(old);
8c8eed97 94 usleep(1);
f69f195a
MD
95 }
96
97 return ((void*)2);
98}
ac260fd9
MD
99
100int main()
101{
f69f195a
MD
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);
ac260fd9 119
f69f195a
MD
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 }
ac260fd9 130
f69f195a 131 return 0;
ac260fd9 132}
This page took 0.040322 seconds and 4 git commands to generate.