KSynchronization.cs 4.0 KB

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