-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt.c
More file actions
259 lines (195 loc) · 6.9 KB
/
mt.c
File metadata and controls
259 lines (195 loc) · 6.9 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* mutextest.c
*
* Created on: Apr 26, 2015
* Author: hodai
*/
#include "types.h"
#include "stat.h"
#include "user.h"
#define STACK_SIZE 1000
/* globals */
int mutex1;
int mutex2;
int resource1[20];
int resource2;
/* utils */
//#define STACK_PLUS_AT_KERNEL //define at makefile
#ifdef STACK_PLUS_AT_KERNEL
#define STACK(start, size) (stack)
#else
#define STACK(start, size) (stack + size)
#endif
#define ASSERT(cond, ...) \
if(cond){ \
printf(2, "FAIL at %s:%d - ", __FUNCTION__, __LINE__); \
printf(2, __VA_ARGS__); \
printf(2, "\n"); \
exit(); \
}
void* safeThread(){
int i;
/* part one use mutual array of resources */
ASSERT((kthread_mutex_lock(mutex1) == -1), "kthread_mutex_lock(%d) fail", mutex1);
resource1[0] = kthread_id();
for(i = 1 ;i < 20; i++){
sleep(i % 2); // make some more troubles
resource1[i] = resource1[i-1];
}
sleep(kthread_id() % 2); // make some more troubles
ASSERT((resource1[i-1] != kthread_id()), "(resource1[%d] != kthread_id:%d) fail", i, kthread_id());
ASSERT((kthread_mutex_unlock(mutex1) == -1), "kthread_mutex_unlock(%d) fail", mutex1);
/* part two - mutual calculation */
ASSERT((kthread_mutex_lock(mutex2) == -1), "kthread_mutex_lock(%d) fail", mutex2);
sleep(kthread_id() % 2); // make some more troubles
resource2 = resource2 + kthread_id();
ASSERT((kthread_mutex_unlock(mutex2) == -1), "kthread_mutex_unlock(%d) fail", mutex2);
kthread_exit();
return 0;
}
void* unsafeThread(){
int i;
resource1[0] = kthread_id();
for(i = 1 ;i < 20; i++){
sleep(i % 2); // make some more troubles
resource1[i] = resource1[i-1];
}
sleep(kthread_id()); // make some more troubles
//ASSERT((resource1[i-1] != kthread_id()), "(resource1[%d] != kthread_id()) fail", i);
resource2 = resource2 + resource1[i-1];
kthread_exit();
return 0;
}
void* loopThread(){
for(;;){};
return 0;
}
void stressTest1(int count){
int tid[count];
int i,ans;
int c=0;
char* stack;
printf(1, "starting %s test\n", __FUNCTION__);
for (i = 0 ; i < 20; i++)
resource1[i] = 0;
resource2 = 0;
mutex1 = kthread_mutex_alloc();
mutex2 = kthread_mutex_alloc();
ASSERT((mutex1 == mutex2), "(mutex1 == mutex2)");
for (i = 0 ; i < count; i++){
stack = malloc(STACK_SIZE);
tid[i] = kthread_create((void*) safeThread, stack, STACK_SIZE);
ASSERT((tid[i] <= 0), "kthread_create return with: %d, for index:%d", tid[i], i);
c += tid[i];
sleep(i % 2); // make some more troubles
}
for (i = 0 ; i < count; i++){
ans = kthread_join(tid[i]);
// if fail here it's not always error!
ASSERT((ans != 0), "kthread_join(%d) return with: %d", tid[i], ans)
}
// free the mutexes
ASSERT( (kthread_mutex_dealloc(mutex1) != 0), "dealloc");
ASSERT( (kthread_mutex_dealloc(mutex2) != 0), "dealloc");
ASSERT((c != resource2), "(c != resource2) : (%d != %d)" , c, resource2);
printf(1, "%s test PASS!\n", __FUNCTION__);
}
/* this test should fail most of the time because synchronization error */
void stressTest2Fail(int count){
int tid[count];
int i,ans;
int c=0;
char* stack;
printf(1, "starting %s test\n", __FUNCTION__);
for (i = 0 ; i < 20; i++)
resource1[i] = 0;
resource2 = 0;
for (i = 0 ; i < count; i++){
stack = malloc(STACK_SIZE);
tid[i] = kthread_create(&unsafeThread, stack, STACK_SIZE);
sleep(i %3); // make some more troubles
ASSERT((tid[i] <= 0), "kthread_create return with: %d, for index:%d", tid[i], i);
c += tid[i];
}
for (i = 0 ; i < count; i++){
ans = kthread_join(tid[i]);
// if fail here it's not always error!
ASSERT((ans != 0), "kthread_join(%d) return with: %d", tid[i], ans)
}
ASSERT((c == resource2), "(c == resource2) : (%d != %d), we expect to fail here!!" , c, resource2);
printf(1, "%s test PASS!\n", __FUNCTION__);
}
/* this test check that we can't create more then 16(count) threads */
void stressTest3toMuchTreads(int count){
int tid[count*2];
int i;
char* stack;
printf(1, "starting %s test\n", __FUNCTION__);
for (i = 0 ; i < count; i++){
stack = malloc(STACK_SIZE);
tid[i] = kthread_create(&loopThread, stack, STACK_SIZE);
ASSERT((tid[i] <= 0), "kthread_create return with: %d, for index:%d", tid[i], i);
if(kthread_create(&loopThread, stack, STACK_SIZE) <= 0)
{
printf(1, "%s test FAIL!\n", __FUNCTION__);
}
else
{
printf(1, "%s test PASS!\n", __FUNCTION__);
}
}
// the threads do not kill themself
exit();
}
void* trubleThread(){
ASSERT((kthread_mutex_lock(mutex1) == -1), "kthread_mutex_lock(%d) fail", mutex1);
resource2 = -10;
ASSERT((kthread_mutex_unlock(mutex1) == -1), "kthread_mutex_unlock(%d) fail", mutex1);
kthread_exit();
return 0;
}
void senaty(int count){
int i, j;
int mutex[64];
printf(1, "starting %s test\n", __FUNCTION__);
for(j=0 ; j<2 ; j++){ // run the test twice to check that mutexes can be reused
for(i=0 ; i < count ; i++){
mutex[i] = kthread_mutex_alloc();
ASSERT((mutex[i] == -1), "kthread_mutex_alloc fail, i=%d", i);
ASSERT((kthread_mutex_lock(mutex[i]) == -1), "kthread_mutex_lock(%d) fail", mutex[i]);
ASSERT((kthread_mutex_unlock(mutex[i]) == -1), "kthread_mutex_unlock(%d) fail", mutex[i]);
ASSERT((kthread_mutex_unlock(mutex[i]) != -1), "second kthread_mutex_unlock(%d) didn't fail as expected", mutex[i]);
}
for(i=0 ; i < count ; i++){
ASSERT((kthread_mutex_lock(mutex[i]) == -1), "kthread_mutex_lock(%d) fail", mutex[i]);
}
for(i=0 ; i < count ; i++){
ASSERT((kthread_mutex_unlock(mutex[i]) == -1), "kthread_mutex_unlock(%d) fail", mutex[i]);
ASSERT((kthread_mutex_unlock(mutex[i]) != -1), "second kthread_mutex_unlock(%d) didn't fail as expected", mutex[i]);
}
for(i=0 ; i < count ; i++){
ASSERT((kthread_mutex_dealloc(mutex[i]) == -1), "kthread_mutex_dealloc(%d) fail", mutex[i]);
ASSERT((kthread_mutex_dealloc(mutex[i]) != -1), "second kthread_mutex_dealloc(%d) didn't fail as expected", mutex[i]);
ASSERT((kthread_mutex_lock(mutex[i]) != -1), "kthread_mutex_lock(%d) didn't fail after dealloc", mutex[i]);
ASSERT((kthread_mutex_unlock(mutex[i]) != -1), "kthread_mutex_unlock(%d) didn't fail after dealloc", mutex[i]);
}
}
/* chack that mutexes are really limited by 64 */
for (i=0 ; i<64 ; i++){
mutex[i] = kthread_mutex_alloc();
ASSERT((mutex[i] == -1), "kthread_mutex_alloc (limit) fail, i=%d, expected fail at:%d", i, 64);
}
ASSERT((kthread_mutex_alloc() != -1), "limit test didn't fail as expected create %d mutexes instad of %d", i+1, 64);
// release all mutexes
for (i=0 ; i<64 ; i++){
ASSERT((kthread_mutex_dealloc(mutex[i]) == -1), "kthread_mutex_dealloc(%d) fail, i=%d", mutex[i], i);
}
printf(1, "%s test PASS!\n", __FUNCTION__);
}
int main(){
senaty(64);
stressTest1(15);
stressTest2Fail(15);
stressTest3toMuchTreads(15); //this test must be the last
exit();
}