-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
143 lines (126 loc) · 2.96 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "green.h"
int threads = 0;
int flag = 0;
pthread_cond_t condpthr;
green_cond_t cond;
green_mutex_t mutex;
void *demanding(long int loop) {
int i = 0;
int toggle = 1;
while (i < loop) {
toggle = (toggle + 1) % 2;
i++;
}
}
volatile int testme = 0;
void *test_locks(void *arg) {
int id = *(int*)arg;
printf("LOCKS %d START\n", id);
int loop = 50000;
while (loop > 0) {
green_mutex_lock(&mutex);
demanding(10000);
testme++;
green_mutex_unlock(&mutex);
loop--;
}
printf("LOCKS %d DONE\n", id);
}
void *test_all(void *arg) {
int id = *(int*)arg;
int loop = 500;
printf("MAIN TEST %d\n", id);
while (loop > 0) {
green_mutex_lock(&mutex);
while (flag != id) {
green_cond_waitl(&cond, &mutex);
}
flag = (id + 1) % 2;
green_mutex_unlock(&mutex);
green_cond_signal(&cond);
loop--;
}
printf("MAIN TEST %d DONE\n", id);
}
void *test_isr(void *arg) {
int id = *(int*)arg;
int loop = 50000;
printf("ISR %d\n", id);
while(loop > 0) {
if (id == 0) {
demanding(100000);
} else {
demanding(10000);
}
//printf("thread %d: %d\n", id, loop);
loop--;
}
printf("ISR %d DONE\n", id);
}
/* test conditional variables */
void *test_cond(void *arg) {
int id = *(int*)arg;
printf("CONDITIONAL %d\n", id);
int loop = 5000;
while (loop > 0) {
if (flag == id) {
loop--;
if (id + 1 == 2) {
flag = 0;
} else { //här sätts flaggan fel, alla trådar somnar
flag = id + 1;
}
green_cond_signal(&cond);
} else {
green_cond_wait(&cond);
}
}
printf("CONDITIONAL %d DONE\n", id);
}
/* yielding for each loop */
void *test_yield(void *arg) {
int id = *(int*)arg;
printf("YIELD %d\n", id);
int loop = 100000;
while(loop > 0) {
printf("thread %d: %d\n", id, loop);
loop--;
green_yield();
}
}
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("usage ./test <type>\n");
exit(EXIT_FAILURE);
}
int mode = atoi(argv[1]);
void *fun[5] = {
test_isr, test_cond, test_yield,
test_locks, test_all
};
int a0 = 0;
int a1 = 1;
int a2 = 2;
green_t g0, g1, g2;
if (mode == 1 || mode == 4) {
green_cond_init(&cond);
}
if (mode == 3 || mode == 4) {
green_mutex_init(&mutex);
}
green_create(&g0, fun[mode], &a0);
green_create(&g1, fun[mode], &a1);
green_create(&g2, fun[mode], &a2);
green_join(&g0, NULL);
green_join(&g1, NULL);
green_join(&g2, NULL);
if (mode == 3) {
printf("testme: %d\n", testme);
}
printf("done\n");
return 0;
}