KTimeManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. 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. public KTimeManager(KernelContext context)
  25. {
  26. _context = context;
  27. _waitingObjects = new List<WaitingObject>();
  28. _keepRunning = true;
  29. Thread work = new Thread(WaitAndCheckScheduledObjects)
  30. {
  31. Name = "HLE.TimeManager"
  32. };
  33. work.Start();
  34. }
  35. public void ScheduleFutureInvocation(IKFutureSchedulerObject schedulerObj, long timeout)
  36. {
  37. long timePoint = PerformanceCounter.ElapsedMilliseconds + ConvertNanosecondsToMilliseconds(timeout);
  38. lock (_context.CriticalSection.Lock)
  39. {
  40. _waitingObjects.Add(new WaitingObject(schedulerObj, timePoint));
  41. }
  42. _waitEvent.Set();
  43. }
  44. public void UnscheduleFutureInvocation(IKFutureSchedulerObject schedulerObj)
  45. {
  46. lock (_context.CriticalSection.Lock)
  47. {
  48. _waitingObjects.RemoveAll(x => x.Object == schedulerObj);
  49. }
  50. }
  51. private void WaitAndCheckScheduledObjects()
  52. {
  53. using (_waitEvent = new AutoResetEvent(false))
  54. {
  55. while (_keepRunning)
  56. {
  57. WaitingObject next;
  58. lock (_context.CriticalSection.Lock)
  59. {
  60. next = _waitingObjects.OrderBy(x => x.TimePoint).FirstOrDefault();
  61. }
  62. if (next != null)
  63. {
  64. long timePoint = PerformanceCounter.ElapsedMilliseconds;
  65. if (next.TimePoint > timePoint)
  66. {
  67. _waitEvent.WaitOne((int)(next.TimePoint - timePoint));
  68. }
  69. bool timeUp = PerformanceCounter.ElapsedMilliseconds >= next.TimePoint;
  70. if (timeUp)
  71. {
  72. lock (_context.CriticalSection.Lock)
  73. {
  74. if (_waitingObjects.Remove(next))
  75. {
  76. next.Object.TimeUp();
  77. }
  78. }
  79. }
  80. }
  81. else
  82. {
  83. _waitEvent.WaitOne();
  84. }
  85. }
  86. }
  87. }
  88. public static long ConvertNanosecondsToMilliseconds(long time)
  89. {
  90. time /= 1000000;
  91. if ((ulong)time > int.MaxValue)
  92. {
  93. return int.MaxValue;
  94. }
  95. return time;
  96. }
  97. public static long ConvertMillisecondsToNanoseconds(long time)
  98. {
  99. return time * 1000000;
  100. }
  101. public static long ConvertHostTicksToTicks(long time)
  102. {
  103. return (long)((time / (double)PerformanceCounter.TicksPerSecond) * 19200000.0);
  104. }
  105. public void Dispose()
  106. {
  107. _keepRunning = false;
  108. _waitEvent?.Set();
  109. }
  110. }
  111. }