Skip to content

Commit 55ca0b6

Browse files
Bug, 解决Wakeup时机滞后,可能导致Wait失败
1 parent 5104fc5 commit 55ca0b6

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

include/YY/Base/Threading/TaskRunner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace YY
6767

6868
void __YYAPI operator()()
6969
{
70-
Wakeup(RunTask());
70+
RunTask();
7171
}
7272

7373
/// <summary>

src/YY/Base/Threading/ParallelTaskRunnerImpl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ namespace YY
225225

226226
if (TaskRunnerFlags.bStopWakeup)
227227
{
228+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
228229
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
229230
}
230231

src/YY/Base/Threading/SequencedTaskRunnerImpl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace YY
6868
{
6969
if (_fFlags & StopWakeupRaw)
7070
{
71+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
7172
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
7273
}
7374
else if (_fFlags & (1 << LockedQueuePushBitIndex))

src/YY/Base/Threading/TaskRunner.cpp

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ namespace YY
5252
if (IsCanceled())
5353
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
5454

55+
HRESULT _hr = S_OK;
5556
try
5657
{
5758
pfnTaskCallback();
58-
return S_OK;
5959
}
6060
catch (const YY::Base::OperationCanceledException& _Exception)
6161
{
62-
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
62+
_hr = YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
6363
}
64+
65+
Wakeup(_hr);
66+
return _hr;
6467
}
6568

6669
HRESULT __YYAPI Timer::RunTask()
@@ -70,20 +73,25 @@ namespace YY
7073

7174
if (uInterval.GetTotalMilliseconds() <= 0)
7275
{
76+
return TaskEntry::RunTask();
77+
}
78+
else
79+
{
80+
HRESULT _hr = S_OK;
81+
bool _bRet = false;
82+
auto _uExpire = TickCount::GetNow() + uInterval;
7383
try
7484
{
75-
pfnTaskCallback();
76-
return S_OK;
85+
_bRet = pfnTimerCallback();
7786
}
7887
catch (const YY::Base::OperationCanceledException& _Exception)
7988
{
80-
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
89+
_hr = YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
8190
}
82-
}
83-
else
84-
{
85-
auto _uExpire = TickCount::GetNow() + uInterval;
86-
if (pfnTimerCallback())
91+
92+
Wakeup(_hr);
93+
94+
if (_bRet)
8795
{
8896
auto _pOwnerTaskRunner = pOwnerTaskRunnerWeak.Get();
8997
// 任务被取消?
@@ -93,7 +101,8 @@ namespace YY
93101
uExpire = _uExpire;
94102
return _pOwnerTaskRunner->SetTimerInternal(this);
95103
}
96-
return S_OK;
104+
105+
return _hr;
97106
}
98107
}
99108

@@ -338,35 +347,30 @@ namespace YY
338347
HRESULT __YYAPI TaskRunner::SetTimerInternal(RefPtr<Timer> _pTask)
339348
{
340349
_pTask->pOwnerTaskRunnerWeak = this;
350+
_pTask->hr = E_PENDING;
341351

342352
if (_pTask->IsCanceled())
343353
{
354+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
344355
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
345356
}
346-
347-
if (_pTask->uExpire.GetTicks() == 0)
348-
return PostTaskInternal(std::move(_pTask));
349-
350-
// 现在的时间已经比过期时间大,那么立即触发任务,降低延迟
351-
auto _uCurrent = TickCount::GetNow();
352-
if (_pTask->uExpire <= _uCurrent)
357+
else if (_pTask->uExpire.GetTicks() == 0 || (_pTask->uExpire - YY::TickCount::GetNow()).GetTotalMilliseconds() <= 0)
353358
{
354-
_pTask->uExpire = _uCurrent;
355359
return PostTaskInternal(std::move(_pTask));
356360
}
357-
else
358-
{
359-
TaskRunnerDispatch::Get()->SetTimerInternal(std::move(_pTask));
360-
return S_OK;
361-
}
361+
362+
TaskRunnerDispatch::Get()->SetTimerInternal(std::move(_pTask));
363+
return S_OK;
362364
}
363365

364366
HRESULT __YYAPI TaskRunner::SetWaitInternal(RefPtr<Wait> _pTask)
365367
{
366368
_pTask->pOwnerTaskRunnerWeak = this;
369+
_pTask->hr = E_PENDING;
367370

368371
if (_pTask->IsCanceled())
369372
{
373+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
370374
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
371375
}
372376

@@ -493,21 +497,27 @@ namespace YY
493497
if (IsCanceled())
494498
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
495499

500+
HRESULT _hr = S_OK;
501+
bool _bRet = false;
496502
try
497503
{
498-
if (pfnWaitTaskCallback(uWaitResult))
499-
{
500-
if (auto _pOwnerTaskRunner = pOwnerTaskRunnerWeak.Get())
501-
{
502-
_pOwnerTaskRunner->SetWaitInternal(this);
503-
}
504-
}
505-
return S_OK;
504+
_bRet = pfnWaitTaskCallback(uWaitResult);
506505
}
507506
catch (const YY::Base::OperationCanceledException& _Exception)
508507
{
509-
return HRESULT_From_LSTATUS(ERROR_CANCELLED);
508+
_hr = HRESULT_From_LSTATUS(ERROR_CANCELLED);
510509
}
510+
511+
Wakeup(_hr);
512+
if (_bRet)
513+
{
514+
if (auto _pOwnerTaskRunner = pOwnerTaskRunnerWeak.Get())
515+
{
516+
_pOwnerTaskRunner->SetWaitInternal(this);
517+
}
518+
}
519+
520+
return _hr;
511521
}
512522

513523
bool __YYAPI Wait::Cancel()

src/YY/Base/Threading/ThreadTaskRunnerImpl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ namespace YY
265265

266266
HRESULT __YYAPI ThreadTaskRunnerImpl::PostTaskInternal(RefPtr<TaskEntry> _pTask)
267267
{
268+
_pTask->hr = E_PENDING;
268269
if (bStopWakeup)
269270
{
271+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
270272
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
271273
}
272274

@@ -295,11 +297,17 @@ namespace YY
295297
HRESULT __YYAPI ThreadTaskRunnerImpl::SetTimerInternal(RefPtr<Timer> _pTask)
296298
{
297299
_pTask->pOwnerTaskRunnerWeak = this;
300+
_pTask->hr = E_PENDING;
298301

299302
if (_pTask->IsCanceled())
300303
{
304+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
301305
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
302306
}
307+
else if (_pTask->uExpire.GetTicks() == 0 || (_pTask->uExpire - YY::TickCount::GetNow()).GetTotalMilliseconds() <= 0)
308+
{
309+
return PostTaskInternal(RefPtr<TaskEntry>(std::move(_pTask)));
310+
}
303311

304312
if (uThreadId != GetCurrentThreadId())
305313
{
@@ -319,8 +327,12 @@ namespace YY
319327
if (_pTask == nullptr || _pTask->hHandle == NULL)
320328
return E_INVALIDARG;
321329

330+
_pTask->hr = E_PENDING;
322331
if (_pTask->IsCanceled())
332+
{
333+
_pTask->Wakeup(YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED));
323334
return YY::Base::HRESULT_From_LSTATUS(ERROR_CANCELLED);
335+
}
324336

325337
if (uThreadId != GetCurrentThreadId())
326338
{

0 commit comments

Comments
 (0)