时间轮TimeWheel有特别的使用场景,不能废弃
This commit is contained in:
parent
61cc2b71ea
commit
2df5acdc84
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -93,7 +93,7 @@ public:
|
|||
Lock(int& ref);
|
||||
~Lock();
|
||||
|
||||
bool Wait(int us = -1);
|
||||
bool Wait(int ms);
|
||||
};
|
||||
|
||||
extern "C"
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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())
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue