-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
299 lines (260 loc) · 7.73 KB
/
main.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// Created by eliasital
//
/**
* boolean print (global var) :
* prints to screen if true, otherwise runs, but without any prints.
*
* printCount (function local variable) :
* Functions runs and writes 'in f(0)', 'in f(1)' and so on.
* The number in the brackets is the printCount var.
* A condition of 'if (printCount == x) then ...'
* states what will the function do when it will reach its 'x' cycle.
*
* Tests :
* 1 Spawn functions
* 2 Non-Main function terminates it self
* 3 Non-Main function blocks Non-Main function
* 4 Non-Main function goes to sleep
* 5 Main terminates functions
* 6 Main blocks function
* 7 Main unblocks function
* 8 Main removes blocked function
* 9 Main terminates with functions that are alive
* 10 Sanity check - Main blocks sleeping function (OK!)
* Sanity check - Main unblocks sleeping function (ERR!)
* Sanity check - Main blocks non-existent function (ERR!)
* Sanity check - Main asks to sleep (ERR!)
* Sanity check - Main asks to block himself (ERR!)
* Sanity check - Main asks to unblock sleeping function (ERR!)
*/
#include <array>
#include <iostream>
#include "uthreads.h"
#define QUANT 1000000
#define PRINTER 100000000
bool print = true;
using namespace std;
/**
* A Simple function - runs and terminates itself in the end.
*/
void f()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in f(" << printCount << ")" << endl;}
if (printCount == 7)
{
break;
}
printCount++;
}
}
if (print) {cout << "f terminates" << endl;}
int me = uthread_get_tid();
uthread_terminate(me);
}
/**
* A Simple function - runs and terminates itself in the end.
*/
void g()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in g(" << printCount << ")" << endl;}
if (printCount == 13)
{
break;
}
printCount++;
}
}
if (print) {cout << "g terminates" << endl;}
int me = uthread_get_tid();
uthread_terminate(me);
}
/**
* A Simple function - runs forever, until terminated.
*/
void neverEnding_h()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in h(" << printCount << ")" << endl;}
printCount++;
}
}
}
/**
* A Simple function - runs forever, until terminated.
*/
void neverEnding_i()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in i(" << printCount << ")" << endl;}
printCount++;
}
}
}
/**
* A Simple function - runs forever, until terminated.
*/
void neverEnding_j()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in j(" << printCount << ")" << endl;}
printCount++;
}
}
}
/**
* A simple function that sends it self to sleep.
*/
void sleeper()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in sleeper(" << printCount << ")" << endl;}
if (printCount == 15)
{
if (print) {cout << "sleeper went to sleep for 50 quants!" << endl;}
uthread_sleep(50);
if (print) {cout << "sleeper is awake again!" << endl;}
}
if (printCount == 57)
{
break;
}
printCount++;
}
}
if (print) {cout << "sleeper terminates" << endl;}
int me = uthread_get_tid();
uthread_terminate(me);
}
/**
* Blocks some thread and then unblocks its.
* Blocks it self.
*/
void blocker()
{
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "in blocker(" << printCount << ")" << endl;}
if (printCount == 3)
{
if (print) {cout << "blocker blocks f (tid = 1)!" << endl;}
uthread_block(1);
}
if (printCount == 11)
{
if (print) {cout << "blocker unblocks f (tid = 1)!" << endl;}
uthread_resume(1);
}
if (printCount == 40)
{
if (print) {cout << "blocker blocks himself!" << endl;}
uthread_block(uthread_get_tid());
if (print) {cout << "blocker was resumed!" << endl;}
}
printCount++;
}
}
}
int main()
{
int init = uthread_init(QUANT);
if (print) {cout << "MAIN INIT : " << init << endl << endl;}
int fid = uthread_spawn(&f);
if (print) {cout << "MAIN SPAWNS f : " << fid << endl;}
int gid = uthread_spawn(&g);
if (print) {cout << "MAIN SPAWNS g : " << gid << endl;}
int hid = uthread_spawn(&neverEnding_h);
if (print) {cout << "MAIN SPAWNS h : " << hid << endl;}
int iid = uthread_spawn(&neverEnding_i);
if (print) {cout << "MAIN SPAWNS i : " << iid << endl;}
int jid = uthread_spawn(&neverEnding_j);
if (print) {cout << "MAIN SPAWNS j : " << jid << endl;}
int sleeperid = uthread_spawn(&sleeper);
if (print) {cout << "MAIN SPAWNS sleeper : " << sleeperid << endl;}
int blockerid = uthread_spawn(&blocker);
if (print) {cout << "MAIN SPAWNS blockerid : " << blockerid << endl;}
if (print) {cout << endl << "MAIN RUNS!" << endl;}
int printCount = 0;
for (int i = 0; ; i++)
{
if (i%PRINTER == 0)
{
if (print) {cout << "IN MAIN(" << printCount << ")" << endl;}
if (printCount == 20)
{
if (print) {cout << "GOOD-ERR Main blocks sleeping sleeper!" << endl;}
uthread_block(sleeperid);
if (print) {cout << "ERR Main unblocks sleeping sleeper!" << endl;}
uthread_resume(sleeperid);
if (print) {cout << "ERR Main blocks non-existent 99!" << endl;}
uthread_block(99);
if (print) {cout << "ERR Main unblocks non-existent 101!" << endl;}
uthread_block(101);
if (print) {cout << "ERR Main asks to sleep!" << endl;}
uthread_sleep(5);
if (print) {cout << "ERR Main asks to block himself!" << endl;}
uthread_block(uthread_get_tid());
}
if (printCount == 30)
{
if (print) {cout << "Main removes h,i!" << endl;}
uthread_terminate(hid);
uthread_terminate(iid);
if (print) {cout << "ERR Main unblocks sleeping sleeper!" << endl;}
uthread_resume(sleeperid);
}
if (printCount == 45)
{
if (print) {cout << "Main unblocks blocker (which blocked himself)!" << endl;}
uthread_resume(blockerid);
}
if (printCount == 52)
{
if (print) {cout << "Main blocks blocker!" << endl;}
uthread_block(blockerid);
}
if (printCount == 58)
{
if (print) {cout << "Main removes blocker!" << endl;}
uthread_terminate(blockerid);
}
if (printCount == 65)
{
break;
}
printCount++;
}
}
if (print) {cout << "MAIN TERMINATES WHILE j IS ALIVE" << endl;}
int me = uthread_get_tid();
uthread_terminate(me);
}