-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
210 lines (149 loc) · 4.56 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// (C) 2021 by [email protected]
// released under GPL v3.0
#define __USE_GNU
#define _GNU_SOURCE
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#define TIME 10000000
pthread_mutex_t test = PTHREAD_MUTEX_INITIALIZER;
uint64_t get_us()
{
struct timespec tp = { 0, 0 };
if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
perror("clock_gettime");
return 0;
}
return tp.tv_sec * 1000l * 1000l + tp.tv_nsec / 1000;
}
void lock_unlock(pthread_mutex_t *const mutex)
{
uint64_t start = get_us();
do {
pthread_mutex_lock(mutex);
pthread_yield();
usleep((random() % 1500) + 5);
pthread_mutex_unlock(mutex);
pthread_yield();
usleep((random() % 1500) + 5);
} while (get_us() - start <= TIME);
}
void *thread(void *p)
{
pthread_mutex_t *mutex = (pthread_mutex_t *)p;
pthread_setname_np(pthread_self(), "test-cont");
lock_unlock(mutex);
return NULL;
}
void test_mutex()
{
pthread_mutex_t mutex, mutex2, mutex3 = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, mutex4 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutexattr_t attr, attr2;
pthread_setname_np(pthread_self(), "test-mutex");
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutexattr_init(&attr2);
pthread_mutexattr_settype(&attr2, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&mutex2, &attr2);
pthread_mutexattr_destroy(&attr2);
/* try to simulate contention */
pthread_t th;
pthread_create(&th, NULL, thread, &mutex);
lock_unlock(&mutex);
pthread_mutex_lock(&test); /* test lock */
pthread_mutex_unlock(&test); /* test double unlock */
pthread_mutex_unlock(&test); /* test double unlock */
pthread_mutex_lock(&mutex); /* test double lock */
/* test performance of lock + unlock */
uint64_t cnt = 0;
uint64_t start = get_us();
do {
pthread_mutex_lock(&mutex4);
cnt++;
pthread_mutex_unlock(&mutex4);
pthread_mutex_unlock(&mutex4);
pthread_mutex_unlock(&mutex4);
pthread_mutex_unlock(&mutex4);
} while (get_us() - start <= TIME);
printf("%f/s\n", cnt / (TIME / 1000000.0));
pthread_mutex_lock(&mutex); /* test double lock */
pthread_mutex_lock(&mutex);
pthread_mutex_trylock(&mutex); /* test trylock */
pthread_mutex_unlock(&mutex); /* test invalid unlock */
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&mutex); /* test "still locked" */
pthread_mutex_lock(&mutex2); /* test deadlock */
pthread_mutex_lock(&mutex2);
uint64_t dummy = 0;
for(int i=0; i<1024; i++) { /* test adaptive lock */
pthread_mutex_lock(&mutex3);
dummy += i * cnt;
pthread_mutex_unlock(&mutex3);
}
}
void test_rwlock()
{
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
pthread_setname_np(pthread_self(), "test-rwlock");
pthread_rwlock_rdlock(&rwlock); // regular
pthread_rwlock_unlock(&rwlock);
pthread_rwlock_wrlock(&rwlock); // regular
pthread_rwlock_unlock(&rwlock);
pthread_rwlock_wrlock(&rwlock); // double write-lock
pthread_rwlock_wrlock(&rwlock);
pthread_rwlock_unlock(&rwlock); // regular
pthread_rwlock_unlock(&rwlock); // double un-lock
pthread_rwlock_unlock(&rwlock);
pthread_rwlock_rdlock(&rwlock);
pthread_rwlock_rdlock(&rwlock); // double read-lock
pthread_rwlock_unlock(&rwlock);
pthread_rwlock_unlock(&rwlock); // double un-lock
pthread_rwlock_unlock(&rwlock);
}
void test_try_lock()
{
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
pthread_setname_np(pthread_self(), "test-try-lock");
pthread_rwlock_rdlock(&rwlock);
pthread_rwlock_tryrdlock(&rwlock);
pthread_rwlock_trywrlock(&rwlock);
struct timespec ts = { 0, 0 };
pthread_rwlock_timedrdlock(&rwlock, &ts);
pthread_rwlock_timedwrlock(&rwlock, &ts);
}
void * signal_c_func(void *arg)
{
pthread_cond_t *cond = (pthread_cond_t *)arg;
usleep(501000);
pthread_cond_signal(cond);
return NULL;
}
void test_conditional()
{
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_setname_np(pthread_self(), "test-conditional");
pthread_t signal_c;
pthread_create(&signal_c, NULL, signal_c_func, &cond);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
pthread_join(signal_c, NULL);
}
int main(int argc, char *argv[])
{
test_mutex();
test_rwlock();
test_try_lock();
test_conditional();
pthread_setname_np(pthread_self(), "main");
exit(0); // trigger dump in trace library
}