-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.cpp
134 lines (111 loc) · 2.58 KB
/
task.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
/*
* task.c
*
* Created: 12/4/2013 9:28:57 AM
* Author: dcstanc
*
* Task handling routines for the kernel. Provides task creation and queue management services.
*
*
*
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 "kernel.h"
// Priority queue routines
void prioEnq(int pid, tTCB *tasklist, tQueue *q)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
unsigned char i;
unsigned int iter=q->head;
unsigned char flag=0;
if(q->ctr >= q->len)
{
OSExitAtomic(sreg);
return;
}
while(iter != q->tail && !flag)
{
flag=(tasklist[q->qptr[iter]].prio > tasklist[pid].prio);
if(!flag)
iter=(iter+1) % q->len;
}
// If we have found our spot, shift the rest down and insert. Otherwise insert at the end
if(flag)
{
if(q->tail > q->head)
for(i=q->tail-1; i>=iter && i != 255; i--)
q->qptr[(i+1)%q->len]=q->qptr[i];
else
{
for(i=(q->tail > 0 ? q->tail-1 : q->len-1); i!=iter; i=(i>0 ? i-1 : q->len-1))
q->qptr[(i+1)%q->len]=q->qptr[i];
// Last case
q->qptr[(i+1)%q->len]=q->qptr[i];
}
}
else
iter=q->tail;
q->tail=(q->tail+1)%q->len;
q->qptr[iter]=pid;
q->ctr++;
OSExitAtomic(sreg);
}
void procEnq(int pid, tTCB *tasklist, tQueue *q)
{
prioEnq(pid, tasklist, q);
}
unsigned char procPeek(tQueue *q)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
if(!q->ctr)
{
OSExitAtomic(sreg);
return 255;
}
else
{
OSExitAtomic(sreg);
return q->qptr[q->head];
}
}
unsigned char procDeq(tQueue *q)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
unsigned char ret=255;
if(q->ctr>0)
{
ret=q->qptr[q->head];
q->head=(q->head+1)%q->len;
q->ctr--;
}
OSExitAtomic(sreg);
return ret;
}
void initQ(unsigned char *qbuf, unsigned char len, tQueue *q)
{
unsigned char sreg;
OSMakeAtomic(&sreg);
unsigned char i;
q->head=0;
q->tail=0;
q->qptr=qbuf;
q->len=len;
q->ctr=0;
for(i=0; i<len; i++)
q->qptr[i]=255;
OSExitAtomic(sreg);
}