2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-13 21:43:08 -05:00
|
|
|
using Ryujinx.OsHle.Handles;
|
|
|
|
using System.Collections.Concurrent;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.OsHle
|
|
|
|
{
|
|
|
|
class Mutex
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
lock (EnterWaitLock)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
|
|
|
int CurrentThreadHandle = ReadMutexValue() & ~MutexHasListenersMask;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (CurrentThreadHandle == RequestingThreadHandle ||
|
|
|
|
CurrentThreadHandle == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:08 -05:00
|
|
|
WriteMutexValue(CurrentThreadHandle | MutexHasListenersMask);
|
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)
|
|
|
|
{
|
|
|
|
lock (EnterWaitLock)
|
|
|
|
{
|
2018-02-13 21:43:08 -05:00
|
|
|
int CurrentThread = ReadMutexValue() & ~MutexHasListenersMask;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (CurrentThread == ThreadHandle)
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unlock()
|
|
|
|
{
|
|
|
|
lock (EnterWaitLock)
|
|
|
|
{
|
2018-02-13 21:43:08 -05:00
|
|
|
int HasListeners = WaitingThreads.Count > 1 ? MutexHasListenersMask : 0;
|
|
|
|
|
|
|
|
WriteMutexValue(HasListeners);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
private int ReadMutexValue()
|
|
|
|
{
|
|
|
|
return AMemoryHelper.ReadInt32Exclusive(Process.Memory, MutexAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteMutexValue(int Value)
|
|
|
|
{
|
|
|
|
AMemoryHelper.WriteInt32Exclusive(Process.Memory, MutexAddress, Value);
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|