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
  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 Object, long TimePoint)
  15. {
  16. this.Object = Object;
  17. this.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 Object, long Timeout)
  31. {
  32. long TimePoint = PerformanceCounter.ElapsedMilliseconds + ConvertNanosecondsToMilliseconds(Timeout);
  33. lock (WaitingObjects)
  34. {
  35. WaitingObjects.Add(new WaitingObject(Object, 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. }