KTimeManager.cs 3.7 KB

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