daily update on 20250122 by nitload youmu, architect rearrange
This commit is contained in:
@@ -42,25 +42,36 @@ using System.Threading;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.BluePrint
|
||||
namespace StgSharp.Blueprint
|
||||
{
|
||||
public static class BlueprintRunner
|
||||
{
|
||||
|
||||
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource(
|
||||
);
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>(
|
||||
);
|
||||
private static NodeRunner[] allAvailableTask = CreateBlueprintRunnerArray();
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource();
|
||||
private static ConcurrentStack<BlueprintSchedueler> blueprintInWaiting
|
||||
= new ConcurrentStack<BlueprintSchedueler>();
|
||||
private static int _callingThread;
|
||||
private static SemaphoreSlim bpLock = new SemaphoreSlim( 0, 1 );
|
||||
|
||||
private static NodeRunner[] CreateBlueprintRunnerArray()
|
||||
{
|
||||
int count = Environment.ProcessorCount;
|
||||
return new NodeRunner[ count < 2 ? count : count - 2];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Only the fallowing occasions of calling are valid:
|
||||
/// 1. no bp is running or the only bp is quitting.
|
||||
/// 2. calling from the main thread and the current running bp.
|
||||
/// </summary>
|
||||
internal static bool IsValidCalling
|
||||
{
|
||||
get
|
||||
{
|
||||
if( ( NodeRunner.CurrentBlueprint == null ) || //no bp running at all
|
||||
( ( blueprintInWaiting.Count == 0 ) && ( NodeRunner.CurrentBlueprint.IsRunning == false ) ) ) // a bp is quiting
|
||||
( ( blueprintInWaiting.IsEmpty ) && ( NodeRunner.CurrentBlueprint.IsRunning == false ) ) )
|
||||
// a bp is quiting
|
||||
{
|
||||
//in case no bp is running
|
||||
return true;
|
||||
@@ -68,9 +79,7 @@ namespace StgSharp.BluePrint
|
||||
int? id = Task.CurrentId;
|
||||
if( id is null ) {
|
||||
// in case calling from main thread and current running bp
|
||||
//Console.WriteLine($"\t{_callingThread}");
|
||||
//Console.WriteLine($"\t{Thread.CurrentThread.ManagedThreadId}");
|
||||
return _callingThread == Thread.CurrentThread.ManagedThreadId;
|
||||
return _callingThread == Environment.CurrentManagedThreadId;
|
||||
}
|
||||
foreach( NodeRunner item in allAvailableTask ) {
|
||||
if( item.Id == id ) {
|
||||
@@ -82,34 +91,27 @@ namespace StgSharp.BluePrint
|
||||
}
|
||||
}
|
||||
|
||||
public static void Run(
|
||||
Blueprint blueprint,
|
||||
Func<bool> terminateInstructor )
|
||||
public static void Run(BlueprintSchedueler blueprint)
|
||||
{
|
||||
//Console.WriteLine();
|
||||
if( blueprint == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if( terminateInstructor == null ) {
|
||||
throw new ArgumentNullException( nameof( terminateInstructor ) );
|
||||
}
|
||||
if( blueprint.IsRunning ) {
|
||||
throw new InvalidOperationException(
|
||||
"Attempt to run a blueprint instance twice." );
|
||||
}
|
||||
blueprint.ShouldClose = terminateInstructor;
|
||||
if( IsValidCalling ) {
|
||||
//Console.WriteLine("valid and run");
|
||||
if( InterruptAndRunNewBp( blueprint ) ) {
|
||||
do { } while (InterruptAndRunNewBp(
|
||||
NodeRunner.CurrentBlueprint ));
|
||||
while (InterruptAndRunNewBp(
|
||||
NodeRunner.CurrentBlueprint )){ };
|
||||
}
|
||||
} else {
|
||||
bpLock.Wait();
|
||||
if( InterruptAndRunNewBp( blueprint ) ) {
|
||||
do { } while (InterruptAndRunNewBp(
|
||||
NodeRunner.CurrentBlueprint ));
|
||||
while (InterruptAndRunNewBp(
|
||||
NodeRunner.CurrentBlueprint)) { }
|
||||
}
|
||||
|
||||
bpLock.Release();
|
||||
@@ -139,13 +141,13 @@ namespace StgSharp.BluePrint
|
||||
foreach( NodeRunner task in allAvailableTask ) {
|
||||
task.Cts.Cancel();
|
||||
}
|
||||
foreach( Blueprint bp in blueprintInWaiting ) {
|
||||
foreach( BlueprintSchedueler bp in blueprintInWaiting ) {
|
||||
bp.Terminate();
|
||||
}
|
||||
blueprintInWaiting.Clear();
|
||||
}
|
||||
|
||||
internal static bool InterruptAndRunNewBp( Blueprint newBp )
|
||||
internal static bool InterruptAndRunNewBp( BlueprintSchedueler newBp )
|
||||
{
|
||||
Debug.Assert( newBp != null );
|
||||
if( ( newBp != NodeRunner.CurrentBlueprint ) && ( NodeRunner.CurrentBlueprint != null ) ) {
|
||||
@@ -156,12 +158,11 @@ namespace StgSharp.BluePrint
|
||||
//load new bp to current bp
|
||||
Interlocked.Exchange( ref NodeRunner.CurrentBlueprint!, newBp );
|
||||
Interlocked.Exchange(
|
||||
ref _callingThread, Thread.CurrentThread.ManagedThreadId );
|
||||
ref _callingThread, Environment.CurrentManagedThreadId );
|
||||
|
||||
//run beginning node to init it first.
|
||||
if( !NodeRunner.CurrentBlueprint.IsRunning ) {
|
||||
( NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode )!.Run(
|
||||
);
|
||||
( NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode )!.Run();
|
||||
}
|
||||
|
||||
//refresh all tasks
|
||||
@@ -188,7 +189,7 @@ namespace StgSharp.BluePrint
|
||||
internal class NodeRunner : CancellableTask
|
||||
{
|
||||
|
||||
private static Blueprint _current;
|
||||
private static BlueprintSchedueler _current;
|
||||
|
||||
internal NodeRunner()
|
||||
: base()
|
||||
@@ -196,7 +197,7 @@ namespace StgSharp.BluePrint
|
||||
_startup = InternalRun;
|
||||
}
|
||||
|
||||
internal static ref Blueprint CurrentBlueprint
|
||||
internal static ref BlueprintSchedueler CurrentBlueprint
|
||||
{
|
||||
get => ref _current;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user