KAutoObject.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Ryujinx.Horizon.Common;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace Ryujinx.HLE.HOS.Kernel.Common
  5. {
  6. class KAutoObject
  7. {
  8. protected KernelContext KernelContext;
  9. private int _referenceCount;
  10. public KAutoObject(KernelContext context)
  11. {
  12. KernelContext = context;
  13. _referenceCount = 1;
  14. }
  15. public virtual Result SetName(string name)
  16. {
  17. if (!KernelContext.AutoObjectNames.TryAdd(name, this))
  18. {
  19. return KernelResult.InvalidState;
  20. }
  21. return Result.Success;
  22. }
  23. public static Result RemoveName(KernelContext context, string name)
  24. {
  25. if (!context.AutoObjectNames.TryRemove(name, out _))
  26. {
  27. return KernelResult.NotFound;
  28. }
  29. return Result.Success;
  30. }
  31. public static KAutoObject FindNamedObject(KernelContext context, string name)
  32. {
  33. if (context.AutoObjectNames.TryGetValue(name, out KAutoObject obj))
  34. {
  35. return obj;
  36. }
  37. return null;
  38. }
  39. public void IncrementReferenceCount()
  40. {
  41. int newRefCount = Interlocked.Increment(ref _referenceCount);
  42. Debug.Assert(newRefCount >= 2);
  43. }
  44. public void DecrementReferenceCount()
  45. {
  46. int newRefCount = Interlocked.Decrement(ref _referenceCount);
  47. Debug.Assert(newRefCount >= 0);
  48. if (newRefCount == 0)
  49. {
  50. Destroy();
  51. }
  52. }
  53. protected virtual void Destroy()
  54. {
  55. }
  56. }
  57. }