谁见过这代码,能释得通,算你技术牛

weixin_42068372 2018-04-27 06:39:37
void ServerEngine::mainloop(BOOL bJumpStart)
{
int n;

Timeval*timeoutp;
Timeval timeout;
Timeval timeout1, timeout2;

Timeval last_left_select_time;
Timeval last_in_select_time;

*g_ppLastEngine = this;
int ttt = 0;
while(!m_bQuit)
{
//if (proc->procnum()== 0 || proc->procnum()== 4|| proc->procnum()== 1)
//printf("on %d---%d\n", proc->procnum(),ttt);

timeout = Timeval(m_sec, m_usec);
if (schedule.empty() && ischedule.empty())
{
timeoutp = &timeout;
}
else
{
timeoutp = &timeout;
GETTIMEOFDAY(now);
printf("my now tv_user:%ld\n",now.tv_usec);
timeout1 = schedule.empty() ? Timeval(600000, 0) : schedule.head_time() - now;
timeout2 = ischedule.empty() ? Timeval(600000, 0) : ischedule.head_time() - now;

if (timeout1 < timeout)
timeout = timeout1;
if (timeout2 < timeout)
timeout = timeout2;

if (proc->procnum()== PROC_CORE)
DPRINTF(D_SELECT, ("==timeout: %ld.%06ld, (%ld.%06ld) from now\n",
schedule.head_time().tv_sec%100,
schedule.head_time().tv_usec,
timeout.tv_sec%100, timeout.tv_usec));

}
if (timeout < 0.0)
{
timeout = Timeval(0.0);
}

last_in_select_time = now;
int i = 0;
n = callbacks.Select((struct timeval*)timeoutp);
#if 0
/*LIANGWL*/
if (proc->procnum()== PROC_CORE && n > 0)
{
//n = callbacks.Select((struct timeval*)timeoutp,NULL);
DPRINTF(D_ERROR, ("Select n = %d \n", n));
}
else
{
//n = callbacks.Select((struct timeval*)timeoutp);
}
#endif
m_ulMainloopIterations++;

GETTIMEOFDAY(now);
last_left_select_time = now;

m_pICurrentElem = ischedule.get_execute_list(now);
m_pSCurrentElem = schedule.get_execute_list(now);

if (n < 0)
{
m_bMoreReaderOrWriter = FALSE;
m_bMoreTSReader = FALSE;

if (errno == EBADF)
{
callbacks.HandleBadFds();
}
}
else if (n == 0)
{
m_bMoreReaderOrWriter = FALSE;
m_bMoreTSReader = FALSE;
}
else
{
m_bMoreReaderOrWriter = TRUE;
m_bMoreTSReader = TRUE;
callbacks.invoke_start();
}

m_bOnce = TRUE;
m_bForceSelect = FALSE;

m_CrashState = IN_LOOP;
while (m_pICurrentElem ||
m_pSCurrentElem ||
m_bMoreReaderOrWriter ||
m_bMoreTSReader ||
m_bOnce)
{
BOOL bCondition;
int i;
if (m_bOnce)
{
m_CrashState = IN_DISPATCHQ;
proc->pc->dispatchq->execute(proc);
m_CrashState = IN_LOOP;
m_bOnce = FALSE;
}

if (m_pICurrentElem || m_bMoreTSReader)
{
if (m_bMoreTSReader)
{
m_bMoreTSReader = callbacks.invoke_n_ts(5, 0, NULL, NULL);
}
for (i = 0; i < 10 && m_pICurrentElem; i++)
{
m_pICurrentElem = ischedule.execute_element(m_pICurrentElem);
}
//if (proc->procnum()== 0 || proc->procnum()== 4|| proc->procnum()== 1)
//printf("%d %d \n",proc->procnum(),i);

if (m_pICurrentElem == NULL && m_bForceSelect == FALSE)
{
GETTIMEOFDAY(now);

if (last_left_select_time < now - 0.333)
{
m_bForceSelect = TRUE;
}
else
{
m_pICurrentElem = ischedule.get_execute_list(now);
}
}

bCondition = ((m_bOnce || m_pSCurrentElem || m_bMoreReaderOrWriter));
}
else
{
bCondition = (m_bOnce || m_pSCurrentElem || m_bMoreReaderOrWriter);
}

if (bCondition)
{
m_bMutexProtection = TRUE;
for (i = 0; i < 5 && m_pSCurrentElem; i++)
{
m_pSCurrentElem = schedule.execute_element(m_pSCurrentElem);
}

if (m_bMoreReaderOrWriter)
{
m_bMoreReaderOrWriter = callbacks.invoke_n(5, 5, NULL, NULL);
}

if (m_bOnce)
{
m_CrashState = IN_DISPATCHQ;
proc->pc->dispatchq->execute(proc);
m_CrashState = IN_LOOP;
m_bOnce = FALSE;
}
m_bMutexProtection = FALSE;
}
}
m_CrashState = NONE;
//if (proc->procnum()== 0 || proc->procnum()== 4|| proc->procnum()== 1)
//if(ttt%500 == 1){
//printf(" tick %d---%d\n", proc->procnum(),ttt);
//}

//ttt++;
}

//printf("leave func %d mainloop\n", proc->pc->process_type-1);
// bit_clear(&g_nQuitedMutex, proc->pc->process_type-1);
bit_clear(&g_nQuitedMutex, proc->procnum());
}
...全文
1492 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
john_liqinghan 2018-08-30
  • 打赏
  • 举报
回复
看了一下,应该是一个调度器,调度任务来着。
_杰杰_ 2018-05-30
  • 打赏
  • 举报
回复
看到这种毫无注释的代码就头大

21,600

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