Ryujinx/Ryujinx.HLE/HOS/Kernel/KTimeManager.cs
gdkchan 00579927e4
Better process implementation (#491)
* Initial implementation of KProcess

* Some improvements to the memory manager, implement back guest stack trace printing

* Better GetInfo implementation, improve checking in some places with information from process capabilities

* Allow the cpu to read/write from the correct memory locations for accesses crossing a page boundary

* Change long -> ulong for address/size on memory related methods to avoid unnecessary casts

* Attempt at implementing ldr:ro with new KProcess

* Allow BSS with size 0 on ldr:ro

* Add checking for memory block slab heap usage, return errors if full, exit gracefully

* Use KMemoryBlockSize const from KMemoryManager

* Allow all methods to read from non-contiguous locations

* Fix for TransactParcelAuto

* Address PR feedback, additionally fix some small issues related to the KIP loader and implement SVCs GetProcessId, GetProcessList, GetSystemInfo, CreatePort and ManageNamedPort

* Fix wrong check for source pages count from page list on MapPhysicalMemory

* Fix some issues with UnloadNro on ldr:ro
2018-11-28 20:18:09 -02:00

143 lines
3.7 KiB
C#

using Ryujinx.Common;
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)
{
long TimePoint = PerformanceCounter.ElapsedMilliseconds + ConvertNanosecondsToMilliseconds(Timeout);
lock (WaitingObjects)
{
WaitingObjects.Add(new WaitingObject(Object, TimePoint));
}
WaitEvent.Set();
}
public static long ConvertNanosecondsToMilliseconds(long Time)
{
Time /= 1000000;
if ((ulong)Time > int.MaxValue)
{
return int.MaxValue;
}
return Time;
}
public static long ConvertMillisecondsToNanoseconds(long Time)
{
return Time * 1000000;
}
public static long ConvertMillisecondsToTicks(long Time)
{
return Time * 19200;
}
public void UnscheduleFutureInvocation(IKFutureSchedulerObject Object)
{
lock (WaitingObjects)
{
WaitingObjects.RemoveAll(x => x.Object == Object);
}
}
private void WaitAndCheckScheduledObjects()
{
using (WaitEvent = new AutoResetEvent(false))
{
while (KeepRunning)
{
WaitingObject Next;
lock (WaitingObjects)
{
Next = WaitingObjects.OrderBy(x => x.TimePoint).FirstOrDefault();
}
if (Next != null)
{
long TimePoint = PerformanceCounter.ElapsedMilliseconds;
if (Next.TimePoint > TimePoint)
{
WaitEvent.WaitOne((int)(Next.TimePoint - TimePoint));
}
bool TimeUp = PerformanceCounter.ElapsedMilliseconds >= Next.TimePoint;
if (TimeUp)
{
lock (WaitingObjects)
{
TimeUp = WaitingObjects.Remove(Next);
}
}
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();
}
}
}
}