NLMISC::CAsyncFileManager Class Reference

#include <async_file_manager.h>

Inheritance diagram for NLMISC::CAsyncFileManager:

NLMISC::CTaskManager NLMISC::IRunnable

Detailed Description

CAsyncFileManager is a class that manage file loading in a seperate thread
Author:
Matthieu Besson

Nevrax France

Date:
2002

Definition at line 41 of file async_file_manager.h.

Public Member Functions

void addLoadTask (IRunnable *ploadTask)
 Add a load task in the CAsyncFileManager task list.

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

bool cancelLoadTask (const ICancelCallback &cancelCallBack)
void cancelSignal (bool *pSgn)
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 loadFile (const std::string &fileName, uint8 **pPtr)
void loadFiles (const std::vector< std::string > &vFileNames, const std::vector< uint8 ** > &vPtrs)
void registerTaskPriorityCallback (IChangeTaskPriority *callback)
 Register task priority callback.

void run (void)
 Manage TaskQueue.

void signal (bool *pSgn)
void sleepTask (void)
 Sleep a Task.

uint taskListSize (void)
 Task list size.


Static Public Member Functions

CAsyncFileManagergetInstance ()
void terminate ()

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

 CAsyncFileManager ()

Static Private Attributes

CAsyncFileManager_Singleton = NULL


Constructor & Destructor Documentation

NLMISC::CAsyncFileManager::CAsyncFileManager  )  [private]
 

Definition at line 40 of file async_file_manager.cpp.

Referenced by getInstance().

00041 {
00042 }


Member Function Documentation

void NLMISC::CAsyncFileManager::addLoadTask IRunnable ploadTask  ) 
 

Add a load task in the CAsyncFileManager task list.

Definition at line 67 of file async_file_manager.cpp.

References NLMISC::CTaskManager::addTask().

00068 {
00069         addTask(ploadTask);
00070 }

void NLMISC::CTaskManager::addTask IRunnable ,
float  priority = 0
[inherited]
 

Add a task to TaskManager and its priority.

Definition at line 126 of file task_manager.cpp.

References NLMISC::CTaskManager::_TaskQueue, and r.

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

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

bool NLMISC::CAsyncFileManager::cancelLoadTask const ICancelCallback cancelCallBack  ) 
 

Call this method to cancel a programmed load.

Returns:
False if the task either already ended or running.

Definition at line 72 of file async_file_manager.cpp.

References NLMISC::CAsyncFileManager::ICancelCallback::callback(), and NLMISC::CTaskManager::waitCurrentTaskToComplete().

00073 {
00074         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00075         list<CWaitingTask> &rTaskQueue = acces.value ();
00076         list<CWaitingTask>::iterator it = rTaskQueue.begin();
00077 
00078         while (it != rTaskQueue.end())
00079         {
00080                 IRunnable *pR = it->Task;
00081 
00082                 // check the task with the cancel callback.
00083                 if (callback.callback(pR))
00084                 {
00085                         // Delete the load task
00086                         delete pR;
00087                         rTaskQueue.erase (it);
00088                         return true;
00089                 }
00090                 ++it;
00091         }
00092 
00093         // If not found, the current running task may be the one we want to cancel. Must wait it.
00094         waitCurrentTaskToComplete ();
00095 
00096         return false;
00097 }

void NLMISC::CAsyncFileManager::cancelSignal bool *  pSgn  ) 
 

Definition at line 170 of file async_file_manager.cpp.

References NLMISC::CAsyncFileManager::CSignal::Sgn.

00171 {
00172         CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
00173         list<CWaitingTask> &rTaskQueue = acces.value ();
00174         list<CWaitingTask>::iterator it = rTaskQueue.begin();
00175 
00176         while (it != rTaskQueue.end())
00177         {
00178                 IRunnable *pR = it->Task;
00179                 CSignal *pS = dynamic_cast<CSignal*>(pR);
00180                 if (pS != NULL)
00181                 {
00182                         if (pS->Sgn == pSgn)
00183                         {
00184                                 // Delete signal task
00185                                 delete pS;
00186                                 rTaskQueue.erase (it);
00187                                 return;
00188                         }
00189                 }
00190                 ++it;
00191         }
00192 }

bool NLMISC::CTaskManager::deleteTask IRunnable r  )  [inherited]
 

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 NLMISC::CTaskManager::_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  )  [inherited]
 

Dump task list.

Definition at line 163 of file task_manager.cpp.

References NLMISC::CTaskManager::_DoneTaskQueue, NLMISC::CTaskManager::_RunningTask, NLMISC::CTaskManager::_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 }

CAsyncFileManager & NLMISC::CAsyncFileManager::getInstance  )  [static]
 

Definition at line 46 of file async_file_manager.cpp.

References CAsyncFileManager().

Referenced by terminate().

00047 {
00048         if (_Singleton == NULL)
00049         {
00050                 _Singleton = new CAsyncFileManager();
00051         }
00052         return *_Singleton;
00053 }

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 NLMISC::CTaskManager::run().

00075         {
00076                 result = "NoName";
00077         }

uint NLMISC::CTaskManager::getNumWaitingTasks  )  [inherited]
 

Get number of waiting task.

Definition at line 208 of file task_manager.cpp.

References NLMISC::CTaskManager::_TaskQueue, and uint.

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

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

Is there a current task ?

Definition at line 90 of file task_manager.h.

References NLMISC::CTaskManager::_IsTaskRunning.

00090 {return _IsTaskRunning;}

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

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

Definition at line 81 of file task_manager.h.

References NLMISC::CTaskManager::_ThreadRunning.

00081 {return _ThreadRunning;}

void NLMISC::CAsyncFileManager::loadFile const std::string &  fileName,
uint8 **  pPtr
 

Definition at line 149 of file async_file_manager.cpp.

References NLMISC::CTaskManager::addTask(), and uint8.

00150 {
00151         addTask (new CFileLoad (sFileName, ppFile));
00152 }

void NLMISC::CAsyncFileManager::loadFiles const std::vector< std::string > &  vFileNames,
const std::vector< uint8 ** > &  vPtrs
 

Definition at line 156 of file async_file_manager.cpp.

References NLMISC::CTaskManager::addTask().

00157 {
00158         addTask (new CMultipleFileLoad (vFileNames, vPtrs));
00159 }

void NLMISC::CTaskManager::registerTaskPriorityCallback IChangeTaskPriority callback  )  [inherited]
 

Register task priority callback.

Definition at line 237 of file task_manager.cpp.

References NLMISC::CTaskManager::_ChangePriorityCallback.

00238 {
00239         _ChangePriorityCallback = callback;
00240 }

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

Manage TaskQueue.

Implements NLMISC::IRunnable.

Definition at line 61 of file task_manager.cpp.

References NLMISC::CTaskManager::_DoneTaskQueue, NLMISC::CTaskManager::_IsTaskRunning, NLMISC::CTaskManager::_RunningTask, NLMISC::CTaskManager::_TaskQueue, NLMISC::CTaskManager::_ThreadRunning, NLMISC::CTaskManager::changeTaskPriority(), NLMISC::IRunnable::getName(), NLMISC_DONE_TASK_SIZE, NLMISC::IRunnable::run(), NLMISC::CTaskManager::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::CAsyncFileManager::signal bool *  pSgn  ) 
 

Definition at line 163 of file async_file_manager.cpp.

References NLMISC::CTaskManager::addTask().

00164 {
00165         addTask (new CSignal (pSgn));
00166 }

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

Sleep a Task.

Definition at line 75 of file task_manager.h.

References NLMISC::nlSleep().

Referenced by NLMISC::CTaskManager::run(), and NLMISC::CTaskManager::waitCurrentTaskToComplete().

00075 { nlSleep(10); }

uint NLMISC::CTaskManager::taskListSize void   )  [inherited]
 

Task list size.

Definition at line 148 of file task_manager.cpp.

References NLMISC::CTaskManager::_TaskQueue, and uint.

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

void NLMISC::CAsyncFileManager::terminate  )  [static]
 

Definition at line 57 of file async_file_manager.cpp.

References getInstance().

00058 {
00059         if (_Singleton != NULL)
00060         {
00061                 delete &getInstance();
00062                 _Singleton = NULL;
00063         }
00064 }

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

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 NLMISC::CTaskManager::_IsTaskRunning, and NLMISC::CTaskManager::sleepTask().

Referenced by cancelLoadTask().

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


Field Documentation

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

Definition at line 148 of file task_manager.h.

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

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

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

Definition at line 146 of file task_manager.h.

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

CAsyncFileManager * NLMISC::CAsyncFileManager::_Singleton = NULL [static, private]
 

Definition at line 36 of file async_file_manager.cpp.

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

Definition at line 147 of file task_manager.h.

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

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

thread pointer

Definition at line 151 of file task_manager.h.

Referenced by NLMISC::CTaskManager::CTaskManager().

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

flag indicate thread loop, if false cause thread exit

Definition at line 154 of file task_manager.h.

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


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