private void CommitTerminateThread(NsScriptThread thread) { thread.CallFrameStack.Clear(); thread.EvalStack.Clear(); _threads.Remove(thread); _terminatedThreads.Add(thread.Id); }
internal void ProcessPendingThreadActions() { if (!IsRunning) { return; } while (PendingThreadActions.TryDequeue(out ThreadAction action)) { NsScriptThread thread = action.Thread; switch (action.Kind) { case ThreadAction.ActionKind.Create: _threads.Add(thread); _newThreads.Add(thread.Id); break; case ThreadAction.ActionKind.Terminate: CommitTerminateThread(thread); break; case ThreadAction.ActionKind.Suspend: CommitSuspendThread(thread, action.Timeout); break; case ThreadAction.ActionKind.Join: Debug.Assert(action.JoinedThread is not null); CommitJoin(thread, action.JoinedThread); break; case ThreadAction.ActionKind.Resume: CommitResumeThread(thread); break; } } }
public WaitOperation( NsScriptThread thread, WaitCondition condition, EntityQuery?entityQuery) { Thread = thread; Condition = condition; EntityQuery = entityQuery; }
private void CommitResumeThread(NsScriptThread thread) { if (!IsRunning) { return; } thread.SleepTimeout = null; thread.SuspensionTime = null; }
private void CommitJoin(NsScriptThread thread, NsScriptThread target) { if (!IsRunning) { return; } thread.SuspensionTime = Ticks; target.WaitingThread = thread; }
internal void CommitSuspendThread(NsScriptThread thread, TimeSpan?timeoutOpt) { if (!IsRunning) { return; } thread.SuspensionTime = Ticks; if (timeoutOpt is TimeSpan timeout) { thread.SleepTimeout = (long)Math.Round(timeout.TotalSeconds * Stopwatch.Frequency); } }
public NsScriptProcess(NsScriptVM vm, uint id, NsScriptThread mainThread) { VM = vm; Id = id; MainThread = mainThread; _threads = new ArrayBuilder <NsScriptThread>(16); PendingThreadActions = new Queue <ThreadAction>(); _newThreads = new ArrayBuilder <uint>(8); _terminatedThreads = new ArrayBuilder <uint>(8); _clockBase = 0; _clock = Stopwatch.StartNew(); AttachThread(mainThread); }
internal void AttachThread(NsScriptThread thread) { thread.Process = this; PendingThreadActions.Enqueue(ThreadAction.Create(thread)); }