mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(coroutine): 优化协程调度器实现
- 在 CoroutineSlot 中添加 Handle 属性用于标识协程实例 - 使用集合初始化语法替换 HashSet 创建方式 - 简化协程执行状态检查逻辑 - 优化 KillAllByTag 方法中的计数逻辑 - 改进协程清理过程中的句柄验证和等待者唤醒机制
This commit is contained in:
parent
a79f02c987
commit
bf517a4f7f
@ -56,7 +56,8 @@ public sealed class CoroutineScheduler(
|
||||
var slot = new CoroutineSlot
|
||||
{
|
||||
Enumerator = coroutine,
|
||||
State = CoroutineState.Running
|
||||
State = CoroutineState.Running,
|
||||
Handle = handle
|
||||
};
|
||||
|
||||
_slots[slotIndex] = slot;
|
||||
@ -88,7 +89,7 @@ public sealed class CoroutineScheduler(
|
||||
for (var i = 0; i < _nextSlot; i++)
|
||||
{
|
||||
var slot = _slots[i];
|
||||
if (slot == null || slot.State != CoroutineState.Running)
|
||||
if (slot is not { State: CoroutineState.Running })
|
||||
continue;
|
||||
|
||||
try
|
||||
@ -207,7 +208,7 @@ public sealed class CoroutineScheduler(
|
||||
|
||||
if (!_waiting.TryGetValue(target, out var set))
|
||||
{
|
||||
set = new HashSet<CoroutineHandle>();
|
||||
set = [];
|
||||
_waiting[target] = set;
|
||||
}
|
||||
|
||||
@ -223,15 +224,8 @@ public sealed class CoroutineScheduler(
|
||||
{
|
||||
if (!_tagged.TryGetValue(tag, out var handles))
|
||||
return 0;
|
||||
|
||||
var copy = handles.ToArray();
|
||||
var count = 0;
|
||||
|
||||
foreach (var h in copy)
|
||||
if (Kill(h))
|
||||
count++;
|
||||
|
||||
return count;
|
||||
return copy.Count(Kill);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -294,43 +288,28 @@ public sealed class CoroutineScheduler(
|
||||
if (slot == null)
|
||||
return;
|
||||
|
||||
_slots[slotIndex] = null;
|
||||
_activeCount--;
|
||||
|
||||
CoroutineHandle handle = default;
|
||||
foreach (var kv in _metadata)
|
||||
{
|
||||
if (kv.Value.SlotIndex == slotIndex)
|
||||
{
|
||||
handle = kv.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var handle = slot.Handle;
|
||||
if (!handle.IsValid)
|
||||
return;
|
||||
|
||||
_slots[slotIndex] = null;
|
||||
_activeCount--;
|
||||
|
||||
RemoveTag(handle);
|
||||
_metadata.Remove(handle);
|
||||
|
||||
// 唤醒等待者
|
||||
if (_waiting.TryGetValue(handle, out var waiters))
|
||||
if (!_waiting.TryGetValue(handle, out var waiters)) return;
|
||||
foreach (var waiter in waiters)
|
||||
{
|
||||
foreach (var waiter in waiters)
|
||||
{
|
||||
if (_metadata.TryGetValue(waiter, out var meta))
|
||||
{
|
||||
var s = _slots[meta.SlotIndex];
|
||||
if (s != null)
|
||||
{
|
||||
s.State = CoroutineState.Running;
|
||||
meta.State = CoroutineState.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_waiting.Remove(handle);
|
||||
if (!_metadata.TryGetValue(waiter, out var meta)) continue;
|
||||
var s = _slots[meta.SlotIndex];
|
||||
if (s == null) continue;
|
||||
s.State = CoroutineState.Running;
|
||||
meta.State = CoroutineState.Running;
|
||||
}
|
||||
|
||||
_waiting.Remove(handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -21,4 +21,9 @@ internal sealed class CoroutineSlot
|
||||
/// 当前等待的指令,用于控制协程的暂停和恢复
|
||||
/// </summary>
|
||||
public IYieldInstruction? Waiting;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 协程句柄,用于标识和管理协程实例
|
||||
/// </summary>
|
||||
public CoroutineHandle Handle;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user