private bool CondVarWait( KThread WaitThread, int WaitThreadHandle, long MutexAddress, long CondVarAddress, ulong Timeout) { WaitThread.WaitHandle = WaitThreadHandle; WaitThread.MutexAddress = MutexAddress; WaitThread.CondVarAddress = CondVarAddress; lock (Process.ThreadSyncLock) { MutexUnlock(WaitThread, MutexAddress); WaitThread.CondVarSignaled = false; Process.ThreadArbiterList.Add(WaitThread); } Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state..."); if (Timeout != ulong.MaxValue) { Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout)); lock (Process.ThreadSyncLock) { if (!WaitThread.CondVarSignaled || WaitThread.MutexOwner != null) { if (WaitThread.MutexOwner != null) { WaitThread.MutexOwner.MutexWaiters.Remove(WaitThread); WaitThread.MutexOwner.UpdatePriority(); WaitThread.MutexOwner = null; } Process.ThreadArbiterList.Remove(WaitThread); Ns.Log.PrintDebug(LogClass.KernelSvc, "Timed out..."); return(false); } } } else { Process.Scheduler.EnterWait(WaitThread); } return(true); }
static ulong WaitForAddress(Process Process, AThreadState ThreadState, long Address, ulong Timeout) { KThread CurrentThread = Process.GetThread(ThreadState.Tpidr); Process.Scheduler.SetReschedule(CurrentThread.ProcessorId); CurrentThread.ArbiterWaitAddress = Address; CurrentThread.ArbiterSignaled = false; Process.Scheduler.EnterWait(CurrentThread, NsTimeConverter.GetTimeMs(Timeout)); if (!CurrentThread.ArbiterSignaled) { return(MakeError(ErrorModule.Kernel, KernelErr.Timeout)); } return(0); }
private void SvcSleepThread(AThreadState ThreadState) { ulong TimeoutNs = ThreadState.X0; KThread CurrThread = Process.GetThread(ThreadState.Tpidr); if (TimeoutNs == 0) { Process.Scheduler.Yield(CurrThread); } else { Process.Scheduler.Suspend(CurrThread); Thread.Sleep(NsTimeConverter.GetTimeMs(TimeoutNs)); Process.Scheduler.Resume(CurrThread); } }
private void SvcSleepThread(AThreadState ThreadState) { ulong TimeoutNs = ThreadState.X0; Ns.Log.PrintDebug(LogClass.KernelSvc, "Timeout = " + TimeoutNs.ToString("x16")); KThread CurrThread = Process.GetThread(ThreadState.Tpidr); if (TimeoutNs == 0) { Process.Scheduler.Yield(CurrThread); } else { Process.Scheduler.Suspend(CurrThread); Thread.Sleep(NsTimeConverter.GetTimeMs(TimeoutNs)); Process.Scheduler.Resume(CurrThread); } }
private void SvcWaitSynchronization(AThreadState ThreadState) { long HandlesPtr = (long)ThreadState.X1; int HandlesCount = (int)ThreadState.X2; ulong Timeout = ThreadState.X3; Ns.Log.PrintDebug(LogClass.KernelSvc, "HandlesPtr = " + HandlesPtr.ToString("x16") + ", " + "HandlesCount = " + HandlesCount.ToString("x8") + ", " + "Timeout = " + Timeout.ToString("x16")); if ((uint)HandlesCount > 0x40) { ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.CountOutOfRange); return; } KThread CurrThread = Process.GetThread(ThreadState.Tpidr); WaitHandle[] Handles = new WaitHandle[HandlesCount + 1]; for (int Index = 0; Index < HandlesCount; Index++) { int Handle = Memory.ReadInt32(HandlesPtr + Index * 4); KSynchronizationObject SyncObj = Process.HandleTable.GetData <KSynchronizationObject>(Handle); if (SyncObj == null) { Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid handle 0x{Handle:x8}!"); ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle); return; } Handles[Index] = SyncObj.WaitEvent; } using (AutoResetEvent WaitEvent = new AutoResetEvent(false)) { if (!SyncWaits.TryAdd(CurrThread, WaitEvent)) { throw new InvalidOperationException(); } Handles[HandlesCount] = WaitEvent; Process.Scheduler.Suspend(CurrThread); int HandleIndex; ulong Result = 0; if (Timeout != ulong.MaxValue) { HandleIndex = WaitHandle.WaitAny(Handles, NsTimeConverter.GetTimeMs(Timeout)); } else { HandleIndex = WaitHandle.WaitAny(Handles); } if (HandleIndex == WaitHandle.WaitTimeout) { Result = MakeError(ErrorModule.Kernel, KernelErr.Timeout); } else if (HandleIndex == HandlesCount) { Result = MakeError(ErrorModule.Kernel, KernelErr.Canceled); } SyncWaits.TryRemove(CurrThread, out _); Process.Scheduler.Resume(CurrThread); ThreadState.X0 = Result; if (Result == 0) { ThreadState.X1 = (ulong)HandleIndex; } } }