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