daily uodate on 20240626 by nitload youmu[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[p
This commit is contained in:
@@ -12,9 +12,10 @@ namespace StgSharp.Logic
|
||||
{
|
||||
public static class BlueprintRunner
|
||||
{
|
||||
private static int _callingThread;
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>();
|
||||
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
|
||||
private static ReaderWriterLockSlim bpLock = new ReaderWriterLockSlim();
|
||||
private static SemaphoreSlim bpLock = new SemaphoreSlim(0,1);
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource();
|
||||
|
||||
public static void Run(Blueprint blueprint, Func<bool> terminateInstructor)
|
||||
@@ -27,36 +28,48 @@ namespace StgSharp.Logic
|
||||
}
|
||||
if (blueprint.IsRunning)
|
||||
{
|
||||
throw new InvalidOperationException( $"Attempt to run a blueprint twice." );
|
||||
throw new InvalidOperationException("Attempt to run a blueprint instance twice.");
|
||||
}
|
||||
blueprint.ShouldClose = terminateInstructor;
|
||||
if (IsInternalCalling)
|
||||
if (IsValidCalling)
|
||||
{
|
||||
if(InterruptAndRunNewBp(blueprint))
|
||||
//Console.WriteLine("valid and run");
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
do { } while(InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!bpLock.IsWriteLockHeld)
|
||||
{
|
||||
bpLock.EnterWriteLock();
|
||||
}
|
||||
bpLock.Wait();
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
bpLock.ExitWriteLock();
|
||||
bpLock.Release();
|
||||
}
|
||||
Interlocked.Exchange(ref _callingThread, -1);
|
||||
}
|
||||
|
||||
internal static bool IsInternalCalling
|
||||
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
|
||||
{
|
||||
//in case no bp is running
|
||||
return true;
|
||||
}
|
||||
int? id = Task.CurrentId;
|
||||
if (id is null) return false;
|
||||
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;
|
||||
}
|
||||
foreach (var item in allAvailableTask)
|
||||
{
|
||||
if (item.Id == id)
|
||||
{
|
||||
//if calling from any bp running task
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -69,13 +82,18 @@ namespace StgSharp.Logic
|
||||
Debug.Assert(newBp != null);
|
||||
if (newBp != NodeRunner.CurrentBlueprint && NodeRunner.CurrentBlueprint != null)
|
||||
{
|
||||
//if a running bp exists
|
||||
blueprintInWaiting.Push(NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
//load new bp to current bp
|
||||
Interlocked.Exchange(ref NodeRunner.CurrentBlueprint!, newBp);
|
||||
Interlocked.Exchange(ref _callingThread, Thread.CurrentThread.ManagedThreadId);
|
||||
//run beginning node to init it first.
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
(NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode)!.Run();
|
||||
}
|
||||
//refresh all tasks
|
||||
for (int i = 0; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
var item = allAvailableTask[i];
|
||||
|
||||
Reference in New Issue
Block a user