-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterproc_com.c
264 lines (173 loc) · 4.85 KB
/
interproc_com.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
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
//
// interproc_com.c
// mOS
//
// Created by Markus Axelsson on 2015-02-13.
// Copyright (c) 2015 Markus & Oskar. All rights reserved.
//
#include "interproc_com.h"
#include "main.h"
#include "kernel.h"
#include "list_admin.h"
//mailbox struct with FIFO-implementation
exception push_mailbox(mailbox *mBox, msg *message){
if(mBox == NULL || message == NULL){
return FAIL;
}
message->pPrevious = mBox->pHead;
message->pNext = mBox->pHead->pNext;
mBox->pHead->pNext->pPrevious = message;
mBox->pHead->pNext = message;
return OK;
}
msg* pop_mailbox(mailbox *mBox){
msg* target_message = mBox->pTail->pPrevious;
target_message->pPrevious->pNext = target_message->pNext;
mBox->pTail->pPrevious = target_message->pPrevious;
target_message->pNext = NULL;
target_message->pPrevious = NULL;
return target_message;
}
void remove_message(msg *new_message){
new_message->pPrevious->pNext = new_message->pNext;
new_message->pNext->pPrevious = new_message->pPrevious;
new_message->pNext = new_message->pPrevious = NULL;
free(new_message);
}
mailbox* create_mailbox( uint nMessages, uint nDataSize ){
mailbox *newMb = malloc(sizeof(mailbox));
newMb->pHead = malloc(sizeof(msg));
newMb->pTail = malloc(sizeof(msg));
if(newMb == NULL || newMb->pHead == NULL || newMb->pTail == NULL){
free(newMb->pHead);
free(newMb->pTail);
free(newMb);
return NULL;
}
else{
newMb->nMessages = nMessages;
newMb->nDataSize = nDataSize;
newMb->nMessages = 0;
newMb->nBlockedMsg = 0;
newMb->pHead->pNext = newMb->pTail;
newMb->pTail->pNext = newMb->pHead;
newMb->pTail->pPrevious = newMb->pHead;
return newMb;
}
}
exception remove_mailbox( mailbox * mBox){
if (mBox->nMessages == 0) {
free(mBox->pHead);
free(mBox->pTail);
free(mBox);
return OK;
}
else{
return NOT_EMPTY;
}
}
exception send_wait(mailbox* mBox, void* pData){
volatile int firstrun = TRUE;
isr_off();
SaveContext();
if (firstrun){
firstrun = FALSE;
if (mBox->nBlockedMsg < 0 && mBox->pHead->pNext != mBox->pTail) {
msg* reciever = pop_mailbox(mBox);
memcpy(reciever->pData, pData, mBox->nDataSize);
extract_waitinglist(reciever->pBlock);
insert_waiting_ready_list(g_readylist,reciever->pBlock);
mBox->nBlockedMsg++;
}
else{
msg *message = malloc(sizeof(msg));
if(message == NULL){
free(message);
return FAIL;
}
message->pData = malloc(mBox->nDataSize);
if(message->pData == NULL){
return FAIL;
}
memcpy(message->pData, pData, mBox->nDataSize);
exception push_status = push_mailbox(mBox, message);
if(push_status == FAIL){
return push_status;
}
listobj *sending_task;
sending_task = extract_readylist();
insert_waiting_ready_list(g_waitinglist, sending_task);
message->pBlock = sending_task;
sending_task->pMessage = message;
mBox->nBlockedMsg++;
}
LoadContext();
}
else{
if(g_waitinglist->pHead->pNext->pTask->DeadLine <= TC){
isr_off();
g_readylist->pHead->pNext->pMessage->pNext->pPrevious = g_readylist->pHead->pNext->pMessage->pPrevious;
g_readylist->pHead->pNext->pMessage->pPrevious->pNext = g_readylist->pHead->pNext->pMessage->pNext;
free(g_readylist->pHead->pNext->pMessage->pData);
free(g_readylist->pHead->pNext->pMessage);
mBox->nMessages--;
mBox->nBlockedMsg--;
isr_on();
return DEADLINE_REACHED;
}
else{
return OK;
}
}
return OK;
}
exception recieve_wait(mailbox *mBox, void *data){
volatile int firstrun = TRUE;
isr_off();
SaveContext();
if(firstrun){
firstrun = FALSE;
if(mBox->nBlockedMsg > 0 && mBox->pHead->pNext != mBox->pTail){
memcpy(mBox->pHead->pNext->pData, data, mBox->nDataSize);
remove_message(mBox->pHead->pNext);
mBox->nBlockedMsg--;
mBox->nMessages--;
if(mBox->nBlockedMsg < 0){
listobj *sending_task;
sending_task = mBox->pHead->pNext->pBlock;
extract_waitinglist(sending_task);
insert_waiting_ready_list(g_readylist, sending_task);
mBox->nBlockedMsg--;
mBox->nMessages++;
}
else{
free(mBox->pHead->pNext->pData);
}
}
else{
listobj *recieving_task;
msg *message = malloc(sizeof(msg));
mBox->pHead->pNext = message;
recieving_task = extract_readylist();
insert_waiting_ready_list(g_waitinglist, recieving_task);
}
LoadContext();
}
else{
if(g_waitinglist->pHead->pNext->pTask->DeadLine <= TC){
isr_off();
g_readylist->pHead->pNext->pMessage->pNext->pPrevious = g_readylist->pHead->pNext->pMessage->pPrevious;
g_readylist->pHead->pNext->pMessage->pPrevious->pNext = g_readylist->pHead->pNext->pMessage->pNext;
free(g_readylist->pHead->pNext->pMessage->pData);
free(g_readylist->pHead->pNext->pMessage);
mBox->nMessages--;
mBox->nBlockedMsg--;
isr_on();
return DEADLINE_REACHED;
}
else{
return OK;
}
}
return OK;
}