2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64;
|
2018-11-28 22:18:09 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-09-18 23:36:43 +00:00
|
|
|
using System;
|
2018-05-14 06:01:10 +00:00
|
|
|
using System.Collections.Generic;
|
2018-09-18 23:36:43 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
class KThread : KSynchronizationObject, IKFutureSchedulerObject
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
public CpuThread Context { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public long AffinityMask { get; set; }
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public long ThreadUid { get; private set; }
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public long TotalTimeRunning { get; set; }
|
|
|
|
|
|
|
|
public KSynchronizationObject SignaledObj { get; set; }
|
2018-05-16 01:36:08 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public long CondVarAddress { get; set; }
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private ulong Entrypoint;
|
|
|
|
|
|
|
|
public long MutexAddress { get; set; }
|
|
|
|
|
|
|
|
public KProcess Owner { get; private set; }
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private ulong TlsAddress;
|
|
|
|
|
|
|
|
public long LastScheduledTime { get; set; }
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public LinkedListNode<KThread>[] SiblingsPerCore { get; private set; }
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public LinkedList<KThread> Withholder { get; set; }
|
|
|
|
public LinkedListNode<KThread> WithholderNode { get; set; }
|
|
|
|
|
|
|
|
public LinkedListNode<KThread> ProcessListNode { get; set; }
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
private LinkedList<KThread> MutexWaiters;
|
|
|
|
private LinkedListNode<KThread> MutexWaiterNode;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public KThread MutexOwner { get; private set; }
|
2018-06-26 04:09:32 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public int ThreadHandleForUserMutex { get; set; }
|
|
|
|
|
|
|
|
private ThreadSchedState ForcePauseFlags;
|
|
|
|
|
|
|
|
public int ObjSyncResult { get; set; }
|
|
|
|
|
|
|
|
public int DynamicPriority { get; set; }
|
|
|
|
public int CurrentCore { get; set; }
|
|
|
|
public int BasePriority { get; set; }
|
|
|
|
public int PreferredCore { get; set; }
|
|
|
|
|
|
|
|
private long AffinityMaskOverride;
|
|
|
|
private int PreferredCoreOverride;
|
|
|
|
private int AffinityOverrideCount;
|
|
|
|
|
|
|
|
public ThreadSchedState SchedFlags { get; private set; }
|
|
|
|
|
|
|
|
public bool ShallBeTerminated { get; private set; }
|
|
|
|
|
|
|
|
public bool SyncCancelled { get; set; }
|
|
|
|
public bool WaitingSync { get; set; }
|
|
|
|
|
|
|
|
private bool HasExited;
|
|
|
|
|
|
|
|
public bool WaitingInArbitration { get; set; }
|
|
|
|
|
|
|
|
private KScheduler Scheduler;
|
|
|
|
|
|
|
|
private KSchedulingData SchedulingData;
|
|
|
|
|
|
|
|
public long LastPc { get; set; }
|
2018-02-14 02:43:08 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KThread(Horizon System) : base(System)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
Scheduler = System.Scheduler;
|
|
|
|
SchedulingData = System.Scheduler.SchedulingData;
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
SiblingsPerCore = new LinkedListNode<KThread>[KScheduler.CpuCoresCount];
|
2018-05-14 06:01:10 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
MutexWaiters = new LinkedList<KThread>();
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Initialize(
|
|
|
|
ulong Entrypoint,
|
|
|
|
ulong ArgsPtr,
|
|
|
|
ulong StackTop,
|
|
|
|
int Priority,
|
|
|
|
int DefaultCpuCore,
|
|
|
|
KProcess Owner,
|
|
|
|
ThreadType Type = ThreadType.User)
|
|
|
|
{
|
|
|
|
if ((uint)Type > 3)
|
|
|
|
{
|
|
|
|
throw new ArgumentException($"Invalid thread type \"{Type}\".");
|
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
PreferredCore = DefaultCpuCore;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
AffinityMask |= 1L << DefaultCpuCore;
|
|
|
|
|
|
|
|
SchedFlags = Type == ThreadType.Dummy
|
|
|
|
? ThreadSchedState.Running
|
|
|
|
: ThreadSchedState.None;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
CurrentCore = PreferredCore;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
DynamicPriority = Priority;
|
|
|
|
BasePriority = Priority;
|
|
|
|
|
|
|
|
ObjSyncResult = 0x7201;
|
|
|
|
|
|
|
|
this.Entrypoint = Entrypoint;
|
|
|
|
|
|
|
|
if (Type == ThreadType.User)
|
|
|
|
{
|
|
|
|
if (Owner.AllocateThreadLocalStorage(out TlsAddress) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryHelper.FillWithZeros(Owner.CpuMemory, (long)TlsAddress, KTlsPageInfo.TlsEntrySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Is64Bits;
|
|
|
|
|
|
|
|
if (Owner != null)
|
|
|
|
{
|
|
|
|
this.Owner = Owner;
|
|
|
|
|
|
|
|
Owner.IncrementThreadCount();
|
|
|
|
|
|
|
|
Is64Bits = (Owner.MmuFlags & 1) != 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Is64Bits = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context = new CpuThread(Owner.Translator, Owner.CpuMemory, (long)Entrypoint);
|
|
|
|
|
|
|
|
Context.ThreadState.X0 = ArgsPtr;
|
|
|
|
Context.ThreadState.X31 = StackTop;
|
|
|
|
|
|
|
|
Context.ThreadState.CntfrqEl0 = 19200000;
|
|
|
|
Context.ThreadState.Tpidr = (long)TlsAddress;
|
|
|
|
|
|
|
|
Owner.SubscribeThreadEventHandlers(Context);
|
|
|
|
|
|
|
|
Context.WorkFinished += ThreadFinishedHandler;
|
|
|
|
|
|
|
|
ThreadUid = System.GetThreadUid();
|
|
|
|
|
|
|
|
if (Owner != null)
|
|
|
|
{
|
|
|
|
Owner.AddThread(this);
|
|
|
|
|
|
|
|
if (Owner.IsPaused)
|
|
|
|
{
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
|
|
|
if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
|
|
|
|
{
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ForcePauseFlags |= ThreadSchedState.ProcessPauseFlag;
|
|
|
|
|
|
|
|
CombineForcePauseFlags();
|
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KernelResult Start()
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
if (!System.KernelInitialized)
|
|
|
|
{
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
|
|
|
if (!ShallBeTerminated && SchedFlags != ThreadSchedState.TerminationPending)
|
|
|
|
{
|
|
|
|
ForcePauseFlags |= ThreadSchedState.KernelInitPauseFlag;
|
|
|
|
|
|
|
|
CombineForcePauseFlags();
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
KernelResult Result = KernelResult.ThreadTerminating;
|
|
|
|
|
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (!ShallBeTerminated)
|
|
|
|
{
|
|
|
|
KThread CurrentThread = System.Scheduler.GetCurrentThread();
|
|
|
|
|
|
|
|
while (SchedFlags != ThreadSchedState.TerminationPending &&
|
|
|
|
CurrentThread.SchedFlags != ThreadSchedState.TerminationPending &&
|
|
|
|
!CurrentThread.ShallBeTerminated)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((SchedFlags & ThreadSchedState.LowMask) != ThreadSchedState.None)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
Result = KernelResult.InvalidState;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurrentThread.ForcePauseFlags == ThreadSchedState.None)
|
|
|
|
{
|
|
|
|
if (Owner != null && ForcePauseFlags != ThreadSchedState.None)
|
|
|
|
{
|
|
|
|
CombineForcePauseFlags();
|
|
|
|
}
|
|
|
|
|
|
|
|
SetNewSchedFlags(ThreadSchedState.Running);
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
Result = KernelResult.Success;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CurrentThread.CombineForcePauseFlags();
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (CurrentThread.ShallBeTerminated)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Exit()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
ForcePauseFlags &= ~ThreadSchedState.ForcePauseMask;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
ExitImpl();
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ExitImpl()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
SetNewSchedFlags(ThreadSchedState.TerminationPending);
|
|
|
|
|
|
|
|
HasExited = true;
|
|
|
|
|
|
|
|
Signal();
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public long Sleep(long Timeout)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (ShallBeTerminated || SchedFlags == ThreadSchedState.TerminationPending)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetNewSchedFlags(ThreadSchedState.Paused);
|
|
|
|
|
|
|
|
if (Timeout > 0)
|
|
|
|
{
|
|
|
|
System.TimeManager.ScheduleFutureInvocation(this, Timeout);
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (Timeout > 0)
|
|
|
|
{
|
|
|
|
System.TimeManager.UnscheduleFutureInvocation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Yield()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (SchedFlags != ThreadSchedState.Running)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
System.Scheduler.ContextSwitch();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DynamicPriority < KScheduler.PrioritiesCount)
|
|
|
|
{
|
|
|
|
//Move current thread to the end of the queue.
|
|
|
|
SchedulingData.Reschedule(DynamicPriority, CurrentCore, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
System.Scheduler.ContextSwitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void YieldWithLoadBalancing()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (SchedFlags != ThreadSchedState.Running)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
System.Scheduler.ContextSwitch();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
int Prio = DynamicPriority;
|
|
|
|
int Core = CurrentCore;
|
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
KThread NextThreadOnCurrentQueue = null;
|
|
|
|
|
|
|
|
if (DynamicPriority < KScheduler.PrioritiesCount)
|
|
|
|
{
|
|
|
|
//Move current thread to the end of the queue.
|
|
|
|
SchedulingData.Reschedule(Prio, Core, this);
|
|
|
|
|
|
|
|
Func<KThread, bool> Predicate = x => x.DynamicPriority == Prio;
|
|
|
|
|
|
|
|
NextThreadOnCurrentQueue = SchedulingData.ScheduledThreads(Core).FirstOrDefault(Predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerable<KThread> SuitableCandidates()
|
|
|
|
{
|
|
|
|
foreach (KThread Thread in SchedulingData.SuggestedThreads(Core))
|
|
|
|
{
|
|
|
|
int SrcCore = Thread.CurrentCore;
|
|
|
|
|
|
|
|
if (SrcCore >= 0)
|
|
|
|
{
|
|
|
|
KThread SelectedSrcCore = Scheduler.CoreContexts[SrcCore].SelectedThread;
|
|
|
|
|
|
|
|
if (SelectedSrcCore == Thread || ((SelectedSrcCore?.DynamicPriority ?? 2) < 2))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//If the candidate was scheduled after the current thread, then it's not worth it,
|
|
|
|
//unless the priority is higher than the current one.
|
2018-11-28 22:18:09 +00:00
|
|
|
if (NextThreadOnCurrentQueue.LastScheduledTime >= Thread.LastScheduledTime ||
|
2018-09-18 23:36:43 +00:00
|
|
|
NextThreadOnCurrentQueue.DynamicPriority < Thread.DynamicPriority)
|
|
|
|
{
|
|
|
|
yield return Thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread Dst = SuitableCandidates().FirstOrDefault(x => x.DynamicPriority <= Prio);
|
|
|
|
|
|
|
|
if (Dst != null)
|
|
|
|
{
|
|
|
|
SchedulingData.TransferToCore(Dst.DynamicPriority, Core, Dst);
|
|
|
|
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this != NextThreadOnCurrentQueue)
|
|
|
|
{
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
System.Scheduler.ContextSwitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void YieldAndWaitForLoadBalancing()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (SchedFlags != ThreadSchedState.Running)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
System.Scheduler.ContextSwitch();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Core = CurrentCore;
|
|
|
|
|
|
|
|
SchedulingData.TransferToCore(DynamicPriority, -1, this);
|
|
|
|
|
|
|
|
KThread SelectedThread = null;
|
|
|
|
|
|
|
|
if (!SchedulingData.ScheduledThreads(Core).Any())
|
|
|
|
{
|
|
|
|
foreach (KThread Thread in SchedulingData.SuggestedThreads(Core))
|
|
|
|
{
|
|
|
|
if (Thread.CurrentCore < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread FirstCandidate = SchedulingData.ScheduledThreads(Thread.CurrentCore).FirstOrDefault();
|
|
|
|
|
|
|
|
if (FirstCandidate == Thread)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FirstCandidate == null || FirstCandidate.DynamicPriority >= 2)
|
|
|
|
{
|
|
|
|
SchedulingData.TransferToCore(Thread.DynamicPriority, Core, Thread);
|
|
|
|
|
|
|
|
SelectedThread = Thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SelectedThread != this)
|
|
|
|
{
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
System.Scheduler.ContextSwitch();
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPriority(int Priority)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
BasePriority = Priority;
|
|
|
|
|
|
|
|
UpdatePriorityInheritance();
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public long SetActivity(bool Pause)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
long Result = 0;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
ThreadSchedState LowNibble = SchedFlags & ThreadSchedState.LowMask;
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
if (LowNibble != ThreadSchedState.Paused && LowNibble != ThreadSchedState.Running)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-06-22 02:05:42 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
return MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-06-22 02:05:42 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
if (!ShallBeTerminated && SchedFlags != ThreadSchedState.TerminationPending)
|
|
|
|
{
|
|
|
|
if (Pause)
|
|
|
|
{
|
|
|
|
//Pause, the force pause flag should be clear (thread is NOT paused).
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((ForcePauseFlags & ThreadSchedState.ThreadPauseFlag) == 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
ForcePauseFlags |= ThreadSchedState.ThreadPauseFlag;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
CombineForcePauseFlags();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Result = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
//Unpause, the force pause flag should be set (thread is paused).
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((ForcePauseFlags & ThreadSchedState.ThreadPauseFlag) != 0)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
ThreadSchedState OldForcePauseFlags = ForcePauseFlags;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
ForcePauseFlags &= ~ThreadSchedState.ThreadPauseFlag;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((OldForcePauseFlags & ~ThreadSchedState.ThreadPauseFlag) == ThreadSchedState.None)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
ThreadSchedState OldSchedFlags = SchedFlags;
|
2018-06-22 02:05:42 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
SchedFlags &= ThreadSchedState.LowMask;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
AdjustScheduling(OldSchedFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-05-13 03:22:42 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
Result = MakeError(ErrorModule.Kernel, KernelErr.InvalidState);
|
2018-05-13 03:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CancelSynchronization()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((SchedFlags & ThreadSchedState.LowMask) != ThreadSchedState.Paused || !WaitingSync)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
SyncCancelled = true;
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
else if (Withholder != null)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
Withholder.Remove(WithholderNode);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
SetNewSchedFlags(ThreadSchedState.Running);
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
Withholder = null;
|
2018-06-22 02:05:42 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
SyncCancelled = true;
|
2018-06-22 02:05:42 +00:00
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SignaledObj = null;
|
|
|
|
ObjSyncResult = (int)MakeError(ErrorModule.Kernel, KernelErr.Cancelled);
|
|
|
|
|
|
|
|
SetNewSchedFlags(ThreadSchedState.Running);
|
|
|
|
|
|
|
|
SyncCancelled = false;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KernelResult SetCoreAndAffinityMask(int NewCore, long NewAffinityMask)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
bool UseOverride = AffinityOverrideCount != 0;
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
//The value -3 is "do not change the preferred core".
|
|
|
|
if (NewCore == -3)
|
2018-06-22 02:05:42 +00:00
|
|
|
{
|
2018-09-18 23:36:43 +00:00
|
|
|
NewCore = UseOverride ? PreferredCoreOverride : PreferredCore;
|
2018-05-13 03:22:42 +00:00
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
if ((NewAffinityMask & (1 << NewCore)) == 0)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.InvalidCombination;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
if (UseOverride)
|
|
|
|
{
|
|
|
|
PreferredCoreOverride = NewCore;
|
|
|
|
AffinityMaskOverride = NewAffinityMask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long OldAffinityMask = AffinityMask;
|
|
|
|
|
|
|
|
PreferredCore = NewCore;
|
|
|
|
AffinityMask = NewAffinityMask;
|
|
|
|
|
|
|
|
if (OldAffinityMask != NewAffinityMask)
|
|
|
|
{
|
|
|
|
int OldCore = CurrentCore;
|
|
|
|
|
|
|
|
if (CurrentCore >= 0 && ((AffinityMask >> CurrentCore) & 1) == 0)
|
|
|
|
{
|
|
|
|
if (PreferredCore < 0)
|
|
|
|
{
|
|
|
|
CurrentCore = HighestSetCore(AffinityMask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CurrentCore = PreferredCore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustSchedulingForNewAffinity(OldAffinityMask, OldCore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int HighestSetCore(long Mask)
|
|
|
|
{
|
|
|
|
for (int Core = KScheduler.CpuCoresCount - 1; Core >= 0; Core--)
|
|
|
|
{
|
|
|
|
if (((Mask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
return Core;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CombineForcePauseFlags()
|
|
|
|
{
|
|
|
|
ThreadSchedState OldFlags = SchedFlags;
|
2018-11-28 22:18:09 +00:00
|
|
|
ThreadSchedState LowNibble = SchedFlags & ThreadSchedState.LowMask;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
SchedFlags = LowNibble | ForcePauseFlags;
|
|
|
|
|
|
|
|
AdjustScheduling(OldFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetNewSchedFlags(ThreadSchedState NewFlags)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
ThreadSchedState OldFlags = SchedFlags;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
SchedFlags = (OldFlags & ThreadSchedState.HighMask) | NewFlags;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((OldFlags & ThreadSchedState.LowMask) != NewFlags)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
AdjustScheduling(OldFlags);
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ReleaseAndResume()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if ((SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
if (Withholder != null)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
Withholder.Remove(WithholderNode);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
SetNewSchedFlags(ThreadSchedState.Running);
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
Withholder = null;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetNewSchedFlags(ThreadSchedState.Running);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Reschedule(ThreadSchedState NewFlags)
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Enter();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
ThreadSchedState OldFlags = SchedFlags;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
SchedFlags = (OldFlags & ThreadSchedState.HighMask) |
|
|
|
|
(NewFlags & ThreadSchedState.LowMask);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
AdjustScheduling(OldFlags);
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddMutexWaiter(KThread Requester)
|
|
|
|
{
|
|
|
|
AddToMutexWaitersList(Requester);
|
|
|
|
|
|
|
|
Requester.MutexOwner = this;
|
|
|
|
|
|
|
|
UpdatePriorityInheritance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveMutexWaiter(KThread Thread)
|
|
|
|
{
|
|
|
|
if (Thread.MutexWaiterNode?.List != null)
|
|
|
|
{
|
|
|
|
MutexWaiters.Remove(Thread.MutexWaiterNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.MutexOwner = null;
|
|
|
|
|
|
|
|
UpdatePriorityInheritance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public KThread RelinquishMutex(long MutexAddress, out int Count)
|
|
|
|
{
|
|
|
|
Count = 0;
|
|
|
|
|
|
|
|
if (MutexWaiters.First == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread NewMutexOwner = null;
|
|
|
|
|
|
|
|
LinkedListNode<KThread> CurrentNode = MutexWaiters.First;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
//Skip all threads that are not waiting for this mutex.
|
|
|
|
while (CurrentNode != null && CurrentNode.Value.MutexAddress != MutexAddress)
|
|
|
|
{
|
|
|
|
CurrentNode = CurrentNode.Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurrentNode == null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListNode<KThread> NextNode = CurrentNode.Next;
|
|
|
|
|
|
|
|
MutexWaiters.Remove(CurrentNode);
|
|
|
|
|
|
|
|
CurrentNode.Value.MutexOwner = NewMutexOwner;
|
|
|
|
|
|
|
|
if (NewMutexOwner != null)
|
|
|
|
{
|
|
|
|
//New owner was already selected, re-insert on new owner list.
|
|
|
|
NewMutexOwner.AddToMutexWaitersList(CurrentNode.Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//New owner not selected yet, use current thread.
|
|
|
|
NewMutexOwner = CurrentNode.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Count++;
|
|
|
|
|
|
|
|
CurrentNode = NextNode;
|
|
|
|
}
|
|
|
|
while (CurrentNode != null);
|
|
|
|
|
|
|
|
if (NewMutexOwner != null)
|
|
|
|
{
|
|
|
|
UpdatePriorityInheritance();
|
|
|
|
|
|
|
|
NewMutexOwner.UpdatePriorityInheritance();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewMutexOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdatePriorityInheritance()
|
|
|
|
{
|
|
|
|
//If any of the threads waiting for the mutex has
|
|
|
|
//higher priority than the current thread, then
|
|
|
|
//the current thread inherits that priority.
|
|
|
|
int HighestPriority = BasePriority;
|
|
|
|
|
|
|
|
if (MutexWaiters.First != null)
|
|
|
|
{
|
|
|
|
int WaitingDynamicPriority = MutexWaiters.First.Value.DynamicPriority;
|
|
|
|
|
|
|
|
if (WaitingDynamicPriority < HighestPriority)
|
|
|
|
{
|
|
|
|
HighestPriority = WaitingDynamicPriority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HighestPriority != DynamicPriority)
|
|
|
|
{
|
|
|
|
int OldPriority = DynamicPriority;
|
|
|
|
|
|
|
|
DynamicPriority = HighestPriority;
|
|
|
|
|
|
|
|
AdjustSchedulingForNewPriority(OldPriority);
|
|
|
|
|
|
|
|
if (MutexOwner != null)
|
|
|
|
{
|
|
|
|
//Remove and re-insert to ensure proper sorting based on new priority.
|
|
|
|
MutexOwner.MutexWaiters.Remove(MutexWaiterNode);
|
|
|
|
|
|
|
|
MutexOwner.AddToMutexWaitersList(this);
|
|
|
|
|
|
|
|
MutexOwner.UpdatePriorityInheritance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddToMutexWaitersList(KThread Thread)
|
|
|
|
{
|
|
|
|
LinkedListNode<KThread> NextPrio = MutexWaiters.First;
|
|
|
|
|
|
|
|
int CurrentPriority = Thread.DynamicPriority;
|
|
|
|
|
|
|
|
while (NextPrio != null && NextPrio.Value.DynamicPriority <= CurrentPriority)
|
|
|
|
{
|
|
|
|
NextPrio = NextPrio.Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NextPrio != null)
|
|
|
|
{
|
|
|
|
Thread.MutexWaiterNode = MutexWaiters.AddBefore(NextPrio, Thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Thread.MutexWaiterNode = MutexWaiters.AddLast(Thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AdjustScheduling(ThreadSchedState OldFlags)
|
|
|
|
{
|
|
|
|
if (OldFlags == SchedFlags)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OldFlags == ThreadSchedState.Running)
|
|
|
|
{
|
|
|
|
//Was running, now it's stopped.
|
|
|
|
if (CurrentCore >= 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Unschedule(DynamicPriority, CurrentCore, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Unsuggest(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (SchedFlags == ThreadSchedState.Running)
|
|
|
|
{
|
|
|
|
//Was stopped, now it's running.
|
|
|
|
if (CurrentCore >= 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Schedule(DynamicPriority, CurrentCore, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Suggest(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AdjustSchedulingForNewPriority(int OldPriority)
|
|
|
|
{
|
|
|
|
if (SchedFlags != ThreadSchedState.Running)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove thread from the old priority queues.
|
|
|
|
if (CurrentCore >= 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Unschedule(OldPriority, CurrentCore, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Unsuggest(OldPriority, Core, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add thread to the new priority queues.
|
|
|
|
KThread CurrentThread = Scheduler.GetCurrentThread();
|
|
|
|
|
|
|
|
if (CurrentCore >= 0)
|
|
|
|
{
|
|
|
|
if (CurrentThread == this)
|
|
|
|
{
|
|
|
|
SchedulingData.SchedulePrepend(DynamicPriority, CurrentCore, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SchedulingData.Schedule(DynamicPriority, CurrentCore, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
if (Core != CurrentCore && ((AffinityMask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
SchedulingData.Suggest(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AdjustSchedulingForNewAffinity(long OldAffinityMask, int OldCore)
|
|
|
|
{
|
|
|
|
if (SchedFlags != ThreadSchedState.Running || DynamicPriority >= KScheduler.PrioritiesCount)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove from old queues.
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
if (((OldAffinityMask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
if (Core == OldCore)
|
|
|
|
{
|
|
|
|
SchedulingData.Unschedule(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SchedulingData.Unsuggest(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Insert on new queues.
|
|
|
|
for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++)
|
|
|
|
{
|
|
|
|
if (((AffinityMask >> Core) & 1) != 0)
|
|
|
|
{
|
|
|
|
if (Core == CurrentCore)
|
|
|
|
{
|
|
|
|
SchedulingData.Schedule(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SchedulingData.Suggest(DynamicPriority, Core, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler.ThreadReselectionRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool IsSignaled()
|
|
|
|
{
|
|
|
|
return HasExited;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public void SetEntryArguments(long ArgsPtr, int ThreadHandle)
|
|
|
|
{
|
|
|
|
Context.ThreadState.X0 = (ulong)ArgsPtr;
|
|
|
|
Context.ThreadState.X1 = (ulong)ThreadHandle;
|
|
|
|
}
|
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
public void ClearExclusive()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
Owner.CpuMemory.ClearExclusive(CurrentCore);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void TimeUp()
|
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
ReleaseAndResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PrintGuestStackTrace()
|
|
|
|
{
|
|
|
|
Owner.Debugger.PrintGuestStackTrace(Context.ThreadState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ThreadFinishedHandler(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
System.Scheduler.ExitThread(this);
|
|
|
|
|
|
|
|
Terminate();
|
|
|
|
|
|
|
|
System.Scheduler.RemoveThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Terminate()
|
|
|
|
{
|
|
|
|
Owner?.RemoveThread(this);
|
|
|
|
|
|
|
|
if (TlsAddress != 0 && Owner.FreeThreadLocalStorage(TlsAddress) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure freeing thread local storage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
|
|
|
//Wake up all threads that may be waiting for a mutex being held
|
|
|
|
//by this thread.
|
|
|
|
foreach (KThread Thread in MutexWaiters)
|
|
|
|
{
|
|
|
|
Thread.MutexOwner = null;
|
|
|
|
Thread.PreferredCoreOverride = 0;
|
|
|
|
Thread.ObjSyncResult = 0xfa01;
|
|
|
|
|
|
|
|
Thread.ReleaseAndResume();
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
System.CriticalSection.Leave();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
Owner?.DecrementThreadCountAndTerminateIfZero();
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|