NLMISC::CTaskManager Class Reference

#include <task_manager.h>

Inheritance diagram for NLMISC::CTaskManager:

NLMISC::IRunnable NLMISC::CAsyncFileManager

Detailed Description

CTaskManager is a class that manage a list of Task with one Thread
Author:
Alain Saffray

Nevrax France

Date:
2000

Definition at line 55 of file task_manager.h.

Public Member Functions

void addTask (IRunnable *, float priority=0)
 Add a task to TaskManager and its priority.

 CTaskManager ()
 Constructor.

bool deleteTask (IRunnable *r)
 Delete a task, only if task is not running, return true if found and deleted.

void dump (std::vector< std::string > &result)
 Dump task list.

virtual void getName (std::string &result) const
uint getNumWaitingTasks ()
 Get number of waiting task.

bool isTaskRunning () const
 Is there a current task ?

bool isThreadRunning () const
 return false if exit() is required. task added with addTask() should test this flag.

void registerTaskPriorityCallback (IChangeTaskPriority *callback)
 Register task priority callback.

void run (void)
 Manage TaskQueue.

void sleepTask (void)
 Sleep a Task.

uint taskListSize (void)
 Task list size.

 ~CTaskManager ()
 Destructeur.


Protected Member Functions

void waitCurrentTaskToComplete ()

Protected Attributes

CSynchronized< std::list<
std::string > > 
_DoneTaskQueue
CSynchronized< std::string > _RunningTask
 queue of tasks, using list container instead of queue for DeleteTask methode

CSynchronized< std::list<
CWaitingTask > > 
_TaskQueue
IThread_Thread
 thread pointer

volatile bool _ThreadRunning
 flag indicate thread loop, if false cause thread exit


Private Member Functions

void changeTaskPriority ()
 Register task priority callback.


Private Attributes

IChangeTaskPriority_ChangePriorityCallback
 The callback.

volatile bool _IsTaskRunning


Constructor & Destructor Documentation

NLMISC::CTaskManager::CTaskManager  ) 
 

Constructor.

Definition at line 39 of file task_manager.cpp.

References _ChangePriorityCallback, _IsTaskRunning, _RunningTask, _Thread, _ThreadRunning, and NLMISC::IThread::start().

00039                            : _RunningTask (""), _TaskQueue (""), _DoneTaskQueue ("")
00040 {
00041         _IsTaskRunning = false;
00042         _ThreadRunning = true;
00043         CSynchronized<string>::CAccessor currentTask(&_RunningTask);
00044         currentTask.value () = "";
00045         _Thread = IThread::create(this);
00046         _Thread->start();
00047         _ChangePriorityCallback = NULL;
00048 }

NLMISC::CTaskManager::~CTaskManager  ) 
 

Destructeur.

Definition at line 53 of file task_manager.cpp.

References _ThreadRunning, and NLMISC::nlSleep().

00054 {
00055         _ThreadRunning = false;
00056         while(!_ThreadRunning)
00057                 nlSleep(10);
00058 }


Member Function Documentation

void NLMISC::CTaskManager::addTask IRunnable ,
float  priority = 0
 

Add a task to TaskManager and its priority.

Definition at line 126 of file task_manager.cpp.

References _TaskQueue, and r.

Referenced by NLMISC::CAsyncFileManager::addLoadTask(), NL3D::CZoneManager::checkZonesAround(), NLMISC::CAsyncFileManager::loadFile(), NLMISC::CAsyncFileManager::loadFiles(), and NLMISC::CAsyncFileManager::signal().

00127 {
00128         CSynchronized<std::list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00129         acces.value().push_back(CWaitingTask(r, priority));
00130 }

void NLMISC::CTaskManager::changeTaskPriority  )  [private]
 

Register task priority callback.

Definition at line 216 of file task_manager.cpp.

References _ChangePriorityCallback, _TaskQueue, and NLMISC::CTaskManager::IChangeTaskPriority::getTaskPriority().

Referenced by run().

00217 {
00218         if (_ChangePriorityCallback)
00219         {
00220                 CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00221                 list<CWaitingTask> &taskList = acces.value();
00222                 
00223                 list<CWaitingTask>::iterator ite = taskList.begin();
00224                 while(ite != taskList.end())
00225                 {
00226                         // Get the new priority
00227                         ite->Priority = _ChangePriorityCallback->getTaskPriority(*(ite->Task));
00228                         
00229                         // Next task
00230                         ite++;
00231                 }
00232         }
00233 }

bool NLMISC::CTaskManager::deleteTask IRunnable r  ) 
 

Delete a task, only if task is not running, return true if found and deleted.

