2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
2018-09-18 19:36:43 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KTimeManager : IDisposable
|
|
|
|
{
|
|
|
|
private class WaitingObject
|
|
|
|
{
|
|
|
|
public IKFutureSchedulerObject Object { get; private set; }
|
|
|
|
|
|
|
|
public long TimePoint { get; private set; }
|
|
|
|
|
|
|
|
public WaitingObject(IKFutureSchedulerObject Object, long TimePoint)
|
|
|
|
{
|
|
|
|
this.Object = Object;
|
|
|
|
this.TimePoint = TimePoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<WaitingObject> WaitingObjects;
|
|
|
|
|
|
|
|
private AutoResetEvent WaitEvent;
|
|
|
|
|
|
|
|
private bool KeepRunning;
|
|
|
|
|
|
|
|
public KTimeManager()
|
|
|
|
{
|
|
|
|
WaitingObjects = new List<WaitingObject>();
|
|
|
|
|
|
|
|
KeepRunning = true;
|
|
|
|
|
|
|
|
Thread Work = new Thread(WaitAndCheckScheduledObjects);
|
|
|
|
|
|
|
|
Work.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ScheduleFutureInvocation(IKFutureSchedulerObject Object, long Timeout)
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
long TimePoint = PerformanceCounter.ElapsedMilliseconds + ConvertNanosecondsToMilliseconds(Timeout);
|
|
|
|
|
2018-09-18 19:36:43 -04:00
|
|
|
lock (WaitingObjects)
|
|
|
|
{
|
|
|
|
WaitingObjects.Add(new WaitingObject(Object, TimePoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitEvent.Set();
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public static long ConvertNanosecondsToMilliseconds(long Time)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
Time /= 1000000;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if ((ulong)Time > int.MaxValue)
|
2018-09-18 19:36:43 -04:00
|
|
|
{
|
|
|
|
return int.MaxValue;
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return Time;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long ConvertMillisecondsToNanoseconds(long Time)
|
|
|
|
{
|
|
|
|
return Time * 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long ConvertMillisecondsToTicks(long Time)
|
|
|
|
{
|
|
|
|
return Time * 19200;
|
2018-09-18 19:36:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void UnscheduleFutureInvocation(IKFutureSchedulerObject Object)
|
|
|
|
{
|
|
|
|
lock (WaitingObjects)
|
|
|
|
{
|
|
|
|
WaitingObjects.RemoveAll(x => x.Object == Object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WaitAndCheckScheduledObjects()
|
|
|
|
{
|
|
|
|
using (WaitEvent = new AutoResetEvent(false))
|
|
|
|
{
|
|
|
|
while (KeepRunning)
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
WaitingObject Next;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
lock (WaitingObjects)
|
|
|
|
{
|
|
|
|
Next = WaitingObjects.OrderBy(x => x.TimePoint).FirstOrDefault();
|
|
|
|
}
|
2018-09-18 19:36:43 -04:00
|
|
|
|
|
|
|
if (Next != null)
|
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
long TimePoint = PerformanceCounter.ElapsedMilliseconds;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
|
|
|
if (Next.TimePoint > TimePoint)
|
|
|
|
{
|
|
|
|
WaitEvent.WaitOne((int)(Next.TimePoint - TimePoint));
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
bool TimeUp = PerformanceCounter.ElapsedMilliseconds >= Next.TimePoint;
|
2018-09-18 19:36:43 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (TimeUp)
|
|
|
|
{
|
|
|
|
lock (WaitingObjects)
|
|
|
|
{
|
|
|
|
TimeUp = WaitingObjects.Remove(Next);
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 19:36:43 -04:00
|
|
|
|
|
|
|
if (TimeUp)
|
|
|
|
{
|
|
|
|
Next.Object.TimeUp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WaitEvent.WaitOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing)
|
|
|
|
{
|
|
|
|
KeepRunning = false;
|
|
|
|
|
|
|
|
WaitEvent?.Set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|