aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Common/PreciseSleep/PreciseSleepHelper.cs
blob: 3c30a7f60a703de20bceef670d442d744d115ee4 (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
using Ryujinx.Common.SystemInterop;
using System;
using System.Threading;

namespace Ryujinx.Common.PreciseSleep
{
    public static class PreciseSleepHelper
    {
        /// <summary>
        /// Create a precise sleep event for the current platform.
        /// </summary>
        /// <returns>A precise sleep event</returns>
        public static IPreciseSleepEvent CreateEvent()
        {
            if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsIOS() || OperatingSystem.IsAndroid())
            {
                return new NanosleepEvent();
            }
            else if (OperatingSystem.IsWindows())
            {
                return new WindowsSleepEvent();
            }
            else
            {
                return new SleepEvent();
            }
        }

        /// <summary>
        /// Sleeps up to the closest point to the timepoint that the OS reasonably allows.
        /// The provided event is used by the timer to wake the current thread, and should not be signalled from any other source.
        /// </summary>
        /// <param name="evt">Event used to wake this thread</param>
        /// <param name="timePoint">Target timepoint in host ticks</param>
        public static void SleepUntilTimePoint(EventWaitHandle evt, long timePoint)
        {
            if (OperatingSystem.IsWindows())
            {
                WindowsGranularTimer.Instance.SleepUntilTimePointWithoutExternalSignal(evt, timePoint);
            }
            else
            {
                // Events might oversleep by a little, depending on OS.
                // We don't want to miss the timepoint, so bias the wait to be lower.
                // Nanosleep can possibly handle it better, too.
                long accuracyBias = PerformanceCounter.TicksPerMillisecond / 2;
                long now = PerformanceCounter.ElapsedTicks + accuracyBias;
                long ms = Math.Min((timePoint - now) / PerformanceCounter.TicksPerMillisecond, int.MaxValue);

                if (ms > 0)
                {
                    evt.WaitOne((int)ms);
                }

                if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsIOS() || OperatingSystem.IsAndroid())
                {
                    // Do a nanosleep.
                    now = PerformanceCounter.ElapsedTicks;
                    long ns = ((timePoint - now) * 1_000_000) / PerformanceCounter.TicksPerMillisecond;

                    Nanosleep.SleepAtMost(ns);
                }
            }
        }

        /// <summary>
        /// Spinwait until the given timepoint. If wakeSignal is or becomes 1, return early.
        /// Thread is allowed to yield.
        /// </summary>
        /// <param name="timePoint">Target timepoint in host ticks</param>
        /// <param name="wakeSignal">Returns early if this is set to 1</param>
        public static void SpinWaitUntilTimePoint(long timePoint, ref long wakeSignal)
        {
            SpinWait spinWait = new();

            while (Interlocked.Read(ref wakeSignal) != 1 && PerformanceCounter.ElapsedTicks < timePoint)
            {
                // Our time is close - don't let SpinWait go off and potentially Thread.Sleep().
                if (spinWait.NextSpinWillYield)
                {
                    Thread.Yield();

                    spinWait.Reset();
                }
                else
                {
                    spinWait.SpinOnce();
                }
            }
        }

        /// <summary>
        /// Spinwait until the given timepoint, with no opportunity to wake early.
        /// </summary>
        /// <param name="timePoint">Target timepoint in host ticks</param>
        public static void SpinWaitUntilTimePoint(long timePoint)
        {
            while (PerformanceCounter.ElapsedTicks < timePoint)
            {
                Thread.SpinWait(5);
            }
        }
    }
}