ryujinx/Ryujinx.HLE/HOS/Services/Nv/NvHostCtrl/NvHostSyncPt.cs
gdkchan 521751795a
Code style fixes and nits on the HLE project (#355)
* Some style fixes and nits on ITimeZoneService

* Remove some unneeded usings

* Remove the Ryujinx.HLE.OsHle.Handles namespace

* Remove hbmenu automatic load on process exit

* Rename Ns to Device, rename Os to System, rename SystemState to State

* Move Exceptions and Utilities out of OsHle

* Rename OsHle to HOS

* Rename OsHle folder to HOS

* IManagerDisplayService and ISystemDisplayService style fixes

* BsdError shouldn't be public

* Add a empty new line before using static

* Remove unused file

* Some style fixes on NPDM

* Exit gracefully when the application is closed

* Code style fixes on IGeneralService

* Add 0x prefix on values printed as hex

* Small improvements on finalization code

* Move ProcessId and ThreadId out of AThreadState

* Rename VFs to FileSystem

* FsAccessHeader shouldn't be public. Also fix file names casing

* More case changes on NPDM

* Remove unused files

* Move using to the correct place on NPDM

* Use properties on KernelAccessControlMmio

* Address PR feedback
2018-08-16 20:47:36 -03:00

107 lines
2.6 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace Ryujinx.HLE.HOS.Services.Nv.NvHostCtrl
{
class NvHostSyncpt
{
public const int SyncptsCount = 192;
private int[] CounterMin;
private int[] CounterMax;
private long EventMask;
private ConcurrentDictionary<EventWaitHandle, int> Waiters;
public NvHostSyncpt()
{
CounterMin = new int[SyncptsCount];
CounterMax = new int[SyncptsCount];
Waiters = new ConcurrentDictionary<EventWaitHandle, int>();
}
public int GetMin(int Id)
{
return CounterMin[Id];
}
public int GetMax(int Id)
{
return CounterMax[Id];
}
public int Increment(int Id)
{
if (((EventMask >> Id) & 1) != 0)
{
Interlocked.Increment(ref CounterMax[Id]);
}
return IncrementMin(Id);
}
public int IncrementMin(int Id)
{
int Value = Interlocked.Increment(ref CounterMin[Id]);
WakeUpWaiters(Id, Value);
return Value;
}
public int IncrementMax(int Id)
{
return Interlocked.Increment(ref CounterMax[Id]);
}
public void AddWaiter(int Threshold, EventWaitHandle WaitEvent)
{
if (!Waiters.TryAdd(WaitEvent, Threshold))
{
throw new InvalidOperationException();
}
}
public bool RemoveWaiter(EventWaitHandle WaitEvent)
{
return Waiters.TryRemove(WaitEvent, out _);
}
private void WakeUpWaiters(int Id, int NewValue)
{
foreach (KeyValuePair<EventWaitHandle, int> KV in Waiters)
{
if (MinCompare(Id, NewValue, CounterMax[Id], KV.Value))
{
KV.Key.Set();
Waiters.TryRemove(KV.Key, out _);
}
}
}
public bool MinCompare(int Id, int Threshold)
{
return MinCompare(Id, CounterMin[Id], CounterMax[Id], Threshold);
}
private bool MinCompare(int Id, int Min, int Max, int Threshold)
{
int MinDiff = Min - Threshold;
int MaxDiff = Max - Threshold;
if (((EventMask >> Id) & 1) != 0)
{
return MinDiff >= 0;
}
else
{
return (uint)MaxDiff >= (uint)MinDiff;
}
}
}
}