Skip to content

Commit d10a06a

Browse files
Fea, 添加WhenAny、WhenAll协程
1 parent 7684b7f commit d10a06a

File tree

5 files changed

+605
-96
lines changed

5 files changed

+605
-96
lines changed

include/YY/Base/IO/File.h

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace YY
6262
private:
6363
CoroutineReadWriteTask(bool)
6464
{
65-
hCoroutineHandle = -1;
65+
oCoroutineInfo.hCoroutineHandle = -1;
6666
lStatus = ERROR_OUTOFMEMORY;
6767
}
6868

@@ -71,13 +71,7 @@ namespace YY
7171

7272
HRESULT __YYAPI RunTask() override
7373
{
74-
auto _hHandle = (void*)YY::Base::Sync::Exchange(&hCoroutineHandle, /*hReadyHandle*/ (intptr_t)-1);
75-
if (_hHandle)
76-
{
77-
std::coroutine_handle<>::from_address(_hHandle).resume();
78-
}
79-
80-
return S_OK;
74+
return Resume();
8175
}
8276

8377
uint32_t __YYAPI AddRef() noexcept override
@@ -90,7 +84,7 @@ namespace YY
9084
return IoTaskEntry::Release();
9185
}
9286

93-
uint32_t __YYAPI Resume() noexcept override
87+
uint32_t __YYAPI GetResult() noexcept override
9488
{
9589
SetLastError(lStatus);
9690
return uint32_t(InternalHigh);
@@ -263,7 +257,7 @@ namespace YY
263257
if (_lStatus != ERROR_SUCCESS)
264258
{
265259
// 失败时,异步任务不会在触发了。也算是一种任务完成。
266-
_pAsyncTaskEntry->hCoroutineHandle = -1;
260+
_pAsyncTaskEntry->Resume();
267261
}
268262
}
269263
else
@@ -297,7 +291,7 @@ namespace YY
297291
if (_lStatus != ERROR_SUCCESS)
298292
{
299293
// 失败时,异步任务不会在触发了。也算是一种任务完成。
300-
_pAsyncTaskEntry->hCoroutineHandle = -1;
294+
_pAsyncTaskEntry->Resume();
301295
}
302296
}
303297
else
@@ -513,7 +507,7 @@ namespace YY
513507
private:
514508
CoroutineConnectTask(bool)
515509
{
516-
hCoroutineHandle = -1;
510+
oCoroutineInfo.hCoroutineHandle = -1;
517511
lStatus = ERROR_OUTOFMEMORY;
518512
}
519513

@@ -522,13 +516,7 @@ namespace YY
522516

523517
HRESULT __YYAPI RunTask() override
524518
{
525-
auto _hHandle = (void*)YY::Base::Sync::Exchange(&hCoroutineHandle, /*hReadyHandle*/ (intptr_t)-1);
526-
if (_hHandle)
527-
{
528-
std::coroutine_handle<>::from_address(_hHandle).resume();
529-
}
530-
531-
return S_OK;
519+
return Resume();
532520
}
533521

534522
uint32_t __YYAPI AddRef() noexcept override
@@ -541,7 +529,7 @@ namespace YY
541529
return IoTaskEntry::Release();
542530
}
543531

544-
LSTATUS __YYAPI Resume() noexcept override
532+
LSTATUS __YYAPI GetResult() noexcept override
545533
{
546534
return lStatus;
547535
}
@@ -559,7 +547,7 @@ namespace YY
559547
auto _lStatus = AsyncConnectIntetnal(_pConnectTask);
560548
if (_lStatus != ERROR_SUCCESS)
561549
{
562-
_pConnectTask->hCoroutineHandle = -1;
550+
_pConnectTask->Resume();
563551
}
564552
}
565553
else

include/YY/Base/Sync/Interlocked.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,29 @@ namespace YY
408408
return (Type1*)CompareExchange(reinterpret_cast<intptr_t*>(_ppDestination), reinterpret_cast<intptr_t>(static_cast<Type1*>(_pExchange)), reinterpret_cast<intptr_t>(static_cast<Type1*>(_pComparand)));
409409
}
410410

411+
/// <summary>
412+
/// 直接交换2个指针的值
413+
/// </summary>
414+
/// <typeparam name="Type"></typeparam>
415+
/// <param name="_pDestinationPoints"></param>
416+
/// <param name="_pExchangePoints"></param>
417+
/// <param name="_pComparandResult"></param>
418+
/// <returns></returns>
419+
inline bool __YYAPI CompareExchangeTwoPoints(_Inout_updates_(2) volatile intptr_t* _pDestinationPoints, _In_reads_(2) const intptr_t* _pExchangePoints, _Inout_updates_(2) intptr_t* _pComparandResult)
420+
{
421+
#if defined(_X86_) || defined(_ARM_)
422+
static_assert(sizeof(intptr_t) * 2 == sizeof(int64_t), "");
423+
auto _iComparand = *reinterpret_cast<int64_t*>(_pComparandResult);
424+
425+
auto _iPreValue = CompareExchange(reinterpret_cast<volatile int64_t*>(_pDestinationPoints), *reinterpret_cast<const int64_t*>(_pExchangePoints), _iComparand);
426+
*reinterpret_cast<int64_t*>(_pComparandResult) = _iPreValue;
427+
return _iPreValue == _iComparand;
428+
#else
429+
static_assert(sizeof(intptr_t) == sizeof(LONG64), "");
430+
return InterlockedCompareExchange128(reinterpret_cast<volatile LONG64*>(_pDestinationPoints), reinterpret_cast<const LONG64*>(_pExchangePoints)[1], reinterpret_cast<const LONG64*>(_pExchangePoints)[0], reinterpret_cast<LONG64*>(_pComparandResult));
431+
#endif
432+
}
433+
411434
inline int32_t __YYAPI Exchange(volatile int32_t* _pDestination, int32_t _iExchange)
412435
{
413436
#ifdef _MSC_VER

0 commit comments

Comments
 (0)