Repository navigation
Expand file tree
/
Copy pathtask.c
More file actions
92 lines (84 loc) · 2.68 KB
/
Copy pathtask.c
File metadata and controls
92 lines (84 loc) · 2.68 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
#include "../include/kernel.h"
#include <stdio.h>
static int shared_counter = 0;
void task1_high(void) {
tcb_t *t = current_task;
switch (t->app_state) {
case 0:
mutex_lock(&shared_mutex);
if (shared_mutex.owner != t) {
return;
}
printf("[T1-high] Tick %u - entered critical section\n", system_ticks);
t->app_sub_state = 0;
t->app_state = 1;
t->ready = false;
return;
case 1:
if (t->app_sub_state < 40) {
shared_counter++;
printf("[T1-high] work inside mutex %d - counter %d\n", t->app_sub_state, shared_counter);
t->app_sub_state++;
t->ready = false;
return;
} else {
printf("[T1-high] Tick %u - exiting critical section\n", system_ticks);
mutex_unlock(&shared_mutex);
t->app_state = 0;
t->ready = false;
}
break;
}
}
void task2_medium(void) {
tcb_t *t = current_task;
switch (t->app_state) {
case 0:
printf("[T2-med] Tick %u - starting long work...\n", system_ticks);
t->app_sub_state = 0;
t->app_state = 1;
t->ready = false;
return;
case 1:
if (t->app_sub_state < 8) {
printf("[T2-med] work step %d\n", t->app_sub_state);
t->app_sub_state++;
t->ready = false;
return;
} else {
printf("[T2-med] Tick %u - finished long work\n", system_ticks);
t->app_state = 0;
t->ready = false;
}
break;
}
}
void task3_low(void) {
tcb_t *t = current_task;
switch (t->app_state) {
case 0:
mutex_lock(&shared_mutex);
if (shared_mutex.owner != t) {
return;
}
shared_counter += 10;
printf("[T3-low] Tick %u - counter now %d\n", system_ticks, shared_counter);
t->app_sub_state = 0;
t->app_state = 1;
t->ready = false;
return;
case 1:
if (t->app_sub_state < 6) {
printf("[T3-low] holding work step %d\n", t->app_sub_state);
t->app_sub_state++;
t->ready = false;
return;
} else {
printf("[T3-low] Tick %u - exiting critical section\n", system_ticks);
mutex_unlock(&shared_mutex);
t->app_state = 0;
t->ready = false;
}
break;
}
}