-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab7app.c
153 lines (113 loc) · 3.78 KB
/
lab7app.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
144
145
146
147
148
149
150
151
152
153
/*
File: lab7app.c
Revision date: 10 November 2005
Description: Application code for EE 425 lab 7 (Event flags)
*/
#include "clib.h"
#include "yakk.h" /* contains kernel definitions */
#include "lab7defs.h"
#define TASK_STACK_SIZE 512 /* stack size in words */
YKEVENT *charEvent;
YKEVENT *numEvent;
int CharTaskStk[TASK_STACK_SIZE]; /* a stack for each task */
int AllCharsTaskStk[TASK_STACK_SIZE];
int AllNumsTaskStk[TASK_STACK_SIZE];
int STaskStk[TASK_STACK_SIZE];
void CharTask(void) /* waits for any events triggered by letter keys */
{
unsigned events;
printString("Started CharTask (2)\n");
while(1) {
events = YKEventPend(charEvent,
EVENT_A_KEY | EVENT_B_KEY | EVENT_C_KEY,
EVENT_WAIT_ANY);
if(events == 0) {
printString("Oops! At least one event should be set "
"in return value!\n");
}
if(events & EVENT_A_KEY) {
printString("CharTask (A)\n");
YKEventReset(charEvent, EVENT_A_KEY);
}
if(events & EVENT_B_KEY) {
printString("CharTask (B)\n");
YKEventReset(charEvent, EVENT_B_KEY);
}
if(events & EVENT_C_KEY) {
printString("CharTask (C)\n");
YKEventReset(charEvent, EVENT_C_KEY);
}
}
}
void AllCharsTask(void) /* waits for all events triggered by letter keys */
{
unsigned events;
printString("Started AllCharsTask (3)\n");
while(1) {
events = YKEventPend(charEvent,
EVENT_A_KEY | EVENT_B_KEY | EVENT_C_KEY,
EVENT_WAIT_ALL);
// To be reset by WaitForAny task
if(events != 0) {
printString("Oops! Char events weren't reset by CharTask!\n");
}
printString("AllCharsTask (D)\n");
}
}
void AllNumsTask(void) /* waits for events triggered by number keys */
{
unsigned events;
printString("Started AllNumsTask (1)\n");
while(1) {
events = YKEventPend(numEvent,
EVENT_1_KEY | EVENT_2_KEY | EVENT_3_KEY,
EVENT_WAIT_ALL);
if(events != (EVENT_1_KEY | EVENT_2_KEY | EVENT_3_KEY)) {
printString("Oops! All events should be set in return value!\n");
}
printString("AllNumsTask (123)\n");
YKEventReset(numEvent, EVENT_1_KEY | EVENT_2_KEY | EVENT_3_KEY);
}
}
void STask(void) /* tracks statistics */
{
unsigned max, switchCount, idleCount;
int tmp;
YKDelayTask(1);
printString("Welcome to the YAK kernel\r\n");
printString("Determining CPU capacity\r\n");
YKDelayTask(1);
YKIdleCount = 0;
YKDelayTask(5);
max = YKIdleCount / 25;
YKIdleCount = 0;
YKNewTask(CharTask, (void *) &CharTaskStk[TASK_STACK_SIZE], 2);
YKNewTask(AllNumsTask, (void *) &AllNumsTaskStk[TASK_STACK_SIZE], 1);
YKNewTask(AllCharsTask, (void *) &AllCharsTaskStk[TASK_STACK_SIZE], 3);
while (1)
{
YKDelayTask(20);
YKEnterMutex();
switchCount = YKCtxSwCount;
idleCount = YKIdleCount;
YKExitMutex();
printString("<<<<< Context switches: ");
printInt((int)switchCount);
printString(", CPU usage: ");
tmp = (int) (idleCount/max);
printInt(100-tmp);
printString("% >>>>>\r\n");
YKEnterMutex();
YKCtxSwCount = 0;
YKIdleCount = 0;
YKExitMutex();
}
}
void main(void)
{
YKInitialize();
charEvent = YKEventCreate(0);
numEvent = YKEventCreate(0);
YKNewTask(STask, (void *) &STaskStk[TASK_STACK_SIZE], 0);
YKRun();
}