@@ -8,6 +8,7 @@ struct thread_info {
8
8
} QEMU_ALIGNED (64 );
9
9
10
10
struct count {
11
+ QemuMutex lock ;
11
12
unsigned long val ;
12
13
} QEMU_ALIGNED (64 );
13
14
@@ -18,11 +19,13 @@ static unsigned int n_ready_threads;
18
19
static struct count * counts ;
19
20
static unsigned int duration = 1 ;
20
21
static unsigned int range = 1024 ;
22
+ static bool use_mutex ;
21
23
static bool test_start ;
22
24
static bool test_stop ;
23
25
24
26
static const char commands_string [] =
25
27
" -n = number of threads\n"
28
+ " -m = use mutexes instead of atomic increments\n"
26
29
" -d = duration in seconds\n"
27
30
" -r = range (will be rounded up to pow2)" ;
28
31
@@ -59,7 +62,13 @@ static void *thread_func(void *arg)
59
62
60
63
info -> r = xorshift64star (info -> r );
61
64
index = info -> r & (range - 1 );
62
- atomic_inc (& counts [index ].val );
65
+ if (use_mutex ) {
66
+ qemu_mutex_lock (& counts [index ].lock );
67
+ counts [index ].val += 1 ;
68
+ qemu_mutex_unlock (& counts [index ].lock );
69
+ } else {
70
+ atomic_inc (& counts [index ].val );
71
+ }
63
72
}
64
73
return NULL ;
65
74
}
@@ -91,6 +100,9 @@ static void create_threads(void)
91
100
th_info = g_new (struct thread_info , n_threads );
92
101
counts = qemu_memalign (64 , sizeof (* counts ) * range );
93
102
memset (counts , 0 , sizeof (* counts ) * range );
103
+ for (i = 0 ; i < range ; i ++ ) {
104
+ qemu_mutex_init (& counts [i ].lock );
105
+ }
94
106
95
107
for (i = 0 ; i < n_threads ; i ++ ) {
96
108
struct thread_info * info = & th_info [i ];
@@ -131,7 +143,7 @@ static void parse_args(int argc, char *argv[])
131
143
int c ;
132
144
133
145
for (;;) {
134
- c = getopt (argc , argv , "hd:n:r :" );
146
+ c = getopt (argc , argv , "hd:n:mr :" );
135
147
if (c < 0 ) {
136
148
break ;
137
149
}
@@ -145,6 +157,9 @@ static void parse_args(int argc, char *argv[])
145
157
case 'n' :
146
158
n_threads = atoi (optarg );
147
159
break ;
160
+ case 'm' :
161
+ use_mutex = true;
162
+ break ;
148
163
case 'r' :
149
164
range = pow2ceil (atoi (optarg ));
150
165
break ;
0 commit comments