Archived
1
0
Fork 0
forked from Mirror/Ryujinx
This repository has been archived on 2024-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
jinx/Ryujinx.Core/OsHle/Mutex.cs

122 lines
3.1 KiB
C#
Raw Normal View History

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