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