Definition at line 133 of file task_manager.cpp.

References _TaskQueue, and r.

00134 {
00135         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00136         for(list<CWaitingTask>::iterator it = acces.value().begin(); it != acces.value().end(); it++)
00137         {
00138                 if(it->Task == r)
00139                 {
00140                         acces.value().erase(it);
00141                         return true;
00142                 }
00143         }
00144         return false;
00145 }

void NLMISC::CTaskManager::dump std::vector< std::string > &  result  ) 
 

Dump task list.

Definition at line 163 of file task_manager.cpp.

References _DoneTaskQueue, _RunningTask, _TaskQueue, and NLMISC::toString().

00164 {
00165         CSynchronized<string>::CAccessor accesCurrent(&_RunningTask);
00166         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00167         CSynchronized<list<string> >::CAccessor accesDone(&_DoneTaskQueue);
00168 
00169         const list<CWaitingTask> &taskList = acces.value();
00170         const list<string> &taskDone = accesDone.value();
00171         const string &taskCurrent = accesCurrent.value();
00172         
00173         // Resize the destination array
00174         result.clear ();
00175         result.reserve (taskList.size () + taskDone.size () + 1);
00176 
00177         // Add the waiting strings
00178         list<string>::const_reverse_iterator iteDone = taskDone.rbegin ();
00179         while (iteDone != taskDone.rend ())
00180         {
00181                 result.push_back ("Done : " + *iteDone);
00182                 
00183                 // Next task
00184                 iteDone++;
00185         }
00186         
00187         // Add the current string
00188         if (!taskCurrent.empty())
00189         {
00190                 result.push_back ("Current : " + taskCurrent);
00191         }
00192 
00193         // Add the waiting strings
00194         list<CWaitingTask>::const_iterator ite = taskList.begin ();
00195         while (ite != taskList.end ())
00196         {
00197                 string name;
00198                 ite->Task->getName (name);
00199                 result.push_back ("Waiting : " + name + " " + toString(ite->Priority));
00200         
00201                 // Next task
00202                 ite++;
00203         }
00204 }

virtual void NLMISC::IRunnable::getName std::string &  result  )  const [inline, virtual, inherited]
 

Reimplemented in NL3D::CAsyncFileManager3D::CMeshLoad, NL3D::CAsyncFileManager3D::CIGLoad, NL3D::CAsyncFileManager3D::CIGLoadUser, NL3D::CAsyncFileManager3D::CTextureLoad, NL3D::CZoneLoadingTask, NLPACS::CGlobalRetriever::CLrLoader, NLMISC::CAsyncFileManager::CFileLoad, NLMISC::CAsyncFileManager::CMultipleFileLoad, and NLMISC::CAsyncFileManager::CSignal.

Definition at line 74 of file thread.h.

Referenced by run().

00075         {
00076                 result = "NoName";
00077         }

uint NLMISC::CTaskManager::getNumWaitingTasks  ) 
 

Get number of waiting task.

Definition at line 208 of file task_manager.cpp.

References _TaskQueue, and uint.

00209 {
00210         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00211         return acces.value().size();
00212 }

bool NLMISC::CTaskManager::isTaskRunning  )  const [inline]
 

Is there a current task ?

Definition at line 90 of file task_manager.h.

References _IsTaskRunning.

00090 {return _IsTaskRunning;}

bool NLMISC::CTaskManager::isThreadRunning  )  const [inline]
 

return false if exit() is required. task added with addTask() should test this flag.

Definition at line 81 of file task_manager.h.

References _ThreadRunning.

00081 {return _ThreadRunning;}

void NLMISC::CTaskManager::registerTaskPriorityCallback IChangeTaskPriority callback  ) 
 

Register task priority callback.

Definition at line 237 of file task_manager.cpp.

References _ChangePriorityCallback.

00238 {
00239         _ChangePriorityCallback = callback;
00240 }

void NLMISC::CTaskManager::run void   )  [virtual]
 

Manage TaskQueue.

Implements NLMISC::IRunnable.

Definition at line 61 of file task_manager.cpp.

References _DoneTaskQueue, _IsTaskRunning, _RunningTask, _TaskQueue, _ThreadRunning, changeTaskPriority(), NLMISC::IRunnable::getName(), NLMISC_DONE_TASK_SIZE, NLMISC::IRunnable::run(), sleepTask(), and NLMISC::toString().

00062 {
00063         IRunnable *runnableTask;
00064         float priorityTask;
00065         while(_ThreadRunning)
00066         {
00067                 {
00068                         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00069                         if(acces.value().empty())
00070                         {
00071                                 runnableTask = NULL;
00072                         }
00073                         else
00074                         {
00075                                 // Update task priorities
00076                                 changeTaskPriority ();
00077 
00078                                 // Get the best task
00079                                 list<CWaitingTask> &taskList = acces.value();
00080                                 list<CWaitingTask>::iterator ite = taskList.begin();
00081                                 list<CWaitingTask>::iterator bestIte = ite;
00082                                 while (ite != taskList.end())
00083                                 {
00084                                         if (ite->Priority < bestIte->Priority)
00085                                                 bestIte = ite;
00086                                         
00087                                         // Next task;
00088                                         ite++;
00089                                 }
00090 
00091                                 _IsTaskRunning = true;
00092                                 runnableTask = bestIte->Task;
00093                                 priorityTask = bestIte->Priority;
00094                                 taskList.erase (bestIte);
00095                         }
00096                 }
00097                 if(runnableTask)
00098                 {
00099                         {
00100                                 CSynchronized<string>::CAccessor currentTask(&_RunningTask);
00101                                 string temp;
00102                                 runnableTask->getName(temp);
00103                                 currentTask.value () = temp + " " + toString (priorityTask);
00104                         }
00105                         runnableTask->run();
00106                         {
00107                                 CSynchronized<string>::CAccessor currentTask(&_RunningTask);
00108                                 CSynchronized<list<string> >::CAccessor doneTask(&_DoneTaskQueue);
00109                                 doneTask.value().push_front (currentTask.value ());
00110                                 currentTask.value () = "";
00111                                 if (doneTask.value().size () > NLMISC_DONE_TASK_SIZE)
00112                                         doneTask.value().resize (NLMISC_DONE_TASK_SIZE);
00113                         }
00114 
00115                         _IsTaskRunning = false;
00116                 }
00117                 else
00118                 {
00119                         sleepTask();
00120                 }
00121         }
00122         _ThreadRunning = true;
00123 }

void NLMISC::CTaskManager::sleepTask void   )  [inline]
 

Sleep a Task.

Definition at line 75 of file task_manager.h.

References NLMISC::nlSleep().

Referenced by run(), and waitCurrentTaskToComplete().

00075 { nlSleep(10); }

uint NLMISC::CTaskManager::taskListSize void   ) 
 

Task list size.

Definition at line 148 of file task_manager.cpp.

References _TaskQueue, and uint.

00149 {
00150         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00151         return acces.value().size();
00152 }

void NLMISC::CTaskManager::waitCurrentTaskToComplete  )  [protected]
 

If any, wait the current running task to complete this function MUST be called in a 'accessor to the _TaskQueue' statement because a mutex is required eg: { CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue); waitCurrentTaskToComplete(); }

Definition at line 155 of file task_manager.cpp.

References _IsTaskRunning, and sleepTask().

Referenced by NLMISC::CAsyncFileManager::cancelLoadTask().

00156 {
00157         while (_IsTaskRunning)
00158                 sleepTask();
00159 }


Field Documentation

IChangeTaskPriority* NLMISC::CTaskManager::_ChangePriorityCallback [private]
 

The callback.

Definition at line 108 of file task_manager.h.

Referenced by changeTaskPriority(), CTaskManager(), and registerTaskPriorityCallback().

CSynchronized<std::list<std::string> > NLMISC::CTaskManager::_DoneTaskQueue [protected]
 

Definition at line 148 of file task_manager.h.

Referenced by dump(), and run().

volatile bool NLMISC::CTaskManager::_IsTaskRunning [private]
 

Definition at line 158 of file task_manager.h.

Referenced by CTaskManager(), isTaskRunning(), run(), and waitCurrentTaskToComplete().

CSynchronized<std::string> NLMISC::CTaskManager::_RunningTask [protected]
 

queue of tasks, using list container instead of queue for DeleteTask methode

Definition at line 146 of file task_manager.h.

Referenced by CTaskManager(), dump(), and run().

CSynchronized<std::list<CWaitingTask> > NLMISC::CTaskManager::_TaskQueue [protected]
 

Definition at line 147 of file task_manager.h.

Referenced by addTask(), changeTaskPriority(), deleteTask(), dump(), getNumWaitingTasks(), run(), and taskListSize().

IThread* NLMISC::CTaskManager::_Thread [protected]
 

thread pointer

Definition at line 151 of file task_manager.h.

Referenced by CTaskManager().

volatile bool NLMISC::CTaskManager::_ThreadRunning [protected]
 

flag indicate thread loop, if false cause thread exit

Definition at line 154 of file task_manager.h.

Referenced by CTaskManager(), isThreadRunning(), run(), and ~CTaskManager().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:37:19 2004 for NeL by doxygen 1.3.6