1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Userspace RCU library - test program
13 #include <sys/types.h>
20 #include <urcu/arch.h>
21 #include <urcu/assert.h>
23 #include "thread-id.h"
32 * static rwlock initializer is broken on Cygwin. Use runtime
35 pthread_rwlock_t lock
;
37 static struct test_array test_array
= { 8 };
39 #define OUTER_READ_LOOP 200U
40 #define INNER_READ_LOOP 100000U
41 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
43 #define OUTER_WRITE_LOOP 10U
44 #define INNER_WRITE_LOOP 200U
45 #define WRITE_LOOP ((unsigned long long)OUTER_WRITE_LOOP * INNER_WRITE_LOOP)
50 #define NR_READ num_read
51 #define NR_WRITE num_write
53 static caa_cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *reader_time
;
54 static caa_cycles_t
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *writer_time
;
57 void *thr_reader(void *arg
)
61 caa_cycles_t time1
, time2
;
63 printf("thread_begin %s, tid %lu\n",
64 "reader", urcu_get_thread_id());
67 time1
= caa_get_cycles();
68 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
69 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
70 ret
= pthread_rwlock_rdlock(&lock
);
72 fprintf(stderr
, "reader pthread_rwlock_rdlock: %s\n", strerror(ret
));
76 urcu_posix_assert(test_array
.a
== 8);
78 ret
= pthread_rwlock_unlock(&lock
);
80 fprintf(stderr
, "reader pthread_rwlock_unlock: %s\n", strerror(ret
));
85 time2
= caa_get_cycles();
87 reader_time
[(unsigned long)arg
] = time2
- time1
;
90 printf("thread_end %s, tid %lu\n",
91 "reader", urcu_get_thread_id());
97 void *thr_writer(void *arg
)
101 caa_cycles_t time1
, time2
;
103 printf("thread_begin %s, tid %lu\n",
104 "writer", urcu_get_thread_id());
107 for (i
= 0; i
< OUTER_WRITE_LOOP
; i
++) {
108 for (j
= 0; j
< INNER_WRITE_LOOP
; j
++) {
109 time1
= caa_get_cycles();
110 ret
= pthread_rwlock_wrlock(&lock
);
112 fprintf(stderr
, "writer pthread_rwlock_wrlock: %s\n", strerror(ret
));
118 ret
= pthread_rwlock_unlock(&lock
);
120 fprintf(stderr
, "writer pthread_rwlock_unlock: %s\n", strerror(ret
));
124 time2
= caa_get_cycles();
125 writer_time
[(unsigned long)arg
] += time2
- time1
;
130 printf("thread_end %s, tid %lu\n",
131 "writer", urcu_get_thread_id());
135 int main(int argc
, char **argv
)
138 pthread_t
*tid_reader
, *tid_writer
;
141 caa_cycles_t tot_rtime
= 0;
142 caa_cycles_t tot_wtime
= 0;
145 printf("Usage : %s nr_readers nr_writers\n", argv
[0]);
148 num_read
= atoi(argv
[1]);
149 num_write
= atoi(argv
[2]);
151 err
= pthread_rwlock_init(&lock
, NULL
);
153 fprintf(stderr
, "pthread_rwlock_init: (%d) %s\n", err
, strerror(err
));
157 reader_time
= calloc(num_read
, sizeof(*reader_time
));
158 writer_time
= calloc(num_write
, sizeof(*writer_time
));
159 tid_reader
= calloc(num_read
, sizeof(*tid_reader
));
160 tid_writer
= calloc(num_write
, sizeof(*tid_writer
));
162 printf("thread %-6s, tid %lu\n",
163 "main", urcu_get_thread_id());
165 for (i
= 0; i
< NR_READ
; i
++) {
166 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
171 for (i
= 0; i
< NR_WRITE
; i
++) {
172 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
180 for (i
= 0; i
< NR_READ
; i
++) {
181 err
= pthread_join(tid_reader
[i
], &tret
);
184 tot_rtime
+= reader_time
[i
];
186 for (i
= 0; i
< NR_WRITE
; i
++) {
187 err
= pthread_join(tid_writer
[i
], &tret
);
190 tot_wtime
+= writer_time
[i
];
192 printf("Time per read : %g cycles\n",
193 (double)tot_rtime
/ ((double)NR_READ
* (double)READ_LOOP
));
194 printf("Time per write : %g cycles\n",
195 (double)tot_wtime
/ ((double)NR_WRITE
* (double)WRITE_LOOP
));
197 err
= pthread_rwlock_destroy(&lock
);
199 fprintf(stderr
, "pthread_rwlock_destroy: (%d) %s\n", err
, strerror(err
));
This page took 0.047981 seconds and 4 git commands to generate.