-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
106 lines (83 loc) · 1.98 KB
/
main.c
File metadata and controls
106 lines (83 loc) · 1.98 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
#include <stdio.h>
#include <assert.h>
#include "threadlib.h"
//#define TESTING 1
/* once you undestand the code
* undefine this (just comment)
*/
#undef TESTING
#ifdef TESTING
/* few threads to play around with
*/
void thread1(void);
void thread2(void);
void thread1(void)
{
int i = 1;
for(;i<10;) {
printf("%s: count = %d\n",__func__,i++);
yield();
}
delete_thread();
assert(0);
}
void thread2(void)
{ // the greedy fellow.
int i = -10;
for(;i<0;) {
printf("%s: count = %d\n",__func__,i++);
printf("%s: count = %d\n",__func__,i++);
printf("%s: count = %d\n",__func__,i++);
yield();
}
delete_thread();
assert(0);
}
int main(void)
{
printf("Creating thread 1\n");
assert(!create_thread(thread2));
printf("Creating thread 2\n");
assert(!create_thread(thread1));
stop_main(); /* give up the CPU */
assert(0); /* you should not come back */
}
#else /* real code
* this is what we'll test/mark
*/
int count = 1;
#define MAX 10 /* how may threads to create */
#define COUNT(x) x*100000
void simple_thread(void); /* want to create many instance of
* this.
*/
void simple_thread(void)
{
int i, myID, sleep;
myID = count;
count ++; /* for the next thread
* It is shared, do I need to lock???
* A lock is not necessary. Yielding is collaborative.
* So cant be pre-empted at the middle
*/
for(i=myID; i<MAX;i++) {
/* print something, sleep for a while and yield */
printf("I'm %s %d: Count=%d\n",__func__,myID,i);
for(sleep=-COUNT(myID); sleep < COUNT(myID); sleep++);
yield();
}
delete_thread(); /* die */
assert(0); /* I should not be running */
}
#define NO_THREADS 10 /* FixMe: this should work for any number
* of threads
*/
int main(void)
{
int i;
for(i=0; i < NO_THREADS; i++)
assert(!create_thread(simple_thread));
stop_main(); /* give up the CPU */
assert(0); /* you should not come back */
}
#endif