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