aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/OsHle/Handles/KThread.cs
blob: 3db46f3d67304d74104e6dadbdc0e096071c0758 (plain)
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
using ChocolArm64;
using System.Collections.Generic;

namespace Ryujinx.HLE.OsHle.Handles
{
    class KThread : KSynchronizationObject
    {
        public AThread Thread { get; private set; }

        public int CoreMask { get; set; }

        public long MutexAddress   { get; set; }
        public long CondVarAddress { get; set; }

        public bool CondVarSignaled { get; set; }

        private Process Process;

        public List<KThread> MutexWaiters { get; private set; }

        public KThread MutexOwner { get; set; }

        public int ActualPriority { get; private set; }
        public int WantedPriority { get; private set; }

        public int ActualCore  { get; set; }
        public int ProcessorId { get; set; }
        public int IdealCore   { get; set; }

        public int WaitHandle { get; set; }

        public long LastPc { get; set; }

        public int ThreadId => Thread.ThreadId;

        public KThread(
            AThread Thread,
            Process Process,
            int     ProcessorId,
            int     Priority)
        {
            this.Thread      = Thread;
            this.Process     = Process;
            this.ProcessorId = ProcessorId;
            this.IdealCore   = ProcessorId;

            MutexWaiters = new List<KThread>();

            CoreMask = 1 << ProcessorId;

            ActualPriority = WantedPriority = Priority;
        }

        public void SetPriority(int Priority)
        {
            WantedPriority = Priority;

            UpdatePriority();
        }

        public void UpdatePriority()
        {
            bool PriorityChanged;

            lock (Process.ThreadSyncLock)
            {
                int OldPriority = ActualPriority;

                int CurrPriority = WantedPriority;

                foreach (KThread Thread in MutexWaiters)
                {
                    int WantedPriority = Thread.WantedPriority;

                    if (CurrPriority > WantedPriority)
                    {
                        CurrPriority = WantedPriority;
                    }
                }

                PriorityChanged = CurrPriority != OldPriority;

                ActualPriority = CurrPriority;
            }

            if (PriorityChanged)
            {
                Process.Scheduler.Resort(this);

                MutexOwner?.UpdatePriority();
            }
        }
    }
}