2018-11-28 22:18:09 +00:00
|
|
|
using Ryujinx.Common;
|
2018-09-18 23:36:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Common
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
class KTimeManager : IDisposable
|
|
|
|
{
|
2021-11-28 12:15:26 +00:00
|
|
|
public static readonly long DefaultTimeIncrementNanoseconds = ConvertGuestTicksToNanoseconds(2);
|
|
|
|
|
2018-09-18 23:36:43 +00:00
|
|
|
private class WaitingObject
|
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
public IKFutureSchedulerObject Object { get; }
|
|
|
|
public long TimePoint { get; }
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public WaitingObject(IKFutureSchedulerObject schedulerObj, long timePoint)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Object = schedulerObj;
|
|
|
|
TimePoint = timePoint;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
private readonly KernelContext _context;
|
|
|
|
private readonly List<WaitingObject> _waitingObjects;
|
2018-12-06 11:16:24 +00:00
|
|
|
private AutoResetEvent _waitEvent;
|
|
|
|
private bool _keepRunning;
|
2021-11-28 12:15:26 +00:00
|
|
|
private long _enforceWakeupFromSpinWait;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public KTimeManager(KernelContext context)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
_context = context;
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitingObjects = new List<WaitingObject>();
|
|
|
|
_keepRunning = true;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2020-01-13 00:21:54 +00:00
|
|
|
Thread work = new Thread(WaitAndCheckScheduledObjects)
|
|
|
|
{
|
|
|
|
Name = "HLE.TimeManager"
|
|
|
|
};
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
work.Start();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public void ScheduleFutureInvocation(IKFutureSchedulerObject schedulerObj, long timeout)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2023-01-05 00:55:27 +00:00
|
|
|
long startTime = PerformanceCounter.ElapsedTicks;
|
|
|
|
long timePoint = startTime + ConvertNanosecondsToHostTicks(timeout);
|
|
|
|
|
|
|
|
if (timePoint < startTime)
|
|
|
|
{
|
|
|
|
timePoint = long.MaxValue;
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
lock (_context.CriticalSection.Lock)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitingObjects.Add(new WaitingObject(schedulerObj, timePoint));
|
2021-11-28 12:15:26 +00:00
|
|
|
|
|
|
|
if (timeout < 1000000)
|
|
|
|
{
|
|
|
|
Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 1);
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitEvent.Set();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public void UnscheduleFutureInvocation(IKFutureSchedulerObject schedulerObj)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
lock (_context.CriticalSection.Lock)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
_waitingObjects.RemoveAll(x => x.Object == schedulerObj);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WaitAndCheckScheduledObjects()
|
|
|
|
{
|
2021-11-28 12:15:26 +00:00
|
|
|
SpinWait spinWait = new SpinWait();
|
|
|
|
WaitingObject next;
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
using (_waitEvent = new AutoResetEvent(false))
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
while (_keepRunning)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
lock (_context.CriticalSection.Lock)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2021-11-28 12:15:26 +00:00
|
|
|
Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 0);
|
|
|
|
|
2023-03-11 20:05:48 +00:00
|
|
|
next = GetNextWaitingObject();
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (next != null)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2021-11-28 12:15:26 +00:00
|
|
|
long timePoint = PerformanceCounter.ElapsedTicks;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (next.TimePoint > timePoint)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2021-11-28 12:15:26 +00:00
|
|
|
long ms = Math.Min((next.TimePoint - timePoint) / PerformanceCounter.TicksPerMillisecond, int.MaxValue);
|
|
|
|
|
|
|
|
if (ms > 0)
|
|
|
|
{
|
|
|
|
_waitEvent.WaitOne((int)ms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Interlocked.Read(ref _enforceWakeupFromSpinWait) != 1 && PerformanceCounter.ElapsedTicks <= next.TimePoint)
|
|
|
|
{
|
|
|
|
if (spinWait.NextSpinWillYield)
|
|
|
|
{
|
|
|
|
Thread.Yield();
|
|
|
|
|
|
|
|
spinWait.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
spinWait.SpinOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
spinWait.Reset();
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 12:15:26 +00:00
|
|
|
bool timeUp = PerformanceCounter.ElapsedTicks >= next.TimePoint;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (timeUp)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
lock (_context.CriticalSection.Lock)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
if (_waitingObjects.Remove(next))
|
|
|
|
{
|
|
|
|
next.Object.TimeUp();
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitEvent.WaitOne();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 20:05:48 +00:00
|
|
|
private WaitingObject GetNextWaitingObject()
|
|
|
|
{
|
|
|
|
WaitingObject selected = null;
|
|
|
|
|
|
|
|
long lowestTimePoint = long.MaxValue;
|
|
|
|
|
|
|
|
for (int index = _waitingObjects.Count - 1; index >= 0; index--)
|
|
|
|
{
|
|
|
|
WaitingObject current = _waitingObjects[index];
|
|
|
|
|
|
|
|
if (current.TimePoint <= lowestTimePoint)
|
|
|
|
{
|
|
|
|
selected = current;
|
|
|
|
lowestTimePoint = current.TimePoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public static long ConvertNanosecondsToMilliseconds(long time)
|
|
|
|
{
|
|
|
|
time /= 1000000;
|
|
|
|
|
|
|
|
if ((ulong)time > int.MaxValue)
|
|
|
|
{
|
|
|
|
return int.MaxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long ConvertMillisecondsToNanoseconds(long time)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
return time * 1000000;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 12:15:26 +00:00
|
|
|
public static long ConvertNanosecondsToHostTicks(long ns)
|
|
|
|
{
|
|
|
|
long nsDiv = ns / 1000000000;
|
|
|
|
long nsMod = ns % 1000000000;
|
|
|
|
long tickDiv = PerformanceCounter.TicksPerSecond / 1000000000;
|
|
|
|
long tickMod = PerformanceCounter.TicksPerSecond % 1000000000;
|
|
|
|
|
|
|
|
long baseTicks = (nsMod * tickMod + PerformanceCounter.TicksPerSecond - 1) / 1000000000;
|
|
|
|
return (nsDiv * tickDiv) * 1000000000 + nsDiv * tickMod + nsMod * tickDiv + baseTicks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long ConvertGuestTicksToNanoseconds(long ticks)
|
|
|
|
{
|
|
|
|
return (long)Math.Ceiling(ticks * (1000000000.0 / 19200000.0));
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public static long ConvertHostTicksToTicks(long time)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
return (long)((time / (double)PerformanceCounter.TicksPerSecond) * 19200000.0);
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2020-12-09 22:20:05 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_keepRunning = false;
|
|
|
|
_waitEvent?.Set();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|