KTimeManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. public KTimeManager(KernelContext context)
  26. {
  27. _context = context;
  28. _waitingObjects = new List<WaitingObject>();
  29. _keepRunning = true;
  30. Thread work = new Thread(WaitAndCheckScheduledObjects)
  31. {
  32. Name = "HLE.TimeManager"
  33. };
  34. work.Start();
  35. }
  36. public void ScheduleFutureInvocation(IKFutureSchedulerObject schedulerObj, long timeout)
  37. {
  38. long startTime = PerformanceCounter.ElapsedTicks;
  39. long timePoint = startTime + ConvertNanosecondsToHostTicks(timeout);
  40. if (timePoint < startTime)
  41. {
  42. timePoint = long.MaxValue;
  43. }
  44. lock (_context.CriticalSection.Lock)
  45. {
  46. _waitingObjects.Add(new WaitingObject(schedulerObj, timePoint));
  47. if (timeout < 1000000)
  48. {
  49. Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 1);
  50. }
  51. }
  52. _waitEvent.Set();
  53. }
  54. public void UnscheduleFutureInvocation(IKFutureSchedulerObject schedulerObj)
  55. {
  56. lock (_context.CriticalSection.Lock)
  57. {
  58. _waitingObjects.RemoveAll(x => x.Object == schedulerObj);
  59. }
  60. }
  61. private void WaitAndCheckScheduledObjects()
  62. {
  63. SpinWait spinWait = new SpinWait();
  64. WaitingObject next;
  65. using (_waitEvent = new AutoResetEvent(false))
  66. {
  67. while (_keepRunning)
  68. {
  69. lock (_context.CriticalSection.Lock)
  70. {
  71. Interlocked.Exchange(ref _enforceWakeupFromSpinWait, 0);
  72. next = GetNextWaitingObject();
  73. }
  74. if (next != null)
  75. {
  76. long timePoint = PerformanceCounter.ElapsedTicks;
  77. if (next.TimePoint > timePoint)
  78. {
  79. long ms = Math.Min((next.TimePoint - timePoint) / PerformanceCounter.TicksPerMillisecond, int.MaxValue);
  80. if (ms > 0)
  81. {
  82. _waitEvent.WaitOne((int)ms);
  83. }
  84. else
  85. {
  86. while (Interlocked.Read(ref _enforceWakeupFromSpinWait) != 1 && PerformanceCounter.ElapsedTicks <= next.TimePoint)
  87. {
  88. if (spinWait.NextSpinWillYield)
  89. {
  90. Thread.Yield();
  91. spinWait.Reset();
  92. }
  93. spinWait.SpinOnce();
  94. }
  95. spinWait.Reset();
  96. }
  97. }
  98. bool timeUp = PerformanceCounter.ElapsedTicks >= next.TimePoint;
  99. if (timeUp)
  100. {
  101. lock (_context.CriticalSection.Lock)
  102. {
  103. if (_waitingObjects.Remove(next))
  104. {
  105. next.Object.TimeUp();
  106. }
  107. }
  108. }
  109. }
  110. else
  111. {
  112. _waitEvent.WaitOne();
  113. }
  114. }
  115. }
  116. }
  117. private WaitingObject GetNextWaitingObject()
  118. {
  119. WaitingObject selected = null;
  120. long lowestTimePoint = long.MaxValue;
  121. for (int index = _waitingObjects.Count - 1; index >= 0; index--)
  122. {
  123. WaitingObject current = _waitingObjects[index];
  124. if (current.TimePoint <= lowestTimePoint)
  125. {
  126. selected = current;
  127. lowestTimePoint = current.TimePoint;
  128. }
  129. }
  130. return selected;
  131. }
  132. public static long ConvertNanosecondsToMilliseconds(long time)
  133. {
  134. time /= 1000000;
  135. if ((ulong)time > int.MaxValue)
  136. {
  137. return int.MaxValue;
  138. }
  139. return time;
  140. }
  141. public static long ConvertMillisecondsToNanoseconds(long time)
  142. {
  143. return time * 1000000;
  144. }
  145. public static long ConvertNanosecondsToHostTicks(long ns)
  146. {
  147. long nsDiv = ns / 1000000000;
  148. long nsMod = ns % 1000000000;
  149. long tickDiv = PerformanceCounter.TicksPerSecond / 1000000000;
  150. long tickMod = PerformanceCounter.TicksPerSecond % 1000000000;
  151. long baseTicks = (nsMod * tickMod + PerformanceCounter.TicksPerSecond - 1) / 1000000000;
  152. return (nsDiv * tickDiv) * 1000000000 + nsDiv * tickMod + nsMod * tickDiv + baseTicks;
  153. }
  154. public static long ConvertGuestTicksToNanoseconds(long ticks)
  155. {
  156. return (long)Math.Ceiling(ticks * (1000000000.0 / 19200000.0));
  157. }
  158. public static long ConvertHostTicksToTicks(long time)
  159. {
  160. return (long)((time / (double)PerformanceCounter.TicksPerSecond) * 19200000.0);
  161. }
  162. public void Dispose()
  163. {
  164. _keepRunning = false;
  165. _waitEvent?.Set();
  166. }
  167. }
  168. }