aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Core/OsHle/Svc/SvcThread.cs
blob: a635edb15c2f1c7c89979a5e16f6f3635d31929c (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
using ChocolArm64.State;
using Ryujinx.Core.OsHle.Handles;

namespace Ryujinx.Core.OsHle.Svc
{
    partial class SvcHandler
    {
        private void SvcCreateThread(AThreadState ThreadState)
        {
            long EntryPoint  = (long)ThreadState.X1;
            long ArgsPtr     = (long)ThreadState.X2;
            long StackTop    = (long)ThreadState.X3;
            int  Priority    =  (int)ThreadState.X4;
            int  ProcessorId =  (int)ThreadState.X5;

            if (Ns.Os.TryGetProcess(ThreadState.ProcessId, out Process Process))
            {
                if (ProcessorId == -2)
                {
                    //TODO: Get this value from the NPDM file.
                    ProcessorId = 0;
                }

                int Handle = Process.MakeThread(
                    EntryPoint,
                    StackTop,
                    ArgsPtr,
                    Priority,
                    ProcessorId);

                ThreadState.X0 = (int)SvcResult.Success;
                ThreadState.X1 = (ulong)Handle;
            }

            //TODO: Error codes.
        }

        private void SvcStartThread(AThreadState ThreadState)
        {
            int Handle = (int)ThreadState.X0;

            HThread Thread = Ns.Os.Handles.GetData<HThread>(Handle);

            if (Thread != null)
            {
                Process.Scheduler.StartThread(Thread);

                ThreadState.X0 = (int)SvcResult.Success;
            }

            //TODO: Error codes.
        }

        private void SvcSleepThread(AThreadState ThreadState)
        {           
            ulong NanoSecs = ThreadState.X0;

            HThread CurrThread = Process.GetThread(ThreadState.Tpidr);
            
            if (NanoSecs == 0)
            {
                Process.Scheduler.Yield(CurrThread);
            }
            else
            {
                Process.Scheduler.WaitForSignal(CurrThread, (int)(NanoSecs / 1000000));
            }
        }

        private void SvcGetThreadPriority(AThreadState ThreadState)
        {
            int Handle = (int)ThreadState.X1;

            HThread Thread = Ns.Os.Handles.GetData<HThread>(Handle);

            if (Thread != null)
            {
                ThreadState.X1 = (ulong)Thread.Priority;
                ThreadState.X0 = (int)SvcResult.Success;
            }

            //TODO: Error codes.
        }
    }
}