2018-02-20 15:09:23 -05:00
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
2018-02-13 21:43:08 -05:00
|
|
|
using System.Collections.Concurrent;
|
2018-02-19 14:37:13 -05:00
|
|
|
using System.Threading;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-20 15:09:23 -05:00
|
|
|
namespace Ryujinx.Core.OsHle
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-20 15:09:23 -05:00
|
|
|
public class Mutex
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
private const int MutexHasListenersMask = 0x40000000;
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
private Process Process;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
private long MutexAddress;
|
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
private bool OwnsMutexValue;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private object EnterWaitLock;
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
private ConcurrentQueue<HThread> WaitingThreads;
|
|
|
|
|
|
|
|
public Mutex(Process Process, long MutexAddress, int OwnerThreadHandle)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-13 21:43:08 -05:00
|
|
|
this.Process = Process;
|
2018-02-04 18:08:20 -05:00
|
|
|
this.MutexAddress = MutexAddress;
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
//Process.Memory.WriteInt32(MutexAddress, OwnerThreadHandle);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
EnterWaitLock = new object();
|
2018-02-13 21:43:08 -05:00
|
|
|
|
|
|
|
WaitingThreads = new ConcurrentQueue<HThread>();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
public void WaitForLock(HThread RequestingThread, int RequestingThreadHandle)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
AcquireMutexValue();
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
lock (EnterWaitLock)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
int CurrentThreadHandle = Process.Memory.ReadInt32(MutexAddress) & ~MutexHasListenersMask;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (CurrentThreadHandle == RequestingThreadHandle ||
|
|
|
|
CurrentThreadHandle == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
Process.Memory.WriteInt32(MutexAddress, CurrentThreadHandle | MutexHasListenersMask);
|
|
|
|
|
|
|
|
ReleaseMutexValue();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
WaitingThreads.Enqueue(RequestingThread);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
Process.Scheduler.WaitForSignal(RequestingThread);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void GiveUpLock(int ThreadHandle)
|
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
AcquireMutexValue();
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
lock (EnterWaitLock)
|
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
int CurrentThread = Process.Memory.ReadInt32(MutexAddress) & ~MutexHasListenersMask;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (CurrentThread == ThreadHandle)
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 14:37:13 -05:00
|
|
|
|
|
|
|
ReleaseMutexValue();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Unlock()
|
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
AcquireMutexValue();
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
lock (EnterWaitLock)
|
|
|
|
{
|
2018-02-13 21:43:08 -05:00
|
|
|
int HasListeners = WaitingThreads.Count > 1 ? MutexHasListenersMask : 0;
|
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
Process.Memory.WriteInt32(MutexAddress, HasListeners);
|
|
|
|
|
|
|
|
ReleaseMutexValue();
|
2018-02-13 21:43:08 -05:00
|
|
|
|
|
|
|
HThread[] UnlockedThreads = new HThread[WaitingThreads.Count];
|
|
|
|
|
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
while (WaitingThreads.TryDequeue(out HThread Thread))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-13 21:43:08 -05:00
|
|
|
UnlockedThreads[Index++] = Thread;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
Process.Scheduler.Signal(UnlockedThreads);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
2018-02-13 21:43:08 -05:00
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
private void AcquireMutexValue()
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
if (!OwnsMutexValue)
|
|
|
|
{
|
|
|
|
while (!Process.Memory.AcquireAddress(MutexAddress))
|
|
|
|
{
|
|
|
|
Thread.Yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnsMutexValue = true;
|
|
|
|
}
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
|
2018-02-19 14:37:13 -05:00
|
|
|
private void ReleaseMutexValue()
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-02-19 14:37:13 -05:00
|
|
|
if (OwnsMutexValue)
|
|
|
|
{
|
|
|
|
OwnsMutexValue = false;
|
|
|
|
|
|
|
|
Process.Memory.ReleaseAddress(MutexAddress);
|
|
|
|
}
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|