时间轮TimeWheel有特别的使用场景,不能废弃

This commit is contained in:
Stone 2016-06-19 17:33:01 +00:00
parent 61cc2b71ea
commit 2df5acdc84
9 changed files with 19 additions and 22 deletions

View File

@ -223,7 +223,7 @@ int IR::Receive(Buffer& bs, int sTimeout)
// 等待起始条件DMA 开始搬运数据
Stat = WaitRev;
TimeWheel tw(sTimeout);
TimeWheel tw(sTimeout * 1000);
tw.Sleep = 50;
while(!tw.Expired() && DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen);

View File

@ -20,7 +20,7 @@ bool WaitExpect::Wait(int msTimeout)
if(!Result) return true;
/*// 等待收到数据
TimeWheel tw(0, msTimeout - 40);
TimeWheel tw(msTimeout - 40);
// 默认检查间隔200ms如果超时时间大于1000ms则以四分之一为检查间隔
// ESP8266串口任务平均时间为150ms左右为了避免接收指令任务里面发送指令时等不到OK需要加大检查间隔
tw.Sleep = 200;

View File

@ -75,7 +75,7 @@ String Sim900A::Send(cstring str, uint msTimeout)
String bs;
bs.SetLength(bs.Capacity());
TimeWheel tw(0, msTimeout);
TimeWheel tw(msTimeout);
tw.Sleep = 100;
do
{

View File

@ -293,7 +293,7 @@ bool W5500::Open()
// 读硬件版本
byte ver = 0;
TimeWheel tw(1);
TimeWheel tw(1000);
while(!tw.Expired() && !ver) ver = ReadByte(0x0039);
if(!ver)
{
@ -456,7 +456,7 @@ void W5500::Reset()
WriteByte(offsetof(TGeneral, MR), mr.ToByte());
// 必须要等一会,否则初始化会失败
Sys.Delay(600); // 最少500us
TimeWheel tw(0, 10, 0);
TimeWheel tw(10);
while(!ReadByte(0x0039) && !tw.Expired());
}
@ -1126,7 +1126,7 @@ bool HardSocket::OnOpen()
//如果Socket打开失败
bool rs = false;
byte sr = 0;
TimeWheel tw(0, 50);
TimeWheel tw(50);
while(!tw.Expired())
{
sr = ReadStatus();
@ -1331,7 +1331,7 @@ bool TcpClient::OnOpen()
while(ReadConfig());
// 等待3秒
TimeWheel tw(3);
TimeWheel tw(3000);
while(ReadStatus() != SOCK_SYNSENT)
{
if(ReadStatus() == SOCK_ESTABLISHE) return true;

View File

@ -121,14 +121,14 @@ Lock::~Lock()
}
}
bool Lock::Wait(int us)
bool Lock::Wait(int ms)
{
// 可能已经进入成功
if(Success) return true;
int& ref = *_ref;
// 等待超时时间
TimeWheel tw(0, 0, us);
TimeWheel tw(ms);
tw.Sleep = 1;
while(ref > 0)
{

View File

@ -93,7 +93,7 @@ public:
Lock(int& ref);
~Lock();
bool Wait(int us = -1);
bool Wait(int ms);
};
extern "C"

View File

@ -150,16 +150,15 @@ extern "C"
/************************************************ TimeWheel ************************************************/
TimeWheel::TimeWheel(uint seconds, uint ms, uint us)
TimeWheel::TimeWheel(uint ms)
{
Sleep = 0;
Reset(seconds, ms, us);
Sleep = 10;
Reset(ms);
}
void TimeWheel::Reset(uint seconds, uint ms, uint us)
void TimeWheel::Reset(uint ms)
{
Expire = Time.Current() + seconds * 1000 + ms;
Expire2 = Time.CurrentTicks() + us * Time.Ticks;
Expire = Time.Current() + + ms;
}
// 是否已过期
@ -167,7 +166,6 @@ bool TimeWheel::Expired()
{
UInt64 now = Time.Current();
if(now > Expire) return true;
if(now == Expire && Time.CurrentTicks() >= Expire2) return true;
// 睡眠释放CPU
if(Sleep) Sys.Sleep(Sleep);

View File

@ -48,12 +48,11 @@ class TimeWheel
{
public:
uint Expire; // 到期时间,毫秒
ushort Expire2; // 到期时间,滴答
ushort Sleep; // 睡眠时间默认0毫秒
ushort Sleep; // 睡眠时间默认10毫秒
TimeWheel(uint seconds, uint ms = 0, uint us = 0);
TimeWheel(uint ms);
void Reset(uint seconds, uint ms = 0, uint us = 0);
void Reset(uint ms);
// 是否已过期
bool Expired();

View File

@ -392,7 +392,7 @@ IPAddress DNS::Query(const String& domain, int msTimeout)
dns_makequery(0, domain, bs);
Socket->Send(bs);
TimeWheel tw(0, msTimeout);
TimeWheel tw(msTimeout);
tw.Sleep = 100;
while(!tw.Expired())
{