aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Core/OsHle/Handles/KThread.cs
blob: ce9c3db23a0d000167465183e040d0738538f04b (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
using ChocolArm64;
using System.Collections.Generic;

namespace Ryujinx.Core.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; }

        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 IdealCore  { get; set; }

        public int WaitHandle { get; set; }

        public int ThreadId => Thread.ThreadId;

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

            MutexWaiters = new List<KThread>();

            CoreMask = 1 << IdealCore;

            ActualPriority = WantedPriority = Priority;
        }

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

            UpdatePriority();
        }

        public void UpdatePriority()
        {
            int OldPriority = ActualPriority;

            int CurrPriority = WantedPriority;

            lock (Process.ThreadSyncLock)
            {
                foreach (KThread Thread in MutexWaiters)
                {
                    if (CurrPriority > Thread.WantedPriority)
                    {
                        CurrPriority = Thread.WantedPriority;
                    }
                }
            }

            if (CurrPriority != OldPriority)
            {
                ActualPriority = CurrPriority;

                Process.Scheduler.Resort(this);

                MutexOwner?.UpdatePriority();
            }
        }
    }
}