2023-01-04 23:15:45 +01:00
|
|
|
using Ryujinx.Horizon.Common;
|
2020-12-09 23:20:05 +01:00
|
|
|
using System.Diagnostics;
|
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
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
protected KernelContext KernelContext;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
private int _referenceCount;
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
public KAutoObject(KernelContext context)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KernelContext = context;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
_referenceCount = 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
public virtual Result SetName(string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
if (!KernelContext.AutoObjectNames.TryAdd(name, this))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
return Result.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
public static Result RemoveName(KernelContext context, string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
if (!context.AutoObjectNames.TryRemove(name, out _))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.NotFound;
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
return Result.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
public static KAutoObject FindNamedObject(KernelContext context, string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
if (context.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()
|
|
|
|
{
|
2020-12-09 23:20:05 +01:00
|
|
|
int newRefCount = Interlocked.Increment(ref _referenceCount);
|
|
|
|
|
|
|
|
Debug.Assert(newRefCount >= 2);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DecrementReferenceCount()
|
|
|
|
{
|
2020-12-09 23:20:05 +01:00
|
|
|
int newRefCount = Interlocked.Decrement(ref _referenceCount);
|
|
|
|
|
|
|
|
Debug.Assert(newRefCount >= 0);
|
|
|
|
|
|
|
|
if (newRefCount == 0)
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
protected virtual void Destroy()
|
|
|
|
{
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|