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-05 00:52:39 +00:00
|
|
|
public WaitingObject(IKFutureSchedulerObject Object, long TimePoint)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
this.Object = Object;
|
|
|
|
this.TimePoint = TimePoint;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private List<WaitingObject> WaitingObjects;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private AutoResetEvent WaitEvent;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private bool KeepRunning;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
|
|
|
public KTimeManager()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitingObjects = new List<WaitingObject>();
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KeepRunning = true;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Thread Work = new Thread(WaitAndCheckScheduledObjects);
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Work.Start();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public void ScheduleFutureInvocation(IKFutureSchedulerObject Object, long Timeout)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
long TimePoint = PerformanceCounter.ElapsedMilliseconds + ConvertNanosecondsToMilliseconds(Timeout);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (WaitingObjects)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitingObjects.Add(new WaitingObject(Object, TimePoint));
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitEvent.Set();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public static long ConvertNanosecondsToMilliseconds(long Time)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Time /= 1000000;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((ulong)Time > int.MaxValue)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
|
|
|
return int.MaxValue;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Time;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public static long ConvertMillisecondsToNanoseconds(long Time)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Time * 1000000;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public static long ConvertMillisecondsToTicks(long Time)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Time * 19200;
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void UnscheduleFutureInvocation(IKFutureSchedulerObject Object)
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (WaitingObjects)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitingObjects.RemoveAll(x => x.Object == Object);
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WaitAndCheckScheduledObjects()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
using (WaitEvent = new AutoResetEvent(false))
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
while (KeepRunning)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitingObject Next;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (WaitingObjects)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +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-05 00:52:39 +00:00
|
|
|
if (Next != null)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
long TimePoint = PerformanceCounter.ElapsedMilliseconds;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Next.TimePoint > TimePoint)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitEvent.WaitOne((int)(Next.TimePoint - TimePoint));
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
bool TimeUp = PerformanceCounter.ElapsedMilliseconds >= Next.TimePoint;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (TimeUp)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (WaitingObjects)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
TimeUp = WaitingObjects.Remove(Next);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (TimeUp)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Next.Object.TimeUp();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitEvent.WaitOne();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
protected virtual void Dispose(bool Disposing)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Disposing)
|
2018-09-18 23:36:43 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KeepRunning = false;
|
2018-09-18 23:36:43 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
WaitEvent?.Set();
|
2018-09-18 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|