KTimeManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Ryujinx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace Ryujinx.HLE.HOS.Kernel.Common
  7. {
  8. class KTimeManager : IDisposable
  9. {
  10. public static readonly long DefaultTimeIncrementNanoseconds = ConvertGuestTicksToNanoseconds(2);
  11. private class WaitingObject
  12. {
  13. public IKFutureSchedulerObject Object { get; }
  14. public long TimePoint { get; }
  15. public WaitingObject(IKFutureSchedulerObject schedulerObj, long timePoint)
  16. {
  17. Object = schedulerObj;
  18. TimePoint = timePoint;
  19. }
  20. }
  21. private readonly KernelContext _context;
  22. private readonly List<WaitingObject> _waitingObjects;
  23. private AutoResetEvent _waitEvent;
  24. private bool _keepRunning;
  25. private long _enforceWakeupFromSpinWait;
  26. public KTimeManager(KernelContext context)
  27. {
  28. _context = context;
  29. _waitingObjects = new List<WaitingObject>();
  30. _keepRunning = true;
  31. Thread work = new Thread(WaitAndCheckScheduledObjects)
  32. {
  33. Name = "HLE.TimeManager"
  34. };
  35. work.Start();
  36. }
  37. public void ScheduleFutureInvocation(IKFutureSchedulerObject schedulerObj, long timeout)
  38. {
  39. long startTime = PerformanceCounter.ElapsedTicks;
  40. long timePoint = startTime + ConvertNanosecondsToHostTicks(timeout);
  41. if (timePoint < startTime)
  42. {
  43. timePoint = long.MaxValue;
  44. }
  45. lock (_context.CriticalSection.Lock)
  46. {
  47. _waitingObjects.Add(new WaitingObject(schedulerObj, timePoint));
  48. if (timeout < 1000000)
  49. {
  50. Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 1);
  51. }
  52. }
  53. _waitEvent.Set();
  54. }
  55. public void UnscheduleFutureInvocation(IKFutureSchedulerObject schedulerObj)
  56. {
  57. lock (_context.CriticalSection.Lock)
  58. {
  59. _waitingObjects.RemoveAll(x => x.Object == schedulerObj);
  60. }
  61. }
  62. private void WaitAndCheckScheduledObjects()
  63. {
  64. SpinWait spinWait = new SpinWait();
  65. WaitingObject next;
  66. using (_waitEvent = new AutoResetEvent(false))
  67. {
  68. while (_keepRunning)
  69. {
  70. lock (_context.CriticalSection.Lock)
  71. {
  72. Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 0);
  73. next = _waitingObjects.OrderBy(x => x.TimePoint).FirstOrDefault();
  74. }
  75. if (next != null)
  76. {
  77. long timePoint = PerformanceCounter.ElapsedTicks;
  78. if (next.TimePoint > timePoint)
  79. {
  80. long ms = Math.Min((next.TimePoint - timePoint) / PerformanceCounter.TicksPerMillisecond, int.MaxValue);
  81. if (ms > 0)
  82. {
  83. _waitEvent.WaitOne((int)ms);
  84. }
  85. else
  86. {
  87. while (Interlocked.Read(ref _enforceWakeupFromSpinWait) != 1 && PerformanceCounter.ElapsedTicks <= next.TimePoint)
  88. {
  89. if (spinWait.NextSpinWillYield)
  90. {
  91. Thread.Yield();
  92. spinWait.Reset();
  93. }
  94. spinWait.SpinOnce();
  95. }
  96. spinWait.Reset();
  97. }
  98. }
  99. bool timeUp = PerformanceCounter.ElapsedTicks >= next.TimePoint;
  100. if (timeUp)
  101. {
  102. lock (_context.CriticalSection.Lock)
  103. {
  104. if (_waitingObjects.Remove(next))
  105. {
  106. next.Object.TimeUp();
  107. }
  108. }
  109. }
  110. }
  111. else
  112. {
  113. _waitEvent.WaitOne();
  114. }
  115. }
  116. }
  117. }
  118. public static long ConvertNanosecondsToMilliseconds(long time)
  119. {
  120. time /= 1000000;
  121. if ((ulong)time > int.MaxValue)
  122. {
  123. return int.MaxValue;
  124. }
  125. return time;
  126. }
  127. public static long ConvertMillisecondsToNanoseconds(long time)
  128. {
  129. return time * 1000000;
  130. }
  131. public static long ConvertNanosecondsToHostTicks(long ns)
  132. {
  133. long nsDiv = ns / 1000000000;
  134. long nsMod = ns % 1000000000;
  135. long tickDiv = PerformanceCounter.TicksPerSecond / 1000000000;
  136. long tickMod = PerformanceCounter.TicksPerSecond % 1000000000;
  137. long baseTicks = (nsMod * tickMod + PerformanceCounter.TicksPerSecond - 1) / 1000000000;
  138. return (nsDiv * tickDiv) * 1000000000 + nsDiv * tickMod + nsMod * tickDiv + baseTicks;
  139. }
  140. public static long ConvertGuestTicksToNanoseconds(long ticks)
  141. {
  142. return (long)Math.Ceiling(ticks * (1000000000.0 / 19200000.0));
  143. }
  144. public static long ConvertHostTicksToTicks(long time)
  145. {
  146. return (long)((time / (double)PerformanceCounter.TicksPerSecond) * 19200000.0);
  147. }
  148. public void Dispose()
  149. {
  150. _keepRunning = false;
  151. _waitEvent?.Set();
  152. }
  153. }
  154. }