@@ -37,6 +37,7 @@ static inline void thread_barrier(struct barrier_struct *b, size_t n)
37
37
#include <stdint.h>
38
38
#include <stdio.h>
39
39
#include <stdlib.h>
40
+ #include <time.h>
40
41
41
42
#include "rcu.h"
42
43
@@ -55,34 +56,42 @@ static atomic_uint grace_periods[GP_IDX_MAX];
55
56
static void * reader_func (void * argv )
56
57
{
57
58
struct test * tmp ;
59
+ unsigned int local_gp_idx ;
58
60
unsigned int old_prev_count ;
61
+ struct timespec ts = {
62
+ .tv_sec = 0 ,
63
+ .tv_nsec = 30000L ,
64
+ };
59
65
60
66
if (rcu_init ())
61
67
abort ();
62
68
63
69
thread_barrier (& test_barrier , N_READERS + 1 );
70
+ nanosleep (& ts , NULL );
64
71
65
72
rcu_read_lock ();
66
73
67
74
tmp = rcu_dereference (dut );
68
75
69
- if (tmp -> count != atomic_load_explicit (& prev_count , memory_order_acquire )) {
70
- old_prev_count = atomic_exchange_explicit (& prev_count , tmp -> count ,
71
- memory_order_release );
72
- if (tmp -> count != old_prev_count )
73
- atomic_fetch_add_explicit (& gp_idx , 1 , memory_order_release );
74
- if (atomic_load_explicit (& gp_idx , memory_order_acquire ) >
75
- N_UPDATE_RUN ) {
76
- fprintf (stderr , "grace period index (%u) is over bound (%u).\n" ,
77
- atomic_load_explicit (& gp_idx , memory_order_acquire ),
78
- N_UPDATE_RUN );
79
- abort ();
80
- }
76
+ old_prev_count = atomic_load_explicit (& prev_count , memory_order_acquire );
77
+ if (old_prev_count < tmp -> count ) {
78
+ atomic_compare_exchange_strong (& prev_count , & old_prev_count ,
79
+ tmp -> count );
80
+ } else if (tmp -> count < old_prev_count ) {
81
+ fprintf (stderr ,
82
+ "old count (%u) should not be larger than new one (%u).\n" ,
83
+ old_prev_count , tmp -> count );
84
+ abort ();
81
85
}
82
86
83
- atomic_fetch_add_explicit (
84
- & grace_periods [atomic_load_explicit (& gp_idx , memory_order_acquire )], 1 ,
85
- memory_order_relaxed );
87
+ local_gp_idx = atomic_load_explicit (& gp_idx , memory_order_acquire );
88
+ if (local_gp_idx > N_UPDATE_RUN ) {
89
+ fprintf (stderr , "grace period index (%u) is over bound (%u).\n" ,
90
+ local_gp_idx , N_UPDATE_RUN );
91
+ abort ();
92
+ }
93
+ atomic_fetch_add_explicit (& grace_periods [local_gp_idx ], 1 ,
94
+ memory_order_relaxed );
86
95
87
96
rcu_read_unlock ();
88
97
@@ -103,6 +112,7 @@ static void *updater_func(void *argv)
103
112
newval -> count = i ;
104
113
oldp = rcu_assign_pointer (dut , newval );
105
114
synchronize_rcu ();
115
+ atomic_fetch_add_explicit (& gp_idx , 1 , memory_order_release );
106
116
free (oldp );
107
117
}
108
118
@@ -140,8 +150,8 @@ int main(int argc, char *argv[])
140
150
141
151
if (total != N_READERS )
142
152
fprintf (stderr ,
143
- "The Sum of records in the array of grace period(s) (%u) is "
144
- "not the same with number of reader(s) (%u)\n" ,
153
+ "The sum of records in the array of grace period(s) (%u)\n "
154
+ "is not the same with number of reader(s) (%u)\n" ,
145
155
total , N_READERS );
146
156
147
157
return 0 ;
0 commit comments