Skip to content

Commit

Permalink
删除checkCancel -- 事件驱动残留
Browse files Browse the repository at this point in the history
同步AlwaysRunning阻断内联
  • Loading branch information
hl845740757 committed Sep 19, 2024
1 parent 25eb9f1 commit 6c14149
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
3 changes: 1 addition & 2 deletions Wjybxx.BTree.Core/src/Branch/Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected override int Execute() {
if (children.Count == 0) {
return TaskStatus.RUNNING;
}
int reentryId = ReentryId;
for (int i = 0; i < children.Count; i++) {
Task<T> child = children[i];
ParallelChildHelper<T> childHelper = GetChildHelper(child);
Expand Down Expand Up @@ -96,7 +95,7 @@ protected override int Execute() {
return result;
}
}
if (CheckCancel(reentryId)) {
if (cancelToken.IsCancelRequested) { // 收到取消信号
return TaskStatus.CANCELLED;
}
}
Expand Down
3 changes: 1 addition & 2 deletions Wjybxx.BTree.Core/src/Branch/SimpleParallel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ protected override int Enter() {

protected override int Execute() {
List<Task<T>> children = this.children;
int reentryId = ReentryId;
for (int idx = 0; idx < children.Count; idx++) {
Task<T> child = children[idx];
ParallelChildHelper<T> childHelper = GetChildHelper(child);
Expand All @@ -61,7 +60,7 @@ protected override int Execute() {
return child.Status;
}
}
if (CheckCancel(reentryId)) { // 得出结果或取消
if (cancelToken.IsCancelRequested) { // 收到取消信号
return TaskStatus.CANCELLED;
}
}
Expand Down
6 changes: 6 additions & 0 deletions Wjybxx.BTree.Core/src/Decorator/AlwaysRunning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public AlwaysRunning(Task<T> child) : base(child) {
protected override void BeforeEnter() {
base.BeforeEnter();
started = false;
if (child == null) {
IsBreakInline = true;
}
}

protected override int Execute() {
Expand All @@ -58,6 +61,9 @@ protected override int Execute() {
started = true;
Template_StartChild(child, true, ref inlineHelper);
}
if (child.IsCompleted) {
IsBreakInline = true;
}
// 需要响应取消
return child.IsCancelled ? TaskStatus.CANCELLED : TaskStatus.RUNNING;
}
Expand Down
31 changes: 6 additions & 25 deletions Wjybxx.BTree.Core/src/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,24 +572,6 @@ public void RegisterCancelListener() {
ctl |= MASK_REGISTERED_LISTENER;
}

/// <summary>
/// 检查取消
/// @see #isExited(int)
/// </summary>
/// <param name="rid">重入id;方法保存的局部变量</param>
/// <returns>任务是否已进入完成状态;如果返回true,调用者应立即退出</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CheckCancel(int rid) {
if (rid != this.reentryId) { // exit
return true;
}
if (cancelToken.IsCancelRequested) { // 这里是手动检查
SetCompleted(TaskStatus.CANCELLED);
return true;
}
return false;
}

/// <summary>
/// 获取重入id
/// 1.重入id用于解决事件(或外部逻辑)可能使当前Task进入完成状态的问题。
Expand All @@ -605,8 +587,7 @@ public int ReentryId {
/// 重入id对应的任务是否已退出,即:是否已执行<see cref="Exit"/>方法。
/// 1.如果已退出,当前逻辑应该立即退出。
/// 2.通常在执行外部代码后都应该检测,eg:运行子节点,派发事件,执行用户钩子...
/// 3.通常循环体中的代码应该调用<see cref="CheckCancel"/>
/// 4.也可以用于检测是否已重新启动
/// 3.也可以用于检测是否已重新启动
/// </summary>
/// <param name="rid">重入id;方法保存的局部变量</param>
/// <returns>重入id对应的任务是否已退出</returns>
Expand Down Expand Up @@ -657,7 +638,7 @@ public bool IsSlowStart {

/// <summary>
/// 告知模板方法是否在<see cref="Enter"/>前自动调用<see cref="ResetChildrenForRestart"/>
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认不分开执行
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认false
/// 2.要覆盖默认值应当在<see cref="BeforeEnter"/>方法中调用
/// 3.部分任务可能在调用<see cref="ResetForRestart()"/>之前不会再次运行,因此需要该特性
/// </summary>
Expand All @@ -669,7 +650,7 @@ public bool IsAutoResetChildren {

/// <summary>
/// 告知模板方法是否手动检测取消
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认不禁用
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认false
/// 2.是否检测取消信号是一个动态的属性,可随时更改 -- 因此不要轻易缓存。
/// </summary>
public bool IsManualCheckCancel {
Expand Down Expand Up @@ -719,18 +700,18 @@ public bool IsBlackboardPerChild {

/// <summary>
/// 当task作为guard节点时,是否取反(减少栈深度) -- 避免套用<see cref="Decorator.Inverter{T}"/>
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认不分开执行
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认false
/// 2.要覆盖默认值应当在<see cref="BeforeEnter"/>方法中调用
/// </summary>
public bool IsInvertedGuard {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (ctl & MASK_INVERTED_GUARD) != 0;
set => SetCtlBit(MASK_INVERTED_GUARD, value);
}

/// <summary>
/// 当Task可以被内联时是否打破内联
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认不分开执行
/// 1.默认值由<see cref="Flags"/>中的信息指定,默认false
/// 2.要覆盖默认值应当在<see cref="BeforeEnter"/>方法中调用
/// 3.它的作用是避免被内联子节点进入完成状态时产生【过长的恢复路径】
/// </summary>
Expand Down

0 comments on commit 6c14149

Please sign in to comment.