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.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KTimeManager : IDisposable
|
|
|
|
{
|
|
|
|
private class WaitingObject
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
public IKFutureSchedulerObject Object { get; private set; }
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public long TimePoint { get; private set; }
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private List<WaitingObject> _waitingObjects;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private AutoResetEvent _waitEvent;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private bool _keepRunning;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
public KTimeManager()
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitingObjects = new List<WaitingObject>();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_keepRunning = true;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
Thread work = new Thread(WaitAndCheckScheduledObjects);
|
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
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long timePoint = PerformanceCounter.ElapsedMilliseconds + ConvertNanosecondsToMilliseconds(timeout);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_waitingObjects)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitingObjects.Add(new WaitingObject(schedulerObj, timePoint));
|
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
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public static long ConvertNanosecondsToMilliseconds(long time)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
time /= 1000000;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((ulong)time > int.MaxValue)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return int.MaxValue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return time;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public static long ConvertMillisecondsToNanoseconds(long time)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return time * 1000000;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public static long ConvertMillisecondsToTicks(long time)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
return time * 19200;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void UnscheduleFutureInvocation(IKFutureSchedulerObject Object)
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_waitingObjects)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitingObjects.RemoveAll(x => x.Object == Object);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WaitAndCheckScheduledObjects()
|
|
|
|
{
|
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
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
WaitingObject next;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_waitingObjects)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
next = _waitingObjects.OrderBy(x => x.TimePoint).FirstOrDefault();
|
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
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long timePoint = PerformanceCounter.ElapsedMilliseconds;
|
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
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_waitEvent.WaitOne((int)(next.TimePoint - timePoint));
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
bool timeUp = PerformanceCounter.ElapsedMilliseconds >= 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
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_waitingObjects)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
timeUp = _waitingObjects.Remove(next);
|
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 (timeUp)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
next.Object.TimeUp();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
protected virtual void Dispose(bool disposing)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
if (disposing)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_keepRunning = false;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|