| | |
| | | |
| | | private bool isForceFinish = false; |
| | | |
| | | // 立即播放并接管队列:正在并发运行、current结束后若尚未完成则提升为currentRecordAction |
| | | private RecordAction pendingTakeoverAction = null; |
| | | |
| | | // 复活独占锁:锁住期间只有 owner 可以执行,其他 RecordAction 保留并等待解锁 |
| | | private bool isExclusiveLocked = false; |
| | | // 持有独占锁的 action(允许它在锁住期间继续执行) |
| | | private RecordAction exclusiveLockOwner = null; |
| | | |
| | | /// <summary> |
| | | /// 设置独占锁。<br/> |
| | | /// locked=true 时:暂停 owner 之外的 RecordAction 调度,已有队列和新入队 action 均保留等待。 |
| | | /// locked=false 时解锁,恢复正常调度。 |
| | | /// </summary> |
| | | public void SetExclusiveLock(bool locked, RecordAction owner = null) |
| | | { |
| | | isExclusiveLocked = locked; |
| | | exclusiveLockOwner = locked ? owner : null; |
| | | } |
| | | |
| | | public bool IsExclusiveLocked => isExclusiveLocked; |
| | | |
| | | public bool IsForceFinish |
| | | { |
| | | get { return isForceFinish; } |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 立即开始并发播放 recordAction,同时将其设为「队列接管者」: |
| | | /// 当 currentRecordAction 播放完毕时,若该 action 尚未结束, |
| | | /// 则将其从 immediatelyActionList 提升为新的 currentRecordAction, |
| | | /// 阻止队列继续推进,直到它也完成为止。 |
| | | /// </summary> |
| | | public void ImmediatelyPlayThenBlockQueue(RecordAction recordAction) |
| | | { |
| | | if (recordAction == null) return; |
| | | recordAction.actionOwner = this; |
| | | if (isForceFinish || stepForcefinish) |
| | | { |
| | | recordAction.ForceFinish(); |
| | | return; |
| | | } |
| | | // 立即加入并发列表,开始执行 |
| | | immediatelyActionList.Add(recordAction); |
| | | // 标记为待接管者,current结束后由 Run() 将其提升 |
| | | pendingTakeoverAction = recordAction; |
| | | } |
| | | |
| | | public void InsertRecord(RecordAction recordAction, int position = 0) |
| | | { |
| | | if (recordAction == null) return; |
| | |
| | | removeIndexList.Add(i); |
| | | continue; |
| | | } |
| | | |
| | | // 独占锁期间,非 owner 的 immediate action 只等待,不执行也不移除 |
| | | if (isExclusiveLocked && action != exclusiveLockOwner) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | // 检查是否可以开始执行(WaitingPlay条件检查) |
| | | if (!action.CanStartExecution()) |
| | |
| | | { |
| | | ImmediatelyPlayRun(); |
| | | |
| | | // 独占锁期间,主线 current/queue 暂停,只允许 owner 继续执行 |
| | | if (isExclusiveLocked && (exclusiveLockOwner == null || currentRecordAction != exclusiveLockOwner)) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | // 等待下一个action |
| | | if (isWaitingNextAction) |
| | | { |
| | |
| | | |
| | | if (currentRecordAction == null) |
| | | { |
| | | if (recordActionQueue.Count <= 0) |
| | | if (recordActionQueue.Count <= 0 && pendingTakeoverAction == null) |
| | | { |
| | | return; |
| | | } |
| | |
| | | |
| | | if (currentRecordAction == null) |
| | | { |
| | | // 检查是否有待接管的 action |
| | | if (pendingTakeoverAction != null) |
| | | { |
| | | if (!pendingTakeoverAction.IsFinished()) |
| | | { |
| | | // 提升为 currentRecordAction,阻塞队列推进 |
| | | immediatelyActionList.Remove(pendingTakeoverAction); |
| | | currentRecordAction = pendingTakeoverAction; |
| | | pendingTakeoverAction = null; |
| | | return; |
| | | } |
| | | // 已经完成,直接清除,不阻塞 |
| | | pendingTakeoverAction = null; |
| | | } |
| | | |
| | | if (recordActionQueue.Count > 0) |
| | | { |
| | | currentRecordAction = recordActionQueue.Dequeue(); |
| | |
| | | { |
| | | currentRecordAction?.ForceFinish(); |
| | | currentRecordAction = null; |
| | | pendingTakeoverAction = null; |
| | | isExclusiveLocked = false; |
| | | exclusiveLockOwner = null; |
| | | recordActionQueue.Clear(); |
| | | immediatelyActionList.Clear(); |
| | | } |
| | |
| | | public void ForceFinish() |
| | | { |
| | | isForceFinish = true; |
| | | isExclusiveLocked = false; |
| | | exclusiveLockOwner = null; |
| | | pendingTakeoverAction = null; |
| | | for (int i = immediatelyActionList.Count - 1; i >= 0; i--) |
| | | { |
| | | var action = immediatelyActionList[i]; |
| | |
| | | currentRecordAction.ForceFinish(); |
| | | } |
| | | currentRecordAction = null; |
| | | pendingTakeoverAction = null; |
| | | recordActionQueue.Clear(); |
| | | immediatelyActionList.Clear(); |
| | | isForceFinish = false; |
| | | isExclusiveLocked = false; |
| | | exclusiveLockOwner = null; |
| | | stepForcefinish = false; |
| | | } |
| | | |