2018-11-28 15:18:09 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
2018-12-17 22:33:36 -07:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
|
|
|
static class KConditionVariable
|
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
public static void Wait(Horizon system, LinkedList<KThread> threadList, object mutex, long timeout)
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
KThread currentThread = system.Scheduler.GetCurrentThread();
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
system.CriticalSection.Enter();
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
Monitor.Exit(mutex);
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
currentThread.Withholder = threadList;
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Paused);
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
currentThread.WithholderNode = threadList.AddLast(currentThread);
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
if (currentThread.ShallBeTerminated ||
|
|
|
|
currentThread.SchedFlags == ThreadSchedState.TerminationPending)
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
threadList.Remove(currentThread.WithholderNode);
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
currentThread.Reschedule(ThreadSchedState.Running);
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
currentThread.Withholder = null;
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
system.CriticalSection.Leave();
|
2018-11-28 15:18:09 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
if (timeout > 0)
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
|
2018-11-28 15:18:09 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
system.CriticalSection.Leave();
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
if (timeout > 0)
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
system.TimeManager.UnscheduleFutureInvocation(currentThread);
|
2018-11-28 15:18:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
Monitor.Enter(mutex);
|
2018-11-28 15:18:09 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
public static void NotifyAll(Horizon system, LinkedList<KThread> threadList)
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
system.CriticalSection.Enter();
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
LinkedListNode<KThread> node = threadList.First;
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
for (; node != null; node = threadList.First)
|
2018-11-28 15:18:09 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
KThread thread = node.Value;
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
threadList.Remove(thread.WithholderNode);
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
thread.Withholder = null;
|
2018-11-28 15:18:09 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
thread.Reschedule(ThreadSchedState.Running);
|
2018-11-28 15:18:09 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
system.CriticalSection.Leave();
|
2018-11-28 15:18:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|