Mercurial > hg > Game > Cerium
annotate TaskManager/kernel/ppe/QueueInfo.h @ 1546:61164c687b29 draft
fix GpuScheduler flip
author | Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 05 Feb 2013 13:15:46 +0900 |
parents | c0dc96c6f209 |
children | 99f8130793b4 b49aaf88eae6 |
rev | line source |
---|---|
806 | 1 #ifndef INCLUDED_QUEUE_INFO |
2 #define INCLUDED_QUEUE_INFO | |
3 | |
4 #include "base.h" | |
821 | 5 #include "types.h" |
806 | 6 |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
7 #if 0 |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
8 template <typename T> class Queue : T { |
806 | 9 public: |
10 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
11 T(); |
806 | 12 |
13 T *waiter; | |
14 T *next; | |
15 T *prev; | |
16 | |
958 | 17 void initOnce(); // to initialize object in pool |
18 void freeOnce(); // to destroy object in pool | |
19 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
20 // virual void init(); |
806 | 21 }; |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
22 #endif |
806 | 23 |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
24 template <typename T> class QueueInfo : public T { |
806 | 25 |
26 public: | |
27 /* constructor */ | |
958 | 28 |
29 /** | |
962 | 30 singleton queuePool constructor |
31 Do not use this as a Queue | |
958 | 32 */ |
821 | 33 QueueInfo<T>(){ |
34 queueInfoInit(); | |
35 } | |
958 | 36 /** |
37 normal constructor requires | |
38 */ | |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
39 QueueInfo<T>(QueueInfo<T> *p) { |
821 | 40 queueInfoInit(); |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
41 queuePool = p; |
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
42 } |
806 | 43 |
44 BASE_NEW_DELETE(QueueInfo); | |
45 | |
46 /* functions */ | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
47 T *create(); |
806 | 48 |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
49 void free_(T *queue); |
806 | 50 |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
51 void addFirst(T* e); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
52 void addLast(T* e); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
53 T* getFirst(); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
54 T* getLast(); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
55 int remove(T* e); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
56 T* poll(); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
57 void moveToFirst(T* e); // or use(); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
58 T* get(int index); |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
59 T* find(T *task); |
806 | 60 int empty(); |
61 void freePool() ; | |
955 | 62 void freeAll(); |
806 | 63 |
64 // Iterator | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
65 T* getNext(T* q) ; |
806 | 66 int length(); |
67 | |
68 private: | |
69 /* variables */ | |
70 | |
958 | 71 /* we cannot use static in template */ |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
72 /* static */ QueueInfo<T> *queuePool; |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
73 T* first; |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
74 T* last; |
806 | 75 |
76 /* functions */ | |
77 int extend_pool(int num); | |
78 void destroy(); | |
821 | 79 void queueInfoInit(); |
899 | 80 } ; |
806 | 81 |
82 | |
83 | |
84 #ifdef CHECK | |
85 #include <stdio.h> | |
86 #endif | |
87 #include <stdlib.h> | |
88 | |
89 | |
958 | 90 /** |
962 | 91 Use singleton queuePool constructor |
958 | 92 all queueInfo should share this as a pool. |
93 | |
94 exteren QueueInfo<H> pool; | |
95 QueueInfo<H> pool = new QueueInfo<H>(); | |
96 | |
97 use this in non initialize envrionment is wrong. | |
98 */ | |
806 | 99 |
821 | 100 template<typename T>void QueueInfo<T>::queueInfoInit() { |
806 | 101 // 最初の一つは自分 |
102 first = last = this; | |
103 this->next = this->prev = this; | |
104 this->waiter = NULL; | |
105 } | |
106 | |
107 template<typename T>void | |
108 QueueInfo<T>::freePool() { | |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
109 for(T * p = queuePool->waiter; p; ) { |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
110 T * next = p->waiter; |
806 | 111 p->waiter = NULL; |
958 | 112 p->freeOnce(); |
806 | 113 free(p); |
114 p = next; | |
115 } | |
116 } | |
117 | |
958 | 118 /** |
119 all pools are shared among QueueInfo (separated by size and type). | |
120 it automatically extended by 64. | |
121 */ | |
806 | 122 template<typename T>int |
123 QueueInfo<T>::extend_pool(int num) | |
124 { | |
962 | 125 #ifdef CHECK |
126 if (queuePool) fprintf(stderr, "don't use queuePool directly"); | |
127 #endif | |
128 | |
821 | 129 T* q = (T*)malloc(sizeof(T)*(num+1)+DEFAULT_ALIGNMENT); |
806 | 130 |
131 // First Queue is previous pool | |
1089 | 132 q->waiter = this->waiter; |
133 this->waiter = q; | |
821 | 134 q = (T*)ROUND_UP_ALIGN((long)q, DEFAULT_ALIGNMENT); |
806 | 135 q++; |
136 | |
137 /* Connect all free queue in the pool */ | |
821 | 138 T* p = q; |
139 for (; num-- > 0;) { | |
140 p->waiter = NULL; | |
956
197b7e19a345
unified queue worked on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
955
diff
changeset
|
141 p->initOnce(); |
962 | 142 addLast(p); |
821 | 143 p = (T*)ROUND_UP_ALIGN((long)(p+1),DEFAULT_ALIGNMENT); |
806 | 144 } |
145 | |
146 return 0; | |
821 | 147 |
806 | 148 } |
149 | |
150 /** | |
151 * Task をプールから取って来て返す | |
152 * | |
153 * @param [cmd] タスクコマンド | |
154 */ | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
155 template<typename T>T * |
806 | 156 QueueInfo<T>::create() |
157 { | |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
158 T * q = queuePool->poll(); |
806 | 159 if (! q) { |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
160 queuePool->extend_pool(64); |
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
161 q = queuePool->poll(); |
806 | 162 } |
163 q->init(); | |
164 return q; | |
165 } | |
166 | |
167 | |
168 template<typename T>void | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
169 QueueInfo<T>::free_(T * q) |
806 | 170 { |
171 q->waiter = NULL; | |
820
3c508c837ad8
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
172 queuePool->addLast(q); |
806 | 173 } |
174 | |
175 | |
176 /*! | |
177 QueueInfo<T> は空にならない。最低1個は要素が入っていて | |
178 1個目は特別扱いする。getFirst すると first->next を返す | |
179 */ | |
180 | |
181 /*! | |
182 最初の1個は特別扱いなので、それの後に追加していく | |
183 */ | |
184 template<typename T>void | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
185 QueueInfo<T>::addFirst(T* e) |
806 | 186 { |
187 e->prev = first; | |
188 e->next = first->next; | |
189 first->next->prev = e; | |
190 first->next = e; | |
191 } | |
192 | |
193 template<typename T>void | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
194 QueueInfo<T>::addLast(T* e) |
806 | 195 { |
196 #ifdef CHECK | |
197 if (find(e)) { | |
198 fprintf(stderr,"Add duplicate task %0x\n",(int)e); | |
199 return; | |
200 // ... | |
201 } | |
202 #endif | |
203 e->next = first; | |
204 e->prev = last; | |
205 last->next = e; | |
206 last = e; | |
207 } | |
208 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
209 template<typename T>T* |
806 | 210 QueueInfo<T>::getFirst() |
211 { | |
212 if (empty()) return NULL; | |
213 return first->next; | |
214 } | |
215 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
216 template<typename T>T* |
806 | 217 QueueInfo<T>::getLast() |
218 { | |
219 if (empty()) return NULL; | |
220 return last; | |
221 } | |
222 | |
223 template<typename T>int | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
224 QueueInfo<T>::remove(T* e) |
806 | 225 { |
226 #ifdef CHECK | |
227 if (!find(e)) { | |
228 fprintf(stderr,"Remove non existing task %0x\n",(int)e); | |
229 return 0; | |
230 // ... | |
231 } | |
232 #endif | |
233 e->prev->next = e->next; | |
234 e->next->prev = e->prev; | |
235 | |
236 if (first->next == e) { | |
237 first->next = e->next; | |
238 } | |
239 if (last == e) { | |
240 last = e->prev; | |
241 } | |
242 | |
243 e->prev = NULL; | |
244 e->next = NULL; | |
245 | |
246 return 1; | |
247 } | |
248 | |
249 /*! | |
250 リストの先頭を取得および削除する。リストが空の場合は NULL を返す。 | |
251 */ | |
252 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
253 template<typename T>T* |
806 | 254 QueueInfo<T>::poll() |
255 { | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
256 T* e = first->next; |
806 | 257 if (e == this) { |
258 return NULL; | |
259 } | |
260 remove(e); | |
261 return e; | |
262 } | |
263 | |
264 template<typename T>void | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
265 QueueInfo<T>::moveToFirst(T* e) |
806 | 266 { |
267 remove(e); | |
268 addFirst(e); | |
269 } | |
270 | |
271 /*! | |
272 リスト内の指定された位置にある要素を返す。 | |
273 要素数を超えた位置を指定した場合 NULL を返す。 | |
274 */ | |
275 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
276 template<typename T>T* |
806 | 277 QueueInfo<T>::get(int index) |
278 { | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
279 T* e = first->next; |
806 | 280 for (int i = 0; i < index; i++) { |
822 | 281 if (e->next == this) return NULL; |
806 | 282 e = e->next; |
283 } | |
284 return e; | |
285 } | |
286 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
287 template<typename T>T* |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
288 QueueInfo<T>::find(T* task) |
806 | 289 { |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
290 T* e = first->next; |
806 | 291 for(;;) { |
292 if (e == this) return NULL; | |
293 if (e == task) break; | |
294 e = e->next; | |
295 } | |
296 return e; | |
297 } | |
298 | |
299 template<typename T>int | |
300 QueueInfo<T>::empty() | |
301 { | |
302 return this->next == this; | |
303 } | |
304 | |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
305 template<typename T>T* |
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
306 QueueInfo<T>::getNext(T* q) |
806 | 307 { |
308 if (q->next==this) return NULL; | |
309 return q->next; | |
310 } | |
311 | |
312 template<typename T>int | |
313 QueueInfo<T>::length() | |
314 { | |
822 | 315 int i = 0; |
806 | 316 if (empty()) return 0; |
818
5d48fa762a24
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
806
diff
changeset
|
317 T* e = first; |
806 | 318 while((e = e->next) != this ) i++; |
319 return i; | |
320 } | |
321 | |
955 | 322 template<typename T>void |
323 QueueInfo<T>::freeAll() | |
324 { | |
325 T* t; | |
326 while((t=poll())) free_(t); | |
327 } | |
806 | 328 |
329 | |
330 | |
331 /* end */ | |
332 | |
333 | |
334 | |
335 #endif |