KSynchronization.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections.Generic;
  2. using static Ryujinx.HLE.HOS.ErrorCode;
  3. namespace Ryujinx.HLE.HOS.Kernel
  4. {
  5. class KSynchronization
  6. {
  7. private Horizon _system;
  8. public KSynchronization(Horizon system)
  9. {
  10. _system = system;
  11. }
  12. public long WaitFor(KSynchronizationObject[] syncObjs, long timeout, ref int hndIndex)
  13. {
  14. long result = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
  15. _system.CriticalSection.Enter();
  16. //Check if objects are already signaled before waiting.
  17. for (int index = 0; index < syncObjs.Length; index++)
  18. {
  19. if (!syncObjs[index].IsSignaled())
  20. {
  21. continue;
  22. }
  23. hndIndex = index;
  24. _system.CriticalSection.Leave();
  25. return 0;
  26. }
  27. if (timeout == 0)
  28. {
  29. _system.CriticalSection.Leave();
  30. return result;
  31. }
  32. KThread currentThread = _system.Scheduler.GetCurrentThread();
  33. if (currentThread.ShallBeTerminated ||
  34. currentThread.SchedFlags == ThreadSchedState.TerminationPending)
  35. {
  36. result = MakeError(ErrorModule.Kernel, KernelErr.ThreadTerminating);
  37. }
  38. else if (currentThread.SyncCancelled)
  39. {
  40. currentThread.SyncCancelled = false;
  41. result = MakeError(ErrorModule.Kernel, KernelErr.Cancelled);
  42. }
  43. else
  44. {
  45. LinkedListNode<KThread>[] syncNodes = new LinkedListNode<KThread>[syncObjs.Length];
  46. for (int index = 0; index < syncObjs.Length; index++)
  47. {
  48. syncNodes[index] = syncObjs[index].AddWaitingThread(currentThread);
  49. }
  50. currentThread.WaitingSync = true;
  51. currentThread.SignaledObj = null;
  52. currentThread.ObjSyncResult = (int)result;
  53. currentThread.Reschedule(ThreadSchedState.Paused);
  54. if (timeout > 0)
  55. {
  56. _system.TimeManager.ScheduleFutureInvocation(currentThread, timeout);
  57. }
  58. _system.CriticalSection.Leave();
  59. currentThread.WaitingSync = false;
  60. if (timeout > 0)
  61. {
  62. _system.TimeManager.UnscheduleFutureInvocation(currentThread);
  63. }
  64. _system.CriticalSection.Enter();
  65. result = (uint)currentThread.ObjSyncResult;
  66. hndIndex = -1;
  67. for (int index = 0; index < syncObjs.Length; index++)
  68. {
  69. syncObjs[index].RemoveWaitingThread(syncNodes[index]);
  70. if (syncObjs[index] == currentThread.SignaledObj)
  71. {
  72. hndIndex = index;
  73. }
  74. }
  75. }
  76. _system.CriticalSection.Leave();
  77. return result;
  78. }
  79. public void SignalObject(KSynchronizationObject syncObj)
  80. {
  81. _system.CriticalSection.Enter();
  82. if (syncObj.IsSignaled())
  83. {
  84. LinkedListNode<KThread> node = syncObj.WaitingThreads.First;
  85. while (node != null)
  86. {
  87. KThread thread = node.Value;
  88. if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
  89. {
  90. thread.SignaledObj = syncObj;
  91. thread.ObjSyncResult = 0;
  92. thread.Reschedule(ThreadSchedState.Running);
  93. }
  94. node = node.Next;
  95. }
  96. }
  97. _system.CriticalSection.Leave();
  98. }
  99. }
  100. }