| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using Ryujinx.Common;
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace Ryujinx.HLE.HOS.Kernel.Common
- {
- class KTimeManager : IDisposable
- {
- public static readonly long DefaultTimeIncrementNanoseconds = ConvertGuestTicksToNanoseconds(2);
- private class WaitingObject
- {
- public IKFutureSchedulerObject Object { get; }
- public long TimePoint { get; }
- public WaitingObject(IKFutureSchedulerObject schedulerObj, long timePoint)
- {
- Object = schedulerObj;
- TimePoint = timePoint;
- }
- }
- private readonly KernelContext _context;
- private readonly List<WaitingObject> _waitingObjects;
- private AutoResetEvent _waitEvent;
- private bool _keepRunning;
- private long _enforceWakeupFromSpinWait;
- public KTimeManager(KernelContext context)
- {
- _context = context;
- _waitingObjects = new List<WaitingObject>();
- _keepRunning = true;
- Thread work = new Thread(WaitAndCheckScheduledObjects)
- {
- Name = "HLE.TimeManager"
- };
- work.Start();
- }
- public void ScheduleFutureInvocation(IKFutureSchedulerObject schedulerObj, long timeout)
- {
- long startTime = PerformanceCounter.ElapsedTicks;
- long timePoint = startTime + ConvertNanosecondsToHostTicks(timeout);
- if (timePoint < startTime)
- {
- timePoint = long.MaxValue;
- }
- lock (_context.CriticalSection.Lock)
- {
- _waitingObjects.Add(new WaitingObject(schedulerObj, timePoint));
- if (timeout < 1000000)
- {
- Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 1);
- }
- }
- _waitEvent.Set();
- }
- public void UnscheduleFutureInvocation(IKFutureSchedulerObject schedulerObj)
- {
- lock (_context.CriticalSection.Lock)
- {
- _waitingObjects.RemoveAll(x => x.Object == schedulerObj);
- }
- }
- private void WaitAndCheckScheduledObjects()
- {
- SpinWait spinWait = new SpinWait();
- WaitingObject next;
- using (_waitEvent = new AutoResetEvent(false))
- {
- while (_keepRunning)
- {
- lock (_context.CriticalSection.Lock)
- {
- Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 0);
- next = GetNextWaitingObject();
- }
- if (next != null)
- {
- long timePoint = PerformanceCounter.ElapsedTicks;
- if (next.TimePoint > timePoint)
- {
- 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();
- }
- }
- bool timeUp = PerformanceCounter.ElapsedTicks >= next.TimePoint;
- if (timeUp)
- {
- lock (_context.CriticalSection.Lock)
- {
- if (_waitingObjects.Remove(next))
- {
- next.Object.TimeUp();
- }
- }
- }
- }
- else
- {
- _waitEvent.WaitOne();
- }
- }
- }
- }
- 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;
- }
- 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 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));
- }
- public static long ConvertHostTicksToTicks(long time)
- {
- return (long)((time / (double)PerformanceCounter.TicksPerSecond) * 19200000.0);
- }
- public void Dispose()
- {
- _keepRunning = false;
- _waitEvent?.Set();
- }
- }
- }
|