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