comparison TaskManager/kernel/ppe/TaskQueueInfo.cc @ 494:ec7b6d89b4e4

Singleton TaskQueue pool
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 10 Oct 2009 17:30:29 +0900
parents dd091fe6128e
children 17319af4ee39
comparison
equal deleted inserted replaced
493:dd091fe6128e 494:ec7b6d89b4e4
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include "TaskQueueInfo.h" 3 #include "TaskQueueInfo.h"
4 4
5 TaskQueueInfo::TaskQueueInfo() 5 // Singleton TaskQueue Pool
6 :taskQueuePool(NULL), freeTaskQueue(NULL) { 6 TaskQueueInfo TaskQueueInfo::taskQueuePool;
7 init(32);
8 }
9 7
10 TaskQueueInfo::~TaskQueueInfo(void) { destroy(); } 8 TaskQueueInfo::TaskQueueInfo() {
11
12 int
13 TaskQueueInfo::init(int num)
14 {
15 if (taskQueuePool == NULL) {
16 extend_pool(num);
17 }
18 // 最初の一つは自分 9 // 最初の一つは自分
19 first = last = this; 10 first = last = this;
20 next = prev = this; 11 next = prev = this;
21 waiter = NULL; 12 waiter = NULL;
22 return 0; 13 }
14
15 void
16 TaskQueueInfo::freePool() {
17 for(TaskQueuePtr p = taskQueuePool.waiter; p; ) {
18 TaskQueuePtr next = p->waiter;
19 p->waiter = NULL;
20 free(p);
21 p = next;
22 }
23 } 23 }
24 24
25 int 25 int
26 TaskQueueInfo::extend_pool(int num) 26 TaskQueueInfo::extend_pool(int num)
27 { 27 {
28 TaskQueuePtr q = NULL; 28 TaskQueuePtr q = (TaskQueuePtr)malloc(sizeof(TaskQueue)*(num+1));
29 29
30 q = (TaskQueuePtr)malloc(sizeof(TaskQueue)*(num+1)); 30 // First Queue is previous pool
31 31 q->waiter = waiter; waiter = q;
32 if (q == NULL) { 32 q++;
33 return -1; // throw...
34 }
35 q->next = taskQueuePool;
36 taskQueuePool = q;
37 33
38 /* Connect all free queue in the pool */ 34 /* Connect all free queue in the pool */
39 for (q = taskQueuePool + 1; --num > 0; q++) { 35 TaskQueuePtr p = q;
40 q->next = q + 1; 36 for (; --num > 0; p++) {
37 p->task = NULL;
38 p->waiter = NULL;
39 addLast(p);
41 } 40 }
42 q->next = freeTaskQueue;
43 freeTaskQueue = taskQueuePool + 1;
44 41
45 return 0; 42 return 0;
46 } 43 }
47 44
48 TaskQueuePtr 45 TaskQueuePtr
49 TaskQueueInfo::create(TaskPtr task) 46 TaskQueueInfo::create(TaskPtr task)
50 { 47 {
51 TaskQueuePtr q; 48 TaskQueuePtr q = taskQueuePool.poll();
52 49 if (! q) {
53 if (freeTaskQueue == NULL) { 50 taskQueuePool.extend_pool(64);
54 extend_pool(64); 51 q = taskQueuePool.poll();
55 } 52 }
56 q = freeTaskQueue;
57 freeTaskQueue = freeTaskQueue->next;
58
59 q->task = task;
60 q->next = q->prev = NULL; 53 q->next = q->prev = NULL;
61 q->waiter = NULL; 54 q->waiter = NULL;
55 q->task = task;
62 56
63 return q; 57 return q;
64 } 58 }
65 59
66 60
67 void 61 void
68 TaskQueueInfo::free_(TaskQueuePtr q) 62 TaskQueueInfo::free_(TaskQueuePtr q)
69 { 63 {
70 // if (!q) return; 64 q->waiter = NULL;
71 q->next = freeTaskQueue; 65 q->task = NULL;
72 q->prev = NULL; 66 taskQueuePool.addLast(q);
73 freeTaskQueue = q;
74 }
75
76
77 void
78 TaskQueueInfo::destroy(void)
79 {
80 TaskQueuePtr q, tmp;
81
82 #if 1
83 q = taskQueuePool;
84 while (q) {
85 tmp = q->next;
86 free(q);
87 q = tmp;
88 }
89 #else
90 for (q = taskQueuePool; q; q = q->next) {
91 free(q);
92 }
93 #endif
94 freeTaskQueue = taskQueuePool = NULL;
95
96
97 } 67 }
98 68
99 69
100 /*! 70 /*!
101 TaskQueueInfo は空にならない。最低1個は要素が入っていて 71 TaskQueueInfo は空にならない。最低1個は要素が入っていて
138 } 108 }
139 109
140 int 110 int
141 TaskQueueInfo::remove(TaskQueue* e) 111 TaskQueueInfo::remove(TaskQueue* e)
142 { 112 {
143 // if (!e) return 0;
144
145 e->prev->next = e->next; 113 e->prev->next = e->next;
146 e->next->prev = e->prev; 114 e->next->prev = e->prev;
147 115
148 if (first->next == e) { 116 if (first->next == e) {
149 first->next = e->next; 117 first->next = e->next;