2019-01-18 23:26:39 +01:00
|
|
|
using System.Threading;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Common
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
class KAutoObject
|
|
|
|
{
|
|
|
|
protected Horizon System;
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
private int _referenceCount;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KAutoObject(Horizon system)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
System = system;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
_referenceCount = 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public virtual KernelResult SetName(string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!System.AutoObjectNames.TryAdd(name, this))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public static KernelResult RemoveName(Horizon system, string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!system.AutoObjectNames.TryRemove(name, out _))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.NotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public static KAutoObject FindNamedObject(Horizon system, string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (system.AutoObjectNames.TryGetValue(name, out KAutoObject obj))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return obj;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
public void IncrementReferenceCount()
|
|
|
|
{
|
|
|
|
Interlocked.Increment(ref _referenceCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DecrementReferenceCount()
|
|
|
|
{
|
|
|
|
if (Interlocked.Decrement(ref _referenceCount) == 0)
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Destroy() { }
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|