KTimeManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. _waitingObjects.RemoveAll(x => x.Object == schedulerObj);
  61. }
  62. }
  63. private void WaitAndCheckScheduledObjects()
  64. {
  65. SpinWait spinWait = new SpinWait();
  66. WaitingObject next;
  67. using (_waitEvent = new AutoResetEvent(false))
  68. {
  69. while (_keepRunning)
  70. {
  71. lock (_context.CriticalSection.Lock)
  72. {
  73. Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 0);
  74. next = GetNextWaitingObject();
  75. }
  76. if (next != null)
  77. {
  78. long timePoint = PerformanceCounter.ElapsedTicks;
  79. if (next.TimePoint > timePoint)
  80. {
  81. long ms = Math.Min((next.TimePoint - timePoint) / PerformanceCounter.TicksPerMillisecond, int.MaxValue);
  82. if (ms > 0)
  83. {
  84. _waitEvent.WaitOne((int)ms);
  85. }
  86. else
  87. {
  88. while (Interlocked.Read(ref _enforceWakeupFromSpinWait) != 1 && PerformanceCounter.ElapsedTicks <= next.TimePoint)
  89. {
  90. if (spinWait.NextSpinWillYield)
  91. {
  92. Thread.Yield();
  93. spinWait.Reset();
  94. }
  95. spinWait.SpinOnce();
  96. }
  97. spinWait.Reset();
  98. }
  99. }
  100. bool timeUp = PerformanceCounter.ElapsedTicks >= next.TimePoint;
  101. if (timeUp)
  102. {
  103. lock (_context.CriticalSection.Lock)
  104. {
  105. if (_waitingObjects.Remove(next))
  106. {
  107. next.Object.TimeUp();
  108. }
  109. }
  110. }
  111. }
  112. else
  113. {
  114. _waitEvent.WaitOne();
  115. }
  116. }
  117. }
  118. }
  119. private WaitingObject GetNextWaitingObject()
  120. {
  121. WaitingObject selected = null;
  122. long lowestTimePoint = long.MaxValue;
  123. for (int index = _waitingObjects.Count - 1; index >= 0; index--)
  124. {
  125. WaitingObject current = _waitingObjects[index];
  126. if (current.TimePoint <= lowestTimePoint)
  127. {
  128. selected = current;
  129. lowestTimePoint = current.TimePoint;
  130. }
  131. }
  132. return selected;
  133. }
  134. public static long ConvertNanosecondsToMilliseconds(long time)
  135. {
  136. time /= NanosecondsPerMillisecond;
  137. if ((ulong)time > int.MaxValue)
  138. {
  139. return int.MaxValue;
  140. }
  141. return time;
  142. }
  143. public static long ConvertMillisecondsToNanoseconds(long time)
  144. {
  145. return time * NanosecondsPerMillisecond;
  146. }
  147. public static long ConvertNanosecondsToHostTicks(long ns)
  148. {
  149. long nsDiv = ns / NanosecondsPerSecond;
  150. long nsMod = ns % NanosecondsPerSecond;
  151. long tickDiv = PerformanceCounter.TicksPerSecond / NanosecondsPerSecond;
  152. long tickMod = PerformanceCounter.TicksPerSecond % NanosecondsPerSecond;
  153. long baseTicks = (nsMod * tickMod + PerformanceCounter.TicksPerSecond - 1) / NanosecondsPerSecond;
  154. return (nsDiv * tickDiv) * NanosecondsPerSecond + nsDiv * tickMod + nsMod * tickDiv + baseTicks;
  155. }
  156. public static long ConvertGuestTicksToNanoseconds(long ticks)
  157. {
  158. return (long)Math.Ceiling(ticks * (1000000000.0 / 19200000.0));
  159. }
  160. public static long ConvertHostTicksToTicks(long time)
  161. {
  162. return (long)((time / (double)PerformanceCounter.TicksPerSecond) * 19200000.0);
  163. }
  164. public void Dispose()
  165. {
  166. _keepRunning = false;
  167. _waitEvent?.Set();
  168. }
  169. }
  170. }