aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/Threading/HleScheduler.cs
blob: d5dbb4d8e4ee4f1a66a4bc4b26e90f6254f3ff57 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Threading;

namespace Ryujinx.HLE.HOS.Kernel.Threading
{
    partial class KScheduler
    {
        private const int RoundRobinTimeQuantumMs = 10;

        private int _currentCore;

        public bool MultiCoreScheduling { get; set; }

        public HleCoreManager CoreManager { get; private set; }

        private bool _keepPreempting;

        public void StartAutoPreemptionThread()
        {
            Thread preemptionThread = new Thread(PreemptCurrentThread);

            _keepPreempting = true;

            preemptionThread.Start();
        }

        public void ContextSwitch()
        {
            lock (CoreContexts)
            {
                if (MultiCoreScheduling)
                {
                    int selectedCount = 0;

                    for (int core = 0; core < CpuCoresCount; core++)
                    {
                        KCoreContext coreContext = CoreContexts[core];

                        if (coreContext.ContextSwitchNeeded && (coreContext.CurrentThread?.Context.IsCurrentThread() ?? false))
                        {
                            coreContext.ContextSwitch();
                        }

                        if (coreContext.CurrentThread?.Context.IsCurrentThread() ?? false)
                        {
                            selectedCount++;
                        }
                    }

                    if (selectedCount == 0)
                    {
                        CoreManager.Reset(Thread.CurrentThread);
                    }
                    else if (selectedCount == 1)
                    {
                        CoreManager.Set(Thread.CurrentThread);
                    }
                    else
                    {
                        throw new InvalidOperationException("Thread scheduled in more than one core!");
                    }
                }
                else
                {
                    KThread currentThread = CoreContexts[_currentCore].CurrentThread;

                    bool hasThreadExecuting = currentThread != null;

                    if (hasThreadExecuting)
                    {
                        //If this is not the thread that is currently executing, we need
                        //to request an interrupt to allow safely starting another thread.
                        if (!currentThread.Context.IsCurrentThread())
                        {
                            currentThread.Context.RequestInterrupt();

                            return;
                        }

                        CoreManager.Reset(currentThread.Context.Work);
                    }

                    //Advance current core and try picking a thread,
                    //keep advancing if it is null.
                    for (int core = 0; core < 4; core++)
                    {
                        _currentCore = (_currentCore + 1) % CpuCoresCount;

                        KCoreContext coreContext = CoreContexts[_currentCore];

                        coreContext.UpdateCurrentThread();

                        if (coreContext.CurrentThread != null)
                        {
                            CoreManager.Set(coreContext.CurrentThread.Context.Work);

                            coreContext.CurrentThread.Context.Execute();

                            break;
                        }
                    }

                    //If nothing was running before, then we are on a "external"
                    //HLE thread, we don't need to wait.
                    if (!hasThreadExecuting)
                    {
                        return;
                    }
                }
            }

            CoreManager.Wait(Thread.CurrentThread);
        }

        private void PreemptCurrentThread()
        {
            //Preempts current thread every 10 milliseconds on a round-robin fashion,
            //when multi core scheduling is disabled, to try ensuring that all threads
            //gets a chance to run.
            while (_keepPreempting)
            {
                lock (CoreContexts)
                {
                    KThread currentThread = CoreContexts[_currentCore].CurrentThread;

                    currentThread?.Context.RequestInterrupt();
                }

                PreemptThreads();

                Thread.Sleep(RoundRobinTimeQuantumMs);
            }
        }

        public void ExitThread(KThread thread)
        {
            thread.Context.StopExecution();

            CoreManager.Exit(thread.Context.Work);
        }

        public void RemoveThread(KThread thread)
        {
            CoreManager.RemoveThread(thread.Context.Work);
        }
    }
}