mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 13:33:28 +08:00
refactor(coroutine): 优化异步操作延续处理逻辑
- 使用while循环替代递归调用避免栈溢出风险 - 改进CAS操作的重试机制提高并发安全性 - 简化空值检查逻辑提升代码可读性 - 优化双重检查锁模式确保线程安全
This commit is contained in:
parent
01fb7c33ca
commit
6cc2bdfeb5
@ -27,6 +27,8 @@ public class AsyncOperation : IYieldInstruction, INotifyCompletion
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="continuation">要执行的延续操作</param>
|
/// <param name="continuation">要执行的延续操作</param>
|
||||||
public void OnCompleted(Action continuation)
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
// 尝试添加延续
|
// 尝试添加延续
|
||||||
var current = _continuation;
|
var current = _continuation;
|
||||||
@ -39,7 +41,7 @@ public class AsyncOperation : IYieldInstruction, INotifyCompletion
|
|||||||
continuation();
|
continuation();
|
||||||
else
|
else
|
||||||
// 重试
|
// 重试
|
||||||
OnCompleted(continuation);
|
continue;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -48,7 +50,10 @@ public class AsyncOperation : IYieldInstruction, INotifyCompletion
|
|||||||
if (_completed)
|
if (_completed)
|
||||||
{
|
{
|
||||||
var cont = Interlocked.Exchange(ref _continuation, null);
|
var cont = Interlocked.Exchange(ref _continuation, null);
|
||||||
if (cont != null) cont();
|
cont?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user