aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Kernel/Threading/KConditionVariable.cs
blob: 891e632f9fa105eb3f00249aaf081a50764c9420 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Collections.Generic;
using System.Threading;

namespace Ryujinx.HLE.HOS.Kernel.Threading
{
    static class KConditionVariable
    {
        public static void Wait(KernelContext context, LinkedList<KThread> threadList, object mutex, long timeout)
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            context.CriticalSection.Enter();

            Monitor.Exit(mutex);

            currentThread.Withholder = threadList;

            currentThread.Reschedule(ThreadSchedState.Paused);

            currentThread.WithholderNode = threadList.AddLast(currentThread);

            if (currentThread.TerminationRequested)
            {
                threadList.Remove(currentThread.WithholderNode);

                currentThread.Reschedule(ThreadSchedState.Running);

                currentThread.Withholder = null;

                context.CriticalSection.Leave();
            }
            else
            {
                if (timeout > 0)
                {
                    context.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
                }

                context.CriticalSection.Leave();

                if (timeout > 0)
                {
                    context.TimeManager.UnscheduleFutureInvocation(currentThread);
                }
            }

            Monitor.Enter(mutex);
        }

        public static void NotifyAll(KernelContext context, LinkedList<KThread> threadList)
        {
            context.CriticalSection.Enter();

            LinkedListNode<KThread> node = threadList.First;

            for (; node != null; node = threadList.First)
            {
                KThread thread = node.Value;

                threadList.Remove(thread.WithholderNode);

                thread.Withholder = null;

                thread.Reschedule(ThreadSchedState.Running);
            }

            context.CriticalSection.Leave();
        }
    }
}