Mercurial > hg > Game > Cerium
changeset 494:f1470e379c31 draft
Singleton TaskQueue pool
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 10 Oct 2009 17:30:29 +0900 |
parents | 2a7a9f80b8e7 |
children | f2816cf264ae |
files | TaskManager/Fifo/FifoTaskManagerImpl.cc TaskManager/kernel/ppe/TaskQueueInfo.cc TaskManager/kernel/ppe/TaskQueueInfo.h |
diffstat | 3 files changed, 34 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/TaskManager/Fifo/FifoTaskManagerImpl.cc Fri Oct 09 11:29:42 2009 +0900 +++ b/TaskManager/Fifo/FifoTaskManagerImpl.cc Sat Oct 10 17:30:29 2009 +0900 @@ -37,7 +37,6 @@ taskQueueImpl = new TaskQueueInfo; htaskImpl = new HTaskInfo; - taskQueueImpl->init(TASK_MAX_SIZE*4); htaskImpl->init(TASK_MAX_SIZE*2); mainTaskList = taskListImpl->create();
--- a/TaskManager/kernel/ppe/TaskQueueInfo.cc Fri Oct 09 11:29:42 2009 +0900 +++ b/TaskManager/kernel/ppe/TaskQueueInfo.cc Sat Oct 10 17:30:29 2009 +0900 @@ -2,45 +2,42 @@ #include <stdlib.h> #include "TaskQueueInfo.h" -TaskQueueInfo::TaskQueueInfo() - :taskQueuePool(NULL), freeTaskQueue(NULL) { - init(32); -} - -TaskQueueInfo::~TaskQueueInfo(void) { destroy(); } +// Singleton TaskQueue Pool +TaskQueueInfo TaskQueueInfo::taskQueuePool; -int -TaskQueueInfo::init(int num) -{ - if (taskQueuePool == NULL) { - extend_pool(num); - } +TaskQueueInfo::TaskQueueInfo() { // 最初の一つは自分 first = last = this; next = prev = this; waiter = NULL; - return 0; +} + +void +TaskQueueInfo::freePool() { + for(TaskQueuePtr p = taskQueuePool.waiter; p; ) { + TaskQueuePtr next = p->waiter; + p->waiter = NULL; + free(p); + p = next; + } } int TaskQueueInfo::extend_pool(int num) { - TaskQueuePtr q = NULL; - - q = (TaskQueuePtr)malloc(sizeof(TaskQueue)*(num+1)); + TaskQueuePtr q = (TaskQueuePtr)malloc(sizeof(TaskQueue)*(num+1)); - if (q == NULL) { - return -1; // throw... - } - q->next = taskQueuePool; - taskQueuePool = q; + // First Queue is previous pool + q->waiter = waiter; waiter = q; + q++; /* Connect all free queue in the pool */ - for (q = taskQueuePool + 1; --num > 0; q++) { - q->next = q + 1; + TaskQueuePtr p = q; + for (; --num > 0; p++) { + p->task = NULL; + p->waiter = NULL; + addLast(p); } - q->next = freeTaskQueue; - freeTaskQueue = taskQueuePool + 1; return 0; } @@ -48,17 +45,14 @@ TaskQueuePtr TaskQueueInfo::create(TaskPtr task) { - TaskQueuePtr q; - - if (freeTaskQueue == NULL) { - extend_pool(64); + TaskQueuePtr q = taskQueuePool.poll(); + if (! q) { + taskQueuePool.extend_pool(64); + q = taskQueuePool.poll(); } - q = freeTaskQueue; - freeTaskQueue = freeTaskQueue->next; - - q->task = task; q->next = q->prev = NULL; q->waiter = NULL; + q->task = task; return q; } @@ -67,33 +61,9 @@ void TaskQueueInfo::free_(TaskQueuePtr q) { - // if (!q) return; - q->next = freeTaskQueue; - q->prev = NULL; - freeTaskQueue = q; -} - - -void -TaskQueueInfo::destroy(void) -{ - TaskQueuePtr q, tmp; - -#if 1 - q = taskQueuePool; - while (q) { - tmp = q->next; - free(q); - q = tmp; - } -#else - for (q = taskQueuePool; q; q = q->next) { - free(q); - } -#endif - freeTaskQueue = taskQueuePool = NULL; - - + q->waiter = NULL; + q->task = NULL; + taskQueuePool.addLast(q); } @@ -140,8 +110,6 @@ int TaskQueueInfo::remove(TaskQueue* e) { - // if (!e) return 0; - e->prev->next = e->next; e->next->prev = e->prev;
--- a/TaskManager/kernel/ppe/TaskQueueInfo.h Fri Oct 09 11:29:42 2009 +0900 +++ b/TaskManager/kernel/ppe/TaskQueueInfo.h Sat Oct 10 17:30:29 2009 +0900 @@ -10,12 +10,10 @@ public: /* constructor */ TaskQueueInfo(); - ~TaskQueueInfo(); BASE_NEW_DELETE(TaskQueueInfo); /* functions */ - int init(int num); TaskQueuePtr create(Task *task); void free_(TaskQueuePtr queue); @@ -29,16 +27,16 @@ TaskQueue* get(int index); TaskQueue* find(Task *task); int empty(); - + void TaskQueueInfo::freePool() ; + + private: /* variables */ - TaskQueue* taskQueuePool; - TaskQueue* freeTaskQueue; + static TaskQueueInfo taskQueuePool; TaskQueue* first; TaskQueue* last; - /* functions */ int extend_pool(int num); void destroy();