-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
212 lines (163 loc) · 4.4 KB
/
queue.cpp
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
/*
* queue.c
*
* Created: 4/5/2013 9:36:36 AM
* Author: dcstanc
Copyright (C) 2013 Colin Tan
This file is part of ArdOS.
ArdOS is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ArdOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ArdOS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "queue.h"
#if OSUSE_QUEUES==1 || OSUSE_PRIOQUEUES==1
// Unblock a blocked process and call the scheduler
void OSQueueUnblock(TMsgQ *queue, unsigned char sreg)
{
_tasks[queue->blockproc].status &= ~_OS_BLOCKED;
// Enqueue blocked process
procEnq(queue->blockproc, _tasks, &_ready);
queue->blockproc=255;
OSExitAtomic(sreg);
// Make a priority swap
OSPrioSwap();
}
int OSDequeue(TMsgQ *queue)
{
unsigned char sreg;
int ret=255;
OSMakeAtomic(&sreg);
if(!queue->count)
{
// Block the current task
queue->blockproc=_running;
_tasks[_running].status|=_OS_BLOCKED;
OSExitAtomic(sreg);
// Make a task swap
OSSwap();
}
#if OSUSE_QUEUES==1
if(!queue->prioQ)
ret=queue->qbuf[queue->head];
#endif
#if OSUSE_PRIOQUEUES==1
if(queue->prioQ)
ret=queue->pqbuf[queue->head].data;
#endif
queue->head = (queue->head+1) % queue->len;
queue->count--;
OSExitAtomic(sreg);
return ret;
}
#endif
#if OSUSE_QUEUES==1
void OSCreateQueue(int *buffer, unsigned char length, TMsgQ *queue)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
queue->blockproc=255;
queue->count=0;
queue->head=0;
queue->tail=0;
queue->qbuf=buffer;
queue->len=length;
queue->prioQ=0;
OSExitAtomic(sreg);
}
void OSEnqueue(int data, TMsgQ *queue)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
if(queue->count>=queue->len)
{
OSExitAtomic(sreg);
return;
}
if(!queue->prioQ)
{
queue->qbuf[queue->tail]=data;
queue->tail = (queue->tail+1) % queue->len;
queue->count++;
}
// Check if any process is blocked. Unblock if there is
if(queue->blockproc!=255)
OSQueueUnblock(queue, sreg);
OSExitAtomic(sreg);
}
#endif
#if OSUSE_PRIOQUEUES==1
void OSCreatePrioQueue(TPrioNode *buffer, unsigned char length, TMsgQ *queue)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
queue->blockproc=255;
queue->count=0;
queue->head=0;
queue->len=length;
queue->pqbuf=buffer;
queue->prioQ=1;
queue->tail=0;
OSExitAtomic(sreg);
}
extern unsigned long _sleepTime[];
extern int _sleepFlag;
void OSPrioEnqueue(int data, unsigned char prio, TMsgQ *queue)
{
unsigned char i;
unsigned int iter=queue->head;
unsigned char flag=0, sreg;
OSMakeAtomic(&sreg);
if(queue->count >= queue->len)
{
OSExitAtomic(sreg);
return;
}
// Locate our insertion point
while(iter != queue->tail && !flag)
{
flag=(queue->pqbuf[iter].prio > prio);
if(!flag)
iter=(iter+1) % queue->len;
}
// If we have found our spot, shift the rest down and insert. Otherwise insert at the end
if(flag)
{
if(queue->tail > queue->head)
for(i=queue->tail-1; i>=iter && i != 255; i--)
queue->pqbuf[(i+1)%queue->len]=queue->pqbuf[i];
else
{
for(i=(queue->tail > 0 ? queue->tail-1 : queue->len-1); i!=iter; i=(i>0 ? i-1 : queue->len-1))
queue->pqbuf[(i+1)%queue->len]=queue->pqbuf[i];
// Last case
queue->pqbuf[(i+1)%queue->len]=queue->pqbuf[i];
}
}
else
iter=queue->tail;
queue->tail=(queue->tail+1)%queue->len;
queue->pqbuf[iter].data=data;
queue->pqbuf[iter].prio=prio;
queue->count++;
if(queue->blockproc!=255)
OSQueueUnblock(queue, sreg);
/* Block of code below is a work around that resolves the data corruption issue */
// Set sleep time
_sleepTime[_running]=0;
_sleepFlag |= (1<<_running);
// Set blocked flag
_tasks[_running].status|=_OS_BLOCKED;
/* Block of code above is a work around that resolves the data corruption issue */
// Note: No need to remove from READY queue because a _running process would have already been de-queued from there.
// So just call scheduler to swap.
OSExitAtomic(sreg);
OSSwap();
}
#endif