Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1658 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | ------------ |
||
21 | CVS : $Id: iqueue.c,v 1.1 2004-06-01 11:42:43 giacomo Exp $ |
||
22 | |||
23 | File: $File$ |
||
24 | Revision: $Revision: 1.1 $ |
||
25 | Last update: $Date: 2004-06-01 11:42:43 $ |
||
26 | ------------ |
||
27 | |||
28 | */ |
||
29 | |||
30 | /* |
||
31 | * Copyright (C) 2002 Paolo Gai |
||
32 | * |
||
33 | * This program is free software; you can redistribute it and/or modify |
||
34 | * it under the terms of the GNU General Public License as published by |
||
35 | * the Free Software Foundation; either version 2 of the License, or |
||
36 | * (at your option) any later version. |
||
37 | * |
||
38 | * This program is distributed in the hope that it will be useful, |
||
39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
41 | * GNU General Public License for more details. |
||
42 | * |
||
43 | * You should have received a copy of the GNU General Public License |
||
44 | * along with this program; if not, write to the Free Software |
||
45 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
46 | * |
||
47 | */ |
||
48 | |||
49 | #include "iqueue.h" |
||
50 | #include <kernel/mem.h> |
||
51 | |||
52 | void iq_init (IQUEUE *q, IQUEUE *share, int flags) |
||
53 | { |
||
54 | q->first = NIL; |
||
55 | q->last = NIL; |
||
56 | |||
57 | if (share) |
||
58 | q->s = share->s; |
||
59 | else { |
||
60 | q->s = (struct IQUEUE_shared *)kern_alloc(sizeof(struct IQUEUE_shared)); |
||
61 | |||
62 | if (!(flags & IQUEUE_NO_PRIORITY)) |
||
63 | q->s->priority = (DWORD *)kern_alloc(sizeof(DWORD) * MAX_PROC); |
||
64 | if (!(flags & IQUEUE_NO_TIMESPEC)) |
||
65 | q->s->timespec_priority = (struct timespec *) |
||
66 | kern_alloc(sizeof(struct timespec) * MAX_PROC); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /*+ |
||
71 | This function insert the task with PID i in the queue que. |
||
72 | The insertion is made respecting the priority field. |
||
73 | (the first item in the queue have the less priority) |
||
74 | +*/ |
||
75 | void iq_priority_insert (PID i, IQUEUE *que) |
||
76 | { |
||
77 | DWORD prio; |
||
78 | PID p,q; |
||
79 | |||
80 | p = NIL; |
||
81 | q = que->first; |
||
82 | prio = que->s->priority[i]; |
||
83 | |||
84 | while ((q != NIL) && (prio >= que->s->priority[q])) { |
||
85 | p = q; |
||
86 | q = que->s->next[q]; |
||
87 | } |
||
88 | |||
89 | if (p != NIL) |
||
90 | que->s->next[p] = i; |
||
91 | else |
||
92 | que->first = i; |
||
93 | |||
94 | if (q != NIL) |
||
95 | que->s->prev[q] = i; |
||
96 | else |
||
97 | que->last = i; |
||
98 | |||
99 | que->s->next[i] = q; |
||
100 | que->s->prev[i] = p; |
||
101 | } |
||
102 | |||
103 | |||
104 | /* |
||
105 | This function insert the task with PID i in the queue que. |
||
106 | The insertion is made respecting the timespec priority field. |
||
107 | (the first item in the queue have the less priority) |
||
108 | */ |
||
109 | void iq_timespec_insert(PID i, IQUEUE *que) |
||
110 | { |
||
111 | struct timespec prio; |
||
112 | PID p,q; |
||
113 | |||
114 | p = NIL; |
||
115 | q = que->first; |
||
116 | |||
117 | TIMESPEC_ASSIGN(&prio, &que->s->timespec_priority[i]); |
||
118 | |||
119 | while ((q != NIL) && |
||
120 | !TIMESPEC_A_LT_B(&prio, &que->s->timespec_priority[q])) { |
||
121 | p = q; |
||
122 | q = que->s->next[q]; |
||
123 | } |
||
124 | |||
125 | if (p != NIL) |
||
126 | que->s->next[p] = i; |
||
127 | else |
||
128 | que->first = i; |
||
129 | |||
130 | if (q != NIL) |
||
131 | que->s->prev[q] = i; |
||
132 | else |
||
133 | que->last = i; |
||
134 | |||
135 | que->s->next[i] = q; |
||
136 | que->s->prev[i] = p; |
||
137 | } |
||
138 | |||
139 | |||
140 | |||
141 | void iq_insertfirst(PID p, IQUEUE *q) |
||
142 | { |
||
143 | if (q->first != NIL) { |
||
144 | q->s->next[p] = q->first; |
||
145 | q->s->prev[q->first] = p; |
||
146 | } |
||
147 | else { |
||
148 | q->last = p; |
||
149 | q->s->next[p] = NIL; |
||
150 | } |
||
151 | q->s->prev[p] = NIL; |
||
152 | q->first = p; |
||
153 | } |
||
154 | |||
155 | |||
156 | void iq_insertlast(PID p, IQUEUE *q) |
||
157 | { |
||
158 | if (q->last != NIL) { |
||
159 | q->s->prev[p] = q->last; |
||
160 | q->s->next[q->last] = p; |
||
161 | } |
||
162 | else { |
||
163 | q->first = p; |
||
164 | q->s->prev[p] = NIL; |
||
165 | } |
||
166 | q->s->next[p] = NIL; |
||
167 | q->last = p; |
||
168 | } |
||
169 | |||
170 | |||
171 | void iq_extract(PID i, IQUEUE *que) |
||
172 | { |
||
173 | PID p,q; |
||
174 | |||
175 | p = que->s->prev[i]; |
||
176 | q = que->s->next[i]; |
||
177 | |||
178 | if (p != NIL) |
||
179 | que->s->next[p] = que->s->next[i]; |
||
180 | else |
||
181 | que->first = q; |
||
182 | |||
183 | if (q != NIL) |
||
184 | que->s->prev[q] = que->s->prev[i]; |
||
185 | else |
||
186 | que->last = p; |
||
187 | } |
||
188 | |||
189 | PID iq_getfirst(IQUEUE *q) |
||
190 | { |
||
191 | PID p = q->first; |
||
192 | |||
193 | if (p == NIL) |
||
194 | return NIL; |
||
195 | |||
196 | q->first = q->s->next[q->first]; |
||
197 | |||
198 | if (q->first != NIL) |
||
199 | q->s->prev[q->first] = NIL; |
||
200 | else |
||
201 | q->last = NIL; |
||
202 | |||
203 | return p; |
||
204 | } |
||
205 | |||
206 | PID iq_getlast(IQUEUE *q) |
||
207 | { |
||
208 | PID p = q->last; |
||
209 | |||
210 | if (p == NIL) |
||
211 | return NIL; |
||
212 | |||
213 | q->last = q->s->prev[q->last]; |
||
214 | |||
215 | if (q->last != NIL) |
||
216 | q->s->next[q->last] = NIL; |
||
217 | else |
||
218 | q->first = NIL; |
||
219 | |||
220 | return p; |
||
221 | } |