4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
15 #include <sys/types.h>
20 #include <sys/syscall.h>
24 #if defined(_syscall0)
25 _syscall0(pid_t
, gettid
)
26 #elif defined(__NR_gettid)
27 static inline pid_t
gettid(void)
29 return syscall(__NR_gettid
);
32 #warning "use pid as tid"
33 static inline pid_t
gettid(void)
45 pthread_rwlock_t lock
= PTHREAD_RWLOCK_INITIALIZER
;
47 static struct test_array test_array
= { 8 };
49 #define OUTER_READ_LOOP 200U
50 #define INNER_READ_LOOP 100000U
51 #define READ_LOOP ((unsigned long long)OUTER_READ_LOOP * INNER_READ_LOOP)
53 #define WRITE_LOOP 2000U
58 static cycles_t reader_time
[NR_READ
] __attribute__((aligned(128)));
60 void *thr_reader(void *arg
)
63 cycles_t time1
, time2
;
65 printf("thread_begin %s, thread id : %lx, tid %lu\n",
66 "reader", pthread_self(), (unsigned long)gettid());
70 for (i
= 0; i
< OUTER_READ_LOOP
; i
++) {
71 for (j
= 0; j
< INNER_READ_LOOP
; j
++) {
72 pthread_rwlock_rdlock(&lock
);
73 assert(test_array
.a
== 8);
74 pthread_rwlock_unlock(&lock
);
79 reader_time
[(unsigned long)arg
] = time2
- time1
;
82 printf("thread_end %s, thread id : %lx, tid %lu\n",
83 "reader", pthread_self(), (unsigned long)gettid());
88 void *thr_writer(void *arg
)
92 printf("thread_begin %s, thread id : %lx, tid %lu\n",
93 "writer", pthread_self(), (unsigned long)gettid());
96 for (i
= 0; i
< WRITE_LOOP
; i
++) {
97 pthread_rwlock_wrlock(&lock
);
99 pthread_rwlock_unlock(&lock
);
103 printf("thread_end %s, thread id : %lx, tid %lu\n",
104 "writer", pthread_self(), (unsigned long)gettid());
111 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
114 cycles_t tot_time
= 0;
116 printf("thread %-6s, thread id : %lx, tid %lu\n",
117 "main", pthread_self(), (unsigned long)gettid());
119 for (i
= 0; i
< NR_READ
; i
++) {
120 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
125 for (i
= 0; i
< NR_WRITE
; i
++) {
126 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
133 for (i
= 0; i
< NR_READ
; i
++) {
134 err
= pthread_join(tid_reader
[i
], &tret
);
137 tot_time
+= reader_time
[i
];
139 for (i
= 0; i
< NR_WRITE
; i
++) {
140 err
= pthread_join(tid_writer
[i
], &tret
);
144 printf("Time per read : %g cycles\n",
145 (double)tot_time
/ ((double)NR_READ
* (double)READ_LOOP
));