46
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
47
|
3 #include "BufferManager.h"
|
46
|
4
|
|
5 BufferManager::BufferManager(int num)
|
|
6 :machineNum(num), activeTaskQueue(NULL), waitTaskQueue(NULL) {}
|
|
7
|
|
8 BufferManager::~BufferManager(void) { finish(); }
|
|
9
|
|
10 void
|
|
11 BufferManager::init(void)
|
|
12 {
|
|
13 tlistImpl = new TaskListInfo;
|
|
14 tqueueImpl = new TaskQueueInfo;
|
|
15 htaskImpl = new HTaskInfo;
|
|
16
|
|
17 tlistImpl->init(machineNum*2);
|
|
18 tqueueImpl->init(TASK_MAX_SIZE*4);
|
|
19 htaskImpl->init(TASK_MAX_SIZE*2);
|
|
20
|
|
21 machineTaskList = new TaskListPtr[machineNum];
|
|
22
|
|
23 for (int i = 0; i < machineNum; i++) {
|
|
24 machineTaskList[i] = tlistImpl->create();
|
|
25 }
|
|
26 }
|
|
27
|
|
28 TaskQueuePtr
|
|
29 BufferManager::create_taskQueue(HTaskPtr task)
|
|
30 {
|
|
31 return tqueueImpl->create(task);
|
|
32 }
|
|
33
|
|
34 HTaskPtr
|
54
|
35 BufferManager::create_task(int cmd, int siz, DmaBuffer *in, DmaBuffer *out)
|
46
|
36 {
|
54
|
37 return htaskImpl->create(cmd, siz, in, out);
|
46
|
38 }
|
|
39
|
|
40 void
|
|
41 BufferManager::free_taskQueue(TaskQueuePtr q)
|
|
42 {
|
|
43 tqueueImpl->free(q);
|
|
44 }
|
|
45
|
|
46 void
|
|
47 BufferManager::free_task(HTaskPtr task)
|
|
48 {
|
|
49 htaskImpl->free(task);
|
|
50 }
|
|
51
|
|
52 void
|
|
53 BufferManager::append_activeTask(HTaskPtr task)
|
|
54 {
|
|
55 TaskQueuePtr q;
|
|
56
|
|
57 q = tqueueImpl->create(task);
|
|
58 activeTaskQueue = tqueueImpl->append(activeTaskQueue, q);
|
|
59 }
|
|
60
|
|
61 void
|
|
62 BufferManager::finish(void)
|
|
63 {
|
|
64 delete tlistImpl;
|
|
65 delete tqueueImpl;
|
|
66 delete htaskImpl;
|
|
67 }
|
|
68
|
|
69 void
|
|
70 BufferManager::append_waitTask(HTaskPtr task)
|
|
71 {
|
|
72 TaskQueuePtr q;
|
|
73
|
|
74 q = tqueueImpl->create(task);
|
|
75 waitTaskQueue = tqueueImpl->append(waitTaskQueue, q);
|
|
76 }
|
|
77
|
|
78 void
|
|
79 BufferManager::check_task_finish(HTaskPtr task)
|
|
80 {
|
|
81 notify_wait_taskQueue(task, task->wait_me);
|
|
82 task->post_func();
|
|
83
|
|
84 htaskImpl->free(task);
|
|
85 }
|
|
86
|
|
87 void
|
|
88 BufferManager::notify_wait_taskQueue(HTaskPtr depend, TaskQueuePtr list)
|
|
89 {
|
|
90 TaskQueuePtr p, d;
|
|
91 HTaskPtr task;
|
|
92
|
|
93 p = list; // wait task list
|
|
94
|
|
95 while (p) {
|
|
96 task = p->task;
|
|
97 task->wait_i = remove_taskQueue_eq_task(task->wait_i, depend);
|
|
98 if (task->wait_i == NULL) {
|
|
99 d = p;
|
|
100 p = p->next;
|
|
101 append_activeTask(task);
|
|
102 waitTaskQueue = remove_taskQueue(waitTaskQueue, d);
|
|
103 } else {
|
|
104 p = p->next;
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108
|
|
109 TaskQueuePtr
|
|
110 BufferManager::remove_taskQueue_eq_task(TaskQueuePtr list, HTaskPtr task)
|
|
111 {
|
|
112 TaskQueuePtr p = list;
|
|
113 TaskQueuePtr p1;
|
|
114
|
|
115 if (!p) return p;
|
|
116
|
|
117 if (p->task == task) {
|
|
118 list = list->next;
|
|
119 tqueueImpl->free(p);
|
|
120 } else {
|
|
121 p1 = p->next;
|
|
122 while (p1 && p1->task && p1->task != task) {
|
|
123 p1 = p1->next;
|
|
124 p = p->next;
|
|
125 }
|
|
126 if (p1) {
|
|
127 p->next = p1->next;
|
|
128 tqueueImpl->free(p1);
|
|
129 }
|
|
130 }
|
|
131
|
|
132 return list;
|
|
133 }
|
|
134
|
|
135 TaskQueuePtr
|
|
136 BufferManager::remove_taskQueue(TaskQueuePtr list, TaskQueuePtr q)
|
|
137 {
|
|
138 TaskQueuePtr p = list;
|
|
139 TaskQueuePtr p1;
|
|
140
|
|
141 if (!p) return p;
|
|
142
|
|
143 if (p == q) {
|
|
144 list = list->next;
|
|
145 tqueueImpl->free(p);
|
|
146 } else {
|
|
147 p1 = p->next;
|
|
148 while (p1 && p1 != q) {
|
|
149 p1 = p1->next;
|
|
150 p = p->next;
|
|
151 }
|
|
152 if (p1) {
|
|
153 p->next = p1->next;
|
|
154 tqueueImpl->free(p1);
|
|
155 }
|
|
156 }
|
|
157
|
|
158 return list;
|
|
159 }
|
|
160
|
|
161 TaskListPtr
|
|
162 BufferManager::get_available_taskList(void)
|
|
163 {
|
|
164 TaskListPtr list, q;
|
|
165
|
|
166 list = machineTaskList[0];
|
|
167
|
|
168 while (list->next) list = list->next;
|
|
169
|
|
170 if (list->length < TASK_MAX_SIZE) {
|
|
171 return list;
|
|
172 } else {
|
|
173 q = tlistImpl->create();
|
|
174 machineTaskList[0] = tlistImpl->append(machineTaskList[0], q);
|
|
175 return q;
|
|
176 }
|
|
177 }
|
|
178
|
|
179 void
|
|
180 BufferManager::clear_taskList(void)
|
|
181 {
|
|
182 TaskListPtr p, p1;
|
|
183
|
|
184 machineTaskList[0]->length = 0;
|
|
185
|
|
186 p = machineTaskList[0]->next;
|
|
187 while (p) {
|
|
188 p1 = p;
|
|
189 p = p->next;
|
|
190 tlistImpl->free(p1);
|
|
191 }
|
|
192 }
|