webkit中timer实现 分析.docx
- 文档编号:17120046
- 上传时间:2023-07-22
- 格式:DOCX
- 页数:8
- 大小:16.39KB
webkit中timer实现 分析.docx
《webkit中timer实现 分析.docx》由会员分享,可在线阅读,更多相关《webkit中timer实现 分析.docx(8页珍藏版)》请在冰点文库上搜索。
webkit中timer实现分析
webkit中timer实现分析
1.webkit中timer实现的框架类图
1.1TimerHeap其实就是一个容器,管理一个一个的Timer
1.2ThreadTimers相当于整个timer模块的管理者,负责调度SharedTimer并且操作TimerHeap
1.3SharedTimer是平台相关的一个类源码如下:
view
plaincopy
toclipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150public:
virtual~SharedTimer(){}
virtualvoidsetFiredFunction(void(*)())=0;
//ThefiretimeisrelativetotheclassicPOSIXepochofJanuary1,1970,
//astheresultofcurrentTime()is.
virtualvoidsetFireTime(double)=0;
virtualvoidstop()=0;
};
//Implementedbyport(sinceitprovidestherunloopforthemainthread).
//FIXME:
makeportsimplementMainThreadSharedTimerdirectlyinstead.
voidsetSharedTimerFiredFunction(void(*)());
voidsetSharedTimerFireTime(double);
voidstopSharedTimer();
//ImplementationofSharedTimerforthemainthread.
classMainThreadSharedTimer:
publicSharedTimer{
public:
virtualvoidsetFiredFunction(void(*function)())
{
setSharedTimerFiredFunction(function);
}
virtualvoidsetFireTime(doublefireTime)
{
setSharedTimerFireTime(fireTime);
}
virtualvoidstop()
{
stopSharedTimer();
}
};
需要设置一个回调函数给底层的Timer,也是通过这个回调,底层timer与上层的结合起来
回调函数是ThreadTimers的一个成员函数,代码如下:
view
plaincopy
toclipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150voidThreadTimers:
:
sharedTimerFiredInternal()
{
//Doare-entrancycheck.
if(m_firingTimers)
return;
m_firingTimers=true;
doublefireTime=currentTime();
doubletimeToQuit=fireTime+maxDurationOfFiringTimers;
while(!
m_timerHeap.isEmpty()&&m_timerHeap.first()->m_nextFireTime<=fireTime){
TimerBase*timer=m_timerHeap.first();
timer->m_nextFireTime=0;
timer->heapDeleteMin();
doubleinterval=timer->repeatInterval();
timer->setNextFireTime(interval?
fireTime+interval:
0);
//Oncethetimerhasbeenfired,itmaybedeleted,sodonothingelsewithitafterthispoint.
timer->fired();
//Catchthecasewherethetimeraskedtimerstofireinanestedeventloop,orweareovertimelimit.
if(!
m_firingTimers||timeToQuit<currentTime())
break;
}
m_firingTimers=false;
updateSharedTimer();
}
通过这个回调,ThreadTimers就从TimerHeap中取出一个个的Timer来执行
1.5注意Webkit中timer的实现是用模板实现,直接上源代码
view
plaincopy
toclipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150template<typenameTimerFiredClass>classTimer:
publicTimerBase{
public:
typedefvoid(TimerFiredClass:
:
*TimerFiredFunction)(Timer*);
Timer(TimerFiredClass*o,TimerFiredFunctionf)
:
m_object(o),m_function(f){}
private:
virtualvoidfired(){(m_object->*m_function)(this);}
TimerFiredClass*m_object;
TimerFiredFunctionm_function;
};
2.webkit中如何应用timer
通过小弟几天的分析,不知正确与否,webkit主要是通过这个timer实现异步,下面以一个场景为例,呈上时序图
场景描述:
DOMHTMLParser解析html文档的时候,如果html文档的标签数超过了引擎规定的默认标签且解析时间超过了允许的最长解析时间,会停止解析,通过一个timer异步,之后再timer回调中恢复解析
时序图如下:
timer回调得到执行的时序图如下:
HTMLDocumentSheduler初始化timer的代码:
view
plaincopy
toclipboardprint?
类成员:
Timer<HTMLParserScheduler>m_continueNextChunkTimer;
构造函数:
HTMLParserScheduler:
:
HTMLParserScheduler(HTMLDocumentParser*parser)
:
m_parser(parser)
m_parserTimeLimit(parserTimeLimit(m_parser->document()->page()))
m_parserChunkSize(parserChunkSize(m_parser->document()->page()))
m_continueNextChunkTimer(this,&HTMLParserScheduler:
:
continueNextChunkTimerFired)
{
}
这样整个timer模块的设计思路和应用场景就清楚了
3.系统如何初始化timer模块
神马都是浮云,直接上时序图
Timer源码:
view
plaincopy
toclipboardprint?
#ifndefTimer_h
#defineTimer_h
#include<wtf/Noncopyable.h>
#include<wtf/Threading.h>
namespaceWebCore{
//Timeintervalsareallinseconds.
classTimerHeapElement;
classTimerBase:
publicNoncopyable{
public:
TimerBase();
virtual~TimerBase();
voidstart(doublenextFireInterval,doublerepeatInterval);
voidstartRepeating(doublerepeatInterval){start(repeatInterval,repeatInterval);}
voidstartOneShot(doubleinterval){start(interval,0);}
voidstop();
boolisActive()const;
doublenextFireInterval()const;
doublerepeatInterval()const{returnm_repeatInterval;}
voidaugmentRepeatInterval(doubledelta){setNextFireTime(m_nextFireTime+delta);m_repeatInterval+=delta;}
staticvoidfireTimersInNestedEventLoop();
private:
virtualvoidfired()=0;
voidcheckConsistency()const;
voidcheckHeapIndex()const;
voidsetNextFireTime(double);
boolinHeap()const{returnm_heapIndex!
=-1;}
voidheapDecreaseKey();
voidheapDelete();
voidheapDeleteMin();
voidheapIncreaseKey();
voidheapInsert();
voidheapPop();
voidheapPopMin();
doublem_nextFireTime;//0ifinactive
doublem_repeatInterval;//0ifnotrepeating
intm_heapIndex;//-1ifnotinheap
unsignedm_heapInsertionOrder;//Usedtokeeporderamongequal-fire-timetimers
#ifndefNDEBUG
ThreadIdentifierm_thread;
#endif
friendclassTimerHeapElement;
friendclassThreadTimers;
friendbooloperator<(constTimerHeapElement&,constTimerHeapElement&);
};
template<typenameTimerFiredClass>classTimer:
publicTimerBase{
public:
typedefvoid(TimerFiredClass:
:
*TimerFiredFunction)(Timer*);
Timer(TimerFiredClass*o,TimerFiredFunctionf)
:
m_object(o),m_function(f){}
private:
virtualvoidfired(){(m_object->*m_function)(this);}
TimerFiredClass*m_object;
TimerFiredFunctionm_function;
};
inlineboolTimerBase:
:
isActive()const
{
ASSERT(m_thread==currentThread());
returnm_nextFireTime;
}
}
#endif
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- webkit中timer实现 分析 webkit timer 实现
![提示](https://static.bingdoc.com/images/bang_tan.gif)