KTimeManager.cs 7.1 KB

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