daily update on 20240620 by nitload youmu

This commit is contained in:
Nitload
2024-06-20 07:47:00 +08:00
parent 7fb220c523
commit b2cb0cbddf
39 changed files with 1252 additions and 550 deletions
+72 -145
View File
@@ -45,33 +45,60 @@ using System.Xml.Serialization;
namespace StgSharp.Logic
{
/// <summary>
/// A sets of complex logic designed for running in multi threads environment.
/// Warning: do not run a <see cref="Blueprint"/> instance in multi threads more than once.
///
/// </summary>
public partial class Blueprint : IConvertableToBlueprintNode
{
private int runningFlag;
private BeginningNode beginNode;
private Dictionary<string, BlueprintNode> allNode;
private EndingNode endNode;
private Func<bool> terminate;
private int currentNodeIndex;
private List<Task> allThread;
internal int currentGlobalNodeIndex;
internal int currentNativeNodeIndex;
private List<BlueprintNode> nativeNodeList;
private List<BlueprintNode> nodeList;
private List<BlueprintNode> globalNodeList;
public Blueprint()
{
allNode = new Dictionary<string, BlueprintNode>();
allThread = new List<Task>();
nodeList = new List<BlueprintNode>();
globalNodeList = new List<BlueprintNode>();
nativeNodeList = new List<BlueprintNode>();
terminate = () => true;
beginNode = new BeginningNode();
endNode = new EndingNode();
//terminate = () => true;
beginNode = new BeginningNode(this);
endNode = new EndingNode(this);
}
public BlueprintNode BeginLayer => beginNode;
public BlueprintNode EndLayer => endNode;
public bool IsRunning => allThread.Any(t => t.Status == TaskStatus.Running);
public bool IsRunning
{
get => runningFlag == 1;
internal set => Interlocked.Exchange(ref runningFlag ,(value ? 1 : 0));
}
/**/
internal Func<bool> ShouldClose
{
get => terminate;
set => terminate = value;
}
/**/
public void AddNode(BlueprintNode node, bool isNative)
{
_ = node ?? throw new ArgumentNullException(nameof(node));
node.IsNative = isNative;
allNode.Add(node.Name, node);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (string name, BlueprintPipelineArgs arg)[] GetOutput()
@@ -79,115 +106,27 @@ namespace StgSharp.Logic
return endNode.OutputData;
}
public void Init(int threadCount)
public void Init()
{
if (threadCount > nodeList.Count)
foreach (KeyValuePair<string, BlueprintNode> item in allNode)
{
InternalIO.InternalWriteLog(
"Thread setting error: amount of threads of this blueprint is larger than amount of nodes.", LogType.Warning);
threadCount = nodeList.Count;
}
if (threadCount!=0)
{
foreach (var item in allNode)
if (item.Value.IsNative)
{
if (item.Value.IsNative)
{
nativeNodeList.Add(item.Value);
}
else
{
nodeList.Add(item.Value);
}
nativeNodeList.Add(item.Value);
}
nativeNodeList.Sort();
PrepareTasks(threadCount);
}
else
{
foreach (var item in allNode)
else
{
nodeList.Add(item.Value);
globalNodeList.Add(item.Value);
}
}
nodeList.Sort();
nativeNodeList.Sort();
globalNodeList.Sort();
}
public void PrepareTasks(int count)
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
{
for (int i = 0; i < count; i++)
{
allThread.Add(new Task(IndividualTask));
}
}
private void IndividualTask()
{
do
{
BlueprintNode node;
while (RequestNextNode(out node))
{
node.Run();
}
} while (!terminate());
}
/// <summary>
/// Run all nodes repeatedly until it is instructed to terminate.
/// </summary>
/// <param name="terminateInstructor">Instructor to terminate all tasks. Returns <see langword="true"/> if tasks should terminate</param>
public void RunRepeatedly(Func<bool> terminateInstructor)
{
if (allThread.Count != 0)
{
MultiThreadRun(terminateInstructor);
return;
}
SingleThreadRun(terminateInstructor);
}
private void MultiThreadRun(Func<bool> terminateInstructor)
{
if (terminate != terminateInstructor)
{
terminate = terminateInstructor;
if (allThread.Count != 0)
{
foreach (var t in allThread)
{
t.Start();
}
}
}
beginNode.Run();
foreach (var node in nativeNodeList)
{
node.Run();
}
EndLayer.Run();
}
private void SingleThreadRun(Func<bool> terminateInstructor)
{
beginNode.Run();
foreach (var item in nodeList)
{
item.Run();
}
EndLayer.Run();
}
public void AddNativeNode(BlueprintNode node)
{
node.IsNative = true;
allNode.Add(node.Name, node);
}
public void AddAsyncNode(BlueprintNode node)
{
node.IsNative = false;
allNode.Add(node.Name, node);
BlueprintRunner.Run(this, () => true);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -196,59 +135,47 @@ namespace StgSharp.Logic
beginNode.InputData = parameters;
}
private bool RequestNextNode(out BlueprintNode node)
internal bool RequestNexGlobalNode(out BlueprintNode node)
{
if (currentNodeIndex < nodeList.Count)
if (currentGlobalNodeIndex < globalNodeList.Count)
{
BlueprintNode ret = nodeList[currentNodeIndex];
Interlocked.Increment(ref currentNodeIndex);
node = ret;
node = globalNodeList[currentGlobalNodeIndex];
Interlocked.Increment(ref currentGlobalNodeIndex);
return true;
}
node = default!;
return false;
}
internal bool RequestNextNativeNode(out BlueprintNode node)
{
if (currentNativeNodeIndex < nativeNodeList.Count)
{
node = nativeNodeList[currentNativeNodeIndex];
Interlocked.Increment(ref currentNativeNodeIndex);
return true;
}
Interlocked.Exchange(ref currentNodeIndex, 0);
node = default!;
return false;
}
string[] IConvertableToBlueprintNode.InputInterfacesName
{
get
{
return beginNode.OutputInterfaces.Select(
p => p.Key).ToArray();
}
}
BlueprintNodeAction IConvertableToBlueprintNode.MainExecution
{
get
{
return (in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
=>
{
beginNode.Run();
foreach (var node in nativeNodeList)
{
node.Run();
}
endNode.Run();
};
}
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
}
string[] IConvertableToBlueprintNode.OutputInterfacesName
{
get
{
return beginNode.OutputInterfaces.Select(
p => p.Key).ToArray();
}
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
}
}
internal class NodeClearException : Exception
{
public void Terminate()
{
Interlocked.Exchange(ref currentGlobalNodeIndex, int.MaxValue);
Interlocked.Exchange(ref currentNativeNodeIndex, int.MaxValue);
}
}
}