前段时间写了篇blog描述了时间轮算法(http://blog.sina.com.cn/s/blog_48d4cf2d0100pq9o.html),昨天终于动手用c++将其实现了,并通过了单元测试。我没有看Linux内核的实现代码,只是看了资料凭自己的理解来实现,在效率方面也没有尽量优化。所以下面的代码用来帮助理解算法可以,拿来实际用就不一定靠谱了。
另外我没有实现删除函数,因为懒得自己实现链表嘿嘿。参考:http://blog.csdn.net/lanmanck/archive/2009/09/07/4528759.aspx。删除的做法很简单,setTimer时直接返回链表结点,删除的时候干掉自己即可。
===========================timing_wheel.h================================
#pragma once
#include <list>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
namespace global
{
typedef unsigned int uint;
class TimeoutHandler
{
public:
virtual ~TimeoutHandler(){}
virtual void onTimeout() = 0;
};
class TimingWheel
{
public:
TimingWheel();
~TimingWheel();
void setTimer(uint inteval, boost::shared_ptr<TimeoutHandler> handler);
void step();
private:
typedef boost::shared_ptr<TimeoutHandler> Handler;
struct ListNode
{
uint inteval;
Handler handler;
ListNode(int inteval_, Handler handler_)
: inteval_r(inteval_)
, handler(handler_)
{
}
};
enum {BUCKET_CNT = 5};
uint newest[BUCKET_CNT];
std::list<ListNode>* buckets[BUCKET_CNT];
boost::recursive_mutex mtx;
};
}
===========================timing_wheel.cpp================================
#include "timing_wheel.h"
#include "xassert.h"
#include <stdexcept>
#include <vector>
namespace global
{
static const uint ELEMENT_CNT_PER_BUCKET[] = {256, 64, 64, 64, 64};
static const uint RIGHT_SHIFT_PER_BUCKET[] = {8, 6, 6, 6, 6};
static const uint BASE_PER_BUCKET[] = {1, 256, 256*64, 256*64*64, 256*64*64*64};
TimingWheel::TimingWheel()
{
for (int bucket_no = 0; bucket_no < BUCKET_CNT; bucket_no++)
{
newest[bucket_no] = 0;
buckets[bucket_no] = new std::list<ListNode>[ELEMENT_CNT_PER_BUCKET[bucket_no]];
}
}
TimingWheel::~TimingWheel()
{
for (int bucket_no = 0; bucket_no < BUCKET_CNT; bucket_no++)
{
delete[] buckets[bucket_no];
}
}
void TimingWheel::setTimer(uint inteval, boost::shared_ptr<TimeoutHandler> handler)
{
boost::recursive_mutex::scoped_lock lock(mtx);
XASSERT(inteval > 0);
uint bucket_no = 0;
uint offset = inteval;
uint left = inteval;
while (offset >= ELEMENT_CNT_PER_BUCKET[bucket_no])
{
offset >>= RIGHT_SHIFT_PER_BUCKET[bucket_no];
left -= BASE_PER_BUCKET[bucket_no]*(ELEMENT_CNT_PER_BUCKET[bucket_no] - newest[bucket_no] - (bucket_no == 0 ? 0 : 1));
++ bucket_no;
}
XASSERT(offset >= 1);
XASSERT(inteval >=
BASE_PER_BUCKET[bucket_no]*offset);
left -= BASE_PER_BUCKET[bucket_no] * (offset - 1);
uint pos = (newest[bucket_no] + offset) % ELEMENT_CNT_PER_BUCKET[bucket_no];
buckets[bucket_no][pos].push_back(ListNode(left, handler));
}
void TimingWheel::step()
{
std::vector<Handler> handlers;
{
boost::recursive_mutex::scoped_lock lock(mtx);
for (int bucket_no = 0; bucket_no < BUCKET_CNT; bucket_no++)
{
newest[bucket_no] = (newest[bucket_no] + 1) % ELEMENT_CNT_PER_BUCKET[bucket_no];
std::list<ListNode>& cur_list(buckets[bucket_no][newest[bucket_no]]);
while (!cur_list.empty())
{
if (bucket_no == 0 || cur_list.front().inteval == 0)
{
handlers.push_back(cur_list.front().handler);
}
else
{
setTimer(cur_list.front().inteval, cur_list.front().handler);
}
cur_list.pop_front();
}
if (newest[bucket_no] != 0)
{
break;
}
}
}
for (std::size_t i = 0; i < handlers.size(); i++)
{
handlers[i]->onTimeout();
}
}
}