最近在看crtmp源代碼,看到timersmanager 子產品時感覺很難了解,花了不少時間反複思考該子產品
的邏輯,現在思考的結果記錄下來,友善以後查閱。
構造函數中将處理時間方法傳進來,将_lastTime指派為目前時間,将目前slot Index設定為0,Slot指針
賦為空,slot數目賦為0。slot可以了解為槽。
TimersManager::TimersManager(ProcessTimerEvent processTimerEvent) {
_processTimerEvent = processTimerEvent;
_lastTime = time(NULL);
_currentSlotIndex = 0;
_pSlots = NULL;
_slotsCount = 0;
}
析構時釋放掉slot,無需多解釋。
TimersManager::~TimersManager() {
if (_pSlots != NULL)
delete[] _pSlots;
}
移除處理事件方法,将id為eventTimerId的事件處理方法從所有的slot中移除掉
void TimersManager::RemoveTimer(uint32_t eventTimerId) {
for (uint32_t i = 0; i < _slotsCount; i++) {
if (MAP_HAS1(_pSlots[i].timers, eventTimerId)) {
_pSlots[i].timers.erase(eventTimerId);
}
}
}
void TimersManager::AddTimer(TimerEvent& timerEvent) {
UpdatePeriods(timerEvent.period);
uint32_t min = 999999999;
uint32_t startIndex = 0;
for (uint32_t i = 0; i < _slotsCount; i++) {
if (min > _pSlots[i].timers.size()) {
startIndex = i;
min = _pSlots[i].timers.size();
}
}
while (!MAP_HAS1(_pSlots[startIndex % _slotsCount].timers, timerEvent.id)) {
_pSlots[startIndex % _slotsCount].timers[timerEvent.id] = timerEvent;
startIndex += timerEvent.period;
}
}
void TimersManager::TimeElapsed(uint64_t currentTime) {
int64_t delta = currentTime - _lastTime;
_lastTime = currentTime;
if (delta <= 0 || _slotsCount == 0)
return;
for (int32_t i = 0; i < delta; i++) {
//每間隔period個槽裡都會存放TimeEvent,比如某個TimeEvent的period為100,delta為9,最多
TimeEvent對應的事件隻執行一次。
FOR_MAP(_pSlots[_currentSlotIndex % _slotsCount].timers, uint32_t, TimerEvent, j) {
_processTimerEvent(MAP_VAL(j));
}
_currentSlotIndex++;
}
}
void TimersManager::UpdatePeriods(uint32_t period) {
//如果 TimeEvent周期已經存在直接傳回
if (MAP_HAS1(_periodsMap, period))
return;
_periodsMap[period] = period;
//将TimeEvent 周期放在period vector中
ADD_VECTOR_END(_periodsVector, period);
//計算所有period的最小公倍數,比如之前period為4,新period為3,總Slot Count 為4 * 3 = 12
uint32_t newSlotsCount = LCM(_periodsVector, 0);
if (newSlotsCount == 0)
newSlotsCount = period;
if (newSlotsCount == _slotsCount)
return;
Slot *pNewSlots = new Slot[newSlotsCount];
if (_slotsCount > 0) {
//将Slot Count增加到30,本質上試講之前的slot複制5份
for (uint32_t i = 0; i < newSlotsCount; i++) {
pNewSlots[i] = _pSlots[i % _slotsCount];
}
delete[] _pSlots;
}
_pSlots = pNewSlots;
_slotsCount = newSlotsCount;
}
GCD最大公約數,LCM 求最大公倍數
uint32_t TimersManager::GCD(uint32_t a, uint32_t b) {
while (b != 0) {
uint32_t t = b;
b = a % b;
a = t;
}
return a;
}
uint32_t TimersManager::LCM(uint32_t a, uint32_t b) {
if (a == 0 || b == 0)
return 0;
uint32_t result = a * b / GCD(a, b);
FINEST("a: %u; b: %u; r: %u", a, b, result);
return result;
}
uint32_t TimersManager::GCD(vector<uint32_t> numbers, uint32_t startIndex) {
if (numbers.size() <= 1)
return 0;
if (numbers.size() <= startIndex)
return 0;
if (numbers.size() - startIndex > 2) {
return GCD(numbers[startIndex], GCD(numbers, startIndex + 1));
} else {
return GCD(numbers[startIndex], numbers[startIndex + 1]);
}
}
uint32_t TimersManager::LCM(vector<uint32_t> numbers, uint32_t startIndex) {
if (numbers.size() <= 1)
return 0;
if (numbers.size() <= startIndex)
return 0;
if (numbers.size() - startIndex > 2) {
return LCM(numbers[startIndex], LCM(numbers, startIndex + 1));
} else {
return LCM(numbers[startIndex], numbers[startIndex + 1]);
}
}
/*
*
* TimersManager tm(NULL);
TimerEvent t1 = {2, 1, NULL};
TimerEvent t2 = {3, 2, NULL};
TimerEvent t3 = {3, 3, NULL};
TimerEvent t4 = {4, 4, NULL};
TimerEvent t5 = {3, 5, NULL};
TimerEvent t6 = {2, 6, NULL};
TimerEvent t7 = {4, 7, NULL};
tm.AddTimer(t1);
tm.AddTimer(t2);
tm.AddTimer(t3);
tm.AddTimer(t4);
tm.AddTimer(t5);
tm.AddTimer(t6);
tm.AddTimer(t7);
*
* */
如下圖所示:第一個TimeEvent的周期為4,是以slot數目為4,也就是圖1中的前4個slot。添加第二個TimeEvent的周期為3,UpdatePeriods
方法将前4個slot複制3份得到12個slot,這樣TimeEvent1被放到1,5,9個slot中。接下來AddTimer方法會将TimeEvent2添加到slot中:第一
個添加的slot是slot_2,因為這個slot的TimeEvent總數為0,接下來添加的solt分别是slot_5,slot_8,slot_11,如果繼續添加下去是(11 + 3)%
3 = 2,但slot_2已經添加過,各槽添加TimeEvent2結束。作者将slot總槽書設計為所有的period最小公倍數,在這裡發送作用了。
圖1