加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TimeWheelArray.cpp 3.28 KB
一键复制 编辑 原始数据 按行查看 历史
“hyk” 提交于 2021-07-22 01:11 . finish timewheel run
#include "TimeWheelArray.h"
TimeWheelArray::TimeWheelArray(uint32_t slot_num,uint32_t step_ms,const std::string& name )
:m_slot_num(slot_num),
m_step_ms(step_ms),
m_name(name),
m_cur_index(-1),
mp_smaller_unit_array(nullptr),
mp_larger_unit_array(nullptr),
m_slots(m_slot_num)
{
}
TimeWheelArray::~TimeWheelArray()
{
}
void TimeWheelArray::addTimeNode(std::shared_ptr<TimeWheelNode> &timenode)
{
uint64_t smaller_unit_cur_time=0;
if(mp_smaller_unit_array!=nullptr)
{
smaller_unit_cur_time=mp_smaller_unit_array->getTimerRunTime();
}
//diff_time only consider this unit time so shoule delete smaller influence
int diff_time=timenode->getTimestamp()-(getNowTimestamp()-smaller_unit_cur_time);
//if difftime is later than step, so this timenode should in this unit slots
if(diff_time>=m_step_ms)
{
uint32_t index=(m_cur_index+diff_time/m_step_ms)%m_slot_num;
m_slots[index].push_back(timenode);
return;
}
//if diff_time is in cur_slot,should consider smallerunit to make timernode preciser
if(mp_smaller_unit_array!=nullptr)
{
mp_smaller_unit_array->addTimeNode(timenode);
return;
}
m_slots[m_cur_index].push_back(timenode);
}
void TimeWheelArray::deleteTimeNode(std::shared_ptr<TimeWheelNode> &timenode)
{
uint64_t smaller_unit_cur_time=0;
if(mp_smaller_unit_array!=nullptr)
{
smaller_unit_cur_time=mp_smaller_unit_array->getTimerRunTime();
}
int diff_time=timenode->getTimestamp()-(getNowTimestamp()-smaller_unit_cur_time);
if(diff_time>=m_step_ms)
{
uint32_t index=(m_cur_index+diff_time/m_step_ms)%m_slot_num;
for(std::list<std::shared_ptr<TimeWheelNode>>::iterator iter=m_slots[index].begin();iter!=m_slots[index].end();iter++)
{
if(*iter==timenode)
{
m_slots[index].erase(iter);
break;
}
}
return;
}
if(mp_smaller_unit_array!=nullptr)
{
mp_smaller_unit_array->deleteTimeNode(timenode);
return;
}
for(std::list<std::shared_ptr<TimeWheelNode>>::iterator iter=m_slots[m_cur_index].begin();iter!=m_slots[m_cur_index].end();iter++)
{
if (*iter == timenode)
{
m_slots[m_cur_index].erase(iter);
break;
}
}
}
void TimeWheelArray::tick()
{
m_cur_index++;
if(m_cur_index<m_slot_num) return;
//if m_cur_index is at tail of vector,it should move to the head
//and mp_larger_unit_array should move one step
m_cur_index%=m_slot_num;
if(mp_larger_unit_array!=nullptr)
{
mp_larger_unit_array->tick();
//get larger missions and put into this solts
std::list<std::shared_ptr<TimeWheelNode>> templist=mp_larger_unit_array->getDegradationTimeNodes();
for(auto &node:templist)
{
addTimeNode(node);
}
}
}
std::list<std::shared_ptr<TimeWheelNode>> TimeWheelArray::getDegradationTimeNodes()
{
//clear m_slots[m_cur_index] and return the value
return std::move(m_slots[m_cur_index]);
}
uint64_t TimeWheelArray::getTimerRunTime()
{
uint64_t time=m_cur_index*m_step_ms;
if(mp_smaller_unit_array!=nullptr)
{
time+=mp_smaller_unit_array->getTimerRunTime();
}
return time;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化