daily update on 20241002 by nitload youmu, intrinsic function rebuild, syntax style arrange
This commit is contained in:
@@ -28,6 +28,9 @@
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
using StgSharp.Threading;
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
@@ -36,9 +39,8 @@ using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using StgSharp.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.BluePrint
|
||||
{
|
||||
@@ -46,33 +48,32 @@ namespace StgSharp.BluePrint
|
||||
{
|
||||
|
||||
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource();
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>();
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource(
|
||||
);
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>(
|
||||
);
|
||||
private static int _callingThread;
|
||||
private static SemaphoreSlim bpLock = new SemaphoreSlim(0, 1);
|
||||
private static SemaphoreSlim bpLock = new SemaphoreSlim( 0, 1 );
|
||||
|
||||
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
|
||||
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)
|
||||
{
|
||||
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 (NodeRunner item in allAvailableTask)
|
||||
{
|
||||
if (item.Id == id)
|
||||
{
|
||||
foreach( NodeRunner item in allAvailableTask ) {
|
||||
if( item.Id == id ) {
|
||||
//if calling from any bp running task
|
||||
return true;
|
||||
}
|
||||
@@ -81,116 +82,105 @@ namespace StgSharp.BluePrint
|
||||
}
|
||||
}
|
||||
|
||||
public static void Run(Blueprint blueprint, Func<bool> terminateInstructor)
|
||||
public static void Run(
|
||||
Blueprint blueprint,
|
||||
Func<bool> terminateInstructor )
|
||||
{
|
||||
//Console.WriteLine();
|
||||
if (blueprint == null)
|
||||
{
|
||||
if( blueprint == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (terminateInstructor == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(terminateInstructor));
|
||||
if( terminateInstructor == null ) {
|
||||
throw new ArgumentNullException( nameof( terminateInstructor ) );
|
||||
}
|
||||
if (blueprint.IsRunning)
|
||||
{
|
||||
throw new InvalidOperationException("Attempt to run a blueprint instance twice.");
|
||||
if( blueprint.IsRunning ) {
|
||||
throw new InvalidOperationException(
|
||||
"Attempt to run a blueprint instance twice." );
|
||||
}
|
||||
blueprint.ShouldClose = terminateInstructor;
|
||||
if (IsValidCalling)
|
||||
{
|
||||
if( IsValidCalling ) {
|
||||
//Console.WriteLine("valid and run");
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
{
|
||||
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
if( InterruptAndRunNewBp( blueprint ) ) {
|
||||
do { } while (InterruptAndRunNewBp(
|
||||
NodeRunner.CurrentBlueprint ));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
bpLock.Wait();
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
{
|
||||
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
if( InterruptAndRunNewBp( blueprint ) ) {
|
||||
do { } while (InterruptAndRunNewBp(
|
||||
NodeRunner.CurrentBlueprint ));
|
||||
}
|
||||
|
||||
bpLock.Release();
|
||||
}
|
||||
Interlocked.Exchange(ref _callingThread, -1);
|
||||
Interlocked.Exchange( ref _callingThread, -1 );
|
||||
}
|
||||
|
||||
|
||||
public static void SetMaxTaskCount(int size)
|
||||
public static void SetMaxTaskCount( int size )
|
||||
{
|
||||
if (size < 1)
|
||||
{
|
||||
if( size < 1 ) {
|
||||
size = 1;
|
||||
}
|
||||
if (allAvailableTask.Length >= size)
|
||||
{
|
||||
for (int i = size; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
if (allAvailableTask[i] != null)
|
||||
{
|
||||
allAvailableTask[i].Cancel();
|
||||
if( allAvailableTask.Length >= size ) {
|
||||
for( int i = size; i < allAvailableTask.Length; i++ ) {
|
||||
if( allAvailableTask[ i ] != null ) {
|
||||
allAvailableTask[ i ].Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
Array.Resize(ref allAvailableTask, size);
|
||||
Array.Resize( ref allAvailableTask, size );
|
||||
}
|
||||
|
||||
public static void Terminate()
|
||||
{
|
||||
cts.Cancel();
|
||||
NodeRunner.CurrentBlueprint.Terminate();
|
||||
foreach (NodeRunner task in allAvailableTask)
|
||||
{
|
||||
foreach( NodeRunner task in allAvailableTask ) {
|
||||
task.Cts.Cancel();
|
||||
}
|
||||
foreach (Blueprint bp in blueprintInWaiting)
|
||||
{
|
||||
foreach( Blueprint bp in blueprintInWaiting ) {
|
||||
bp.Terminate();
|
||||
}
|
||||
blueprintInWaiting.Clear();
|
||||
}
|
||||
|
||||
internal static bool InterruptAndRunNewBp(Blueprint newBp)
|
||||
internal static bool InterruptAndRunNewBp( Blueprint newBp )
|
||||
{
|
||||
Debug.Assert(newBp != null);
|
||||
if ((newBp != NodeRunner.CurrentBlueprint) && (NodeRunner.CurrentBlueprint != null))
|
||||
{
|
||||
Debug.Assert( newBp != null );
|
||||
if( ( newBp != NodeRunner.CurrentBlueprint ) && ( NodeRunner.CurrentBlueprint != null ) ) {
|
||||
//if a running bp exists
|
||||
blueprintInWaiting.Push(NodeRunner.CurrentBlueprint);
|
||||
blueprintInWaiting.Push( NodeRunner.CurrentBlueprint );
|
||||
}
|
||||
|
||||
//load new bp to current bp
|
||||
Interlocked.Exchange(ref NodeRunner.CurrentBlueprint!, newBp);
|
||||
Interlocked.Exchange(ref _callingThread, Thread.CurrentThread.ManagedThreadId);
|
||||
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();
|
||||
if( !NodeRunner.CurrentBlueprint.IsRunning ) {
|
||||
( NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode )!.Run(
|
||||
);
|
||||
}
|
||||
|
||||
//refresh all tasks
|
||||
for (int i = 0; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
NodeRunner item = allAvailableTask[i];
|
||||
if ((item is null) || item.IsCompleted)
|
||||
{
|
||||
allAvailableTask[i] = NodeRunner.Run();
|
||||
for( int i = 0; i < allAvailableTask.Length; i++ ) {
|
||||
NodeRunner item = allAvailableTask[ i ];
|
||||
if( ( item is null ) || item.IsCompleted ) {
|
||||
allAvailableTask[ i ] = NodeRunner.Run();
|
||||
}
|
||||
}
|
||||
BlueprintNode node;
|
||||
while (!cts.Token.IsCancellationRequested
|
||||
&& NodeRunner.CurrentBlueprint.RequestNextNativeNode(out node))
|
||||
{
|
||||
while( !cts.Token.IsCancellationRequested && NodeRunner.CurrentBlueprint
|
||||
.RequestNextNativeNode( out node ) ) {
|
||||
node.Run();
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
if( !NodeRunner.CurrentBlueprint.IsRunning ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(NodeRunner.CurrentBlueprint.EndLayer as EndingNode)!.Run();
|
||||
return blueprintInWaiting.TryPeek(out NodeRunner.CurrentBlueprint);
|
||||
( NodeRunner.CurrentBlueprint.EndLayer as EndingNode )!.Run();
|
||||
return blueprintInWaiting.TryPeek( out NodeRunner.CurrentBlueprint );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -200,7 +190,8 @@ namespace StgSharp.BluePrint
|
||||
|
||||
private static Blueprint _current;
|
||||
|
||||
internal NodeRunner() : base()
|
||||
internal NodeRunner()
|
||||
: base()
|
||||
{
|
||||
_startup = InternalRun;
|
||||
}
|
||||
@@ -213,20 +204,20 @@ namespace StgSharp.BluePrint
|
||||
internal static NodeRunner Run()
|
||||
{
|
||||
NodeRunner ret = new NodeRunner { _startup = InternalRun };
|
||||
ret.taskInternal = Task.Run(ret.InternalAction);
|
||||
ret._internalTask = Task.Run( ret.InternalAction );
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static void InternalRun(CancellationToken token)
|
||||
private static void InternalRun( CancellationToken token )
|
||||
{
|
||||
//Console.WriteLine("Task Begin");
|
||||
BlueprintNode item;
|
||||
while ((CurrentBlueprint != null) && CurrentBlueprint.IsRunning && !token.IsCancellationRequested
|
||||
&& CurrentBlueprint.RequestNexGlobalNode(out item))
|
||||
{
|
||||
while( ( CurrentBlueprint != null ) && CurrentBlueprint.IsRunning && !token.IsCancellationRequested && CurrentBlueprint.RequestNexGlobalNode(
|
||||
out item ) ) {
|
||||
//Console.WriteLine($"Get node of {item.Name}, going to run.");
|
||||
item.Run();
|
||||
}
|
||||
|
||||
//Console.WriteLine("Task Completed");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user