KSynchronization.cs 3.9 KB

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