DSS Source Code Analyse (07) - TaskThread::Entry
来源:互联网 发布:网络法律法规,散布 编辑:程序博客网 时间:2024/05/16 05:34
void TaskThread::Entry()
{
Task* theTask = NULL;
while (true)
{
// get task from heap, if no in heap, get from queue
theTask = this->WaitForTask();
//
// WaitForTask returns NULL when it is time to quit
if (theTask == NULL || false == theTask->Valid() )
return;
Bool16 doneProcessingEvent = false;
while (!doneProcessingEvent)
{
//If a task holds locks when it returns from its Run function,
//that would be catastrophic and certainly lead to a deadlock
#if DEBUG
Assert(this->GetNumLocksHeld() == 0);
Assert(theTask->fInRunCount == 0);
theTask->fInRunCount++;
#endif
// what means?
theTask->fUseThisThread = NULL; // Each invocation of Run must independently
// request a specific thread.
SInt64 theTimeout = 0;
if (theTask->fWriteLock)
{
OSMutexWriteLocker mutexLocker(&TaskThreadPool::sMutexRW);
if (TASK_DEBUG) qtss_printf("TaskThread::Entry run global locked TaskName=%s CurMSec=%.3f thread=%p task=%p\n", theTask->fTaskName, OS::StartTimeMilli_Float() ,(void *) this,(void *) theTask);
// process the task for write
theTimeout = theTask->Run();
theTask->fWriteLock = false;
}
else
{
OSMutexReadLocker mutexLocker(&TaskThreadPool::sMutexRW);
if (TASK_DEBUG) qtss_printf("TaskThread::Entry run TaskName=%s CurMSec=%.3f thread=%p task=%p\n", theTask->fTaskName, OS::StartTimeMilli_Float(), (void *) this,(void *) theTask);
// process the task for read
theTimeout = theTask->Run();
}
#if DEBUG
Assert(this->GetNumLocksHeld() == 0);
theTask->fInRunCount--;
Assert(theTask->fInRunCount == 0);
#endif
if (theTimeout < 0)
{
// The task will be deleted.
if (TASK_DEBUG)
{
qtss_printf("TaskThread::Entry delete TaskName=%s CurMSec=%.3f thread=%p task=%p\n", theTask->fTaskName, OS::StartTimeMilli_Float(), (void *) this, (void *) theTask);
theTask->fUseThisThread = NULL;
if (NULL != fHeap.Remove(&theTask->fTimerHeapElem))
qtss_printf("TaskThread::Entry task still in heap before delete\n");
if (NULL != theTask->fTaskQueueElem.InQueue())
qtss_printf("TaskThread::Entry task still in queue before delete\n");
theTask->fTaskQueueElem.Remove();
if (theTask->fEvents &~ Task::kAlive)
qtss_printf ("TaskThread::Entry flags still set before delete\n");
(void)atomic_sub(&theTask->fEvents, 0);
::strncat (theTask->fTaskName, " deleted", sizeof(theTask->fTaskName) -1);
}
theTask->fTaskName[0] = 'D'; //mark as dead
delete theTask;
theTask = NULL;
doneProcessingEvent = true;
}
else if (theTimeout == 0)
{
//We want to make sure that 100% definitely the task's Run function WILL
//be invoked when another thread calls Signal. We also want to make sure
//that if an event sneaks in right as the task is returning from Run()
//(via Signal) that the Run function will be invoked again.
if (doneProcessingEvent)
theTask = NULL;
}
else
{
// the Task will be continue to be processed next time at OS::Milliseconds() + theTimeout
//note that if we get here, we don't reset theTask, so it will get passed into
//WaitForTask
if (TASK_DEBUG) qtss_printf("TaskThread::Entry insert TaskName=%s in timer heap thread=%p elem=%p task=%p timeout=%.2f\n", theTask->fTaskName, (void *) this, (void *) &theTask->fTimerHeapElem,(void *) theTask, (float)theTimeout / (float) 1000);
// set wait time for timer
theTask->fTimerHeapElem.SetValue(OS::Milliseconds() + theTimeout);
// insert heapelem of task into heap
fHeap.Insert(&theTask->fTimerHeapElem);
(void)atomic_or(&theTask->fEvents, Task::kIdleEvent);
doneProcessingEvent = true;
}
#if TASK_DEBUG
SInt64 yieldStart = OS::Milliseconds();
#endif
this->ThreadYield();
#if TASK_DEBUG
SInt64 yieldDur = OS::Milliseconds() - yieldStart;
static SInt64 numZeroYields;
if ( yieldDur > 1 )
{
if (TASK_DEBUG) qtss_printf( "TaskThread::Entry time in Yield %qd, numZeroYields %qd \n", yieldDur, numZeroYields );
numZeroYields = 0;
}
else
numZeroYields++;
#endif
}
}
}
{
Task* theTask = NULL;
while (true)
{
// get task from heap, if no in heap, get from queue
theTask = this->WaitForTask();
//
// WaitForTask returns NULL when it is time to quit
if (theTask == NULL || false == theTask->Valid() )
return;
Bool16 doneProcessingEvent = false;
while (!doneProcessingEvent)
{
//If a task holds locks when it returns from its Run function,
//that would be catastrophic and certainly lead to a deadlock
#if DEBUG
Assert(this->GetNumLocksHeld() == 0);
Assert(theTask->fInRunCount == 0);
theTask->fInRunCount++;
#endif
// what means?
theTask->fUseThisThread = NULL; // Each invocation of Run must independently
// request a specific thread.
SInt64 theTimeout = 0;
if (theTask->fWriteLock)
{
OSMutexWriteLocker mutexLocker(&TaskThreadPool::sMutexRW);
if (TASK_DEBUG) qtss_printf("TaskThread::Entry run global locked TaskName=%s CurMSec=%.3f thread=%p task=%p\n", theTask->fTaskName, OS::StartTimeMilli_Float() ,(void *) this,(void *) theTask);
// process the task for write
theTimeout = theTask->Run();
theTask->fWriteLock = false;
}
else
{
OSMutexReadLocker mutexLocker(&TaskThreadPool::sMutexRW);
if (TASK_DEBUG) qtss_printf("TaskThread::Entry run TaskName=%s CurMSec=%.3f thread=%p task=%p\n", theTask->fTaskName, OS::StartTimeMilli_Float(), (void *) this,(void *) theTask);
// process the task for read
theTimeout = theTask->Run();
}
#if DEBUG
Assert(this->GetNumLocksHeld() == 0);
theTask->fInRunCount--;
Assert(theTask->fInRunCount == 0);
#endif
if (theTimeout < 0)
{
// The task will be deleted.
if (TASK_DEBUG)
{
qtss_printf("TaskThread::Entry delete TaskName=%s CurMSec=%.3f thread=%p task=%p\n", theTask->fTaskName, OS::StartTimeMilli_Float(), (void *) this, (void *) theTask);
theTask->fUseThisThread = NULL;
if (NULL != fHeap.Remove(&theTask->fTimerHeapElem))
qtss_printf("TaskThread::Entry task still in heap before delete\n");
if (NULL != theTask->fTaskQueueElem.InQueue())
qtss_printf("TaskThread::Entry task still in queue before delete\n");
theTask->fTaskQueueElem.Remove();
if (theTask->fEvents &~ Task::kAlive)
qtss_printf ("TaskThread::Entry flags still set before delete\n");
(void)atomic_sub(&theTask->fEvents, 0);
::strncat (theTask->fTaskName, " deleted", sizeof(theTask->fTaskName) -1);
}
theTask->fTaskName[0] = 'D'; //mark as dead
delete theTask;
theTask = NULL;
doneProcessingEvent = true;
}
else if (theTimeout == 0)
{
//We want to make sure that 100% definitely the task's Run function WILL
//be invoked when another thread calls Signal. We also want to make sure
//that if an event sneaks in right as the task is returning from Run()
//(via Signal) that the Run function will be invoked again.
// All datas had finished sending, run will be called next time when any event occured.
doneProcessingEvent = compare_and_store(Task::kAlive, 0, &theTask->fEvents);if (doneProcessingEvent)
theTask = NULL;
}
else
{
// the Task will be continue to be processed next time at OS::Milliseconds() + theTimeout
//note that if we get here, we don't reset theTask, so it will get passed into
//WaitForTask
if (TASK_DEBUG) qtss_printf("TaskThread::Entry insert TaskName=%s in timer heap thread=%p elem=%p task=%p timeout=%.2f\n", theTask->fTaskName, (void *) this, (void *) &theTask->fTimerHeapElem,(void *) theTask, (float)theTimeout / (float) 1000);
// set wait time for timer
theTask->fTimerHeapElem.SetValue(OS::Milliseconds() + theTimeout);
// insert heapelem of task into heap
fHeap.Insert(&theTask->fTimerHeapElem);
(void)atomic_or(&theTask->fEvents, Task::kIdleEvent);
doneProcessingEvent = true;
}
#if TASK_DEBUG
SInt64 yieldStart = OS::Milliseconds();
#endif
this->ThreadYield();
#if TASK_DEBUG
SInt64 yieldDur = OS::Milliseconds() - yieldStart;
static SInt64 numZeroYields;
if ( yieldDur > 1 )
{
if (TASK_DEBUG) qtss_printf( "TaskThread::Entry time in Yield %qd, numZeroYields %qd \n", yieldDur, numZeroYields );
numZeroYields = 0;
}
else
numZeroYields++;
#endif
}
}
}
- DSS Source Code Analyse (07) - TaskThread::Entry
- DSS Source Code Analyse (03) - EventThread::Entry
- DSS Source Code Analyse (01) - StartServer
- DSS Source Code Analyse (02) - select_waitevent
- DSS Source Code Analyse (04) - TCPListenerSocket::ProcessEvent
- DSS Source Code Analyse (05) - EventContext::ProcessEvent
- DSS Source Code Analyse (06) - Task::Signal
- DSS Source Code Analyse (08) - EventContext::RequestEvent
- DSS Source Code Analyse (09) - RTSPSession::Run
- DSS Source Code Analyse (10) - RTPSession::Run
- DSS Source Code Analyse (11) - TimeoutTask
- DSS Source Code Analyse (19) - Authentication
- DSS Source Code Analyse (20) - HttpProxy
- DSS Source Code Analyse (21) - Filter
- DSS Source Code Analyse (23) - OSHashTable
- DSS Source Code Analyse (24) - OSHeap
- DSS Source Code Analyse (25) - OSQueue
- DSS Source Code Analyse (26) - OSRef
- [ solr入门 ] - 在eclipse中发布solr
- 摘录:Liz Murray 在 DePauw University的演讲
- 四两拨千斤的工作小工具分享!
- js的Boolean对象初始值
- 用nodepad++将代码文件导出为带行号的html文件
- DSS Source Code Analyse (07) - TaskThread::Entry
- ubuntu下面安装虚拟机xp
- 火鸟Firebird数据库的中文参考资料
- FLV --flash video格式介绍
- Oracle 常用的几个通用函数
- JAVA开发者最常去网站
- 对物联网的看法
- Qt4 Menubar and Toolbar
- 北京园博会