KAutoObject.cs 1.7 KB

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