KTimeManager.cs 3.8 KB

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