aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/Common/KAutoObject.cs
blob: 3b30dd808e3c39a378087c749737ef55aa6fb6ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Threading;

namespace Ryujinx.HLE.HOS.Kernel.Common
{
    class KAutoObject
    {
        protected Horizon System;

        private int _referenceCount;

        public KAutoObject(Horizon system)
        {
            System = system;

            _referenceCount = 1;
        }

        public virtual KernelResult SetName(string name)
        {
            if (!System.AutoObjectNames.TryAdd(name, this))
            {
                return KernelResult.InvalidState;
            }

            return KernelResult.Success;
        }

        public static KernelResult RemoveName(Horizon system, string name)
        {
            if (!system.AutoObjectNames.TryRemove(name, out _))
            {
                return KernelResult.NotFound;
            }

            return KernelResult.Success;
        }

        public static KAutoObject FindNamedObject(Horizon system, string name)
        {
            if (system.AutoObjectNames.TryGetValue(name, out KAutoObject obj))
            {
                return obj;
            }

            return null;
        }

        public void IncrementReferenceCount()
        {
            Interlocked.Increment(ref _referenceCount);
        }

        public void DecrementReferenceCount()
        {
            if (Interlocked.Decrement(ref _referenceCount) == 0)
            {
                Destroy();
            }
        }

        protected virtual void Destroy() { }
    }
}