diff options
Diffstat (limited to 'src/Ryujinx.Common/PreciseSleep/SleepEvent.cs')
-rw-r--r-- | src/Ryujinx.Common/PreciseSleep/SleepEvent.cs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Ryujinx.Common/PreciseSleep/SleepEvent.cs b/src/Ryujinx.Common/PreciseSleep/SleepEvent.cs new file mode 100644 index 00000000..f0769d1e --- /dev/null +++ b/src/Ryujinx.Common/PreciseSleep/SleepEvent.cs @@ -0,0 +1,51 @@ +using System; +using System.Threading; + +namespace Ryujinx.Common.PreciseSleep +{ + /// <summary> + /// A cross-platform precise sleep event that has millisecond granularity. + /// </summary> + internal class SleepEvent : IPreciseSleepEvent + { + private readonly AutoResetEvent _waitEvent = new(false); + + public long AdjustTimePoint(long timePoint, long timeoutNs) + { + // No adjustment + return timePoint; + } + + public bool SleepUntil(long timePoint) + { + long now = PerformanceCounter.ElapsedTicks; + long ms = Math.Min((timePoint - now) / PerformanceCounter.TicksPerMillisecond, int.MaxValue); + + if (ms > 0) + { + _waitEvent.WaitOne((int)ms); + + return true; + } + + return false; + } + + public void Sleep() + { + _waitEvent.WaitOne(); + } + + public void Signal() + { + _waitEvent.Set(); + } + + public void Dispose() + { + GC.SuppressFinalize(this); + + _waitEvent.Dispose(); + } + } +} |