aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/LightningJit/TranslatorCache.cs
blob: 60e13de9f73c962ebf72bae1554923a30ff76520 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using ARMeilleure.Translation;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Ryujinx.Cpu.LightningJit
{
    internal class TranslatorCache<T>
    {
        private readonly IntervalTree<ulong, T> _tree;
        private readonly ReaderWriterLockSlim _treeLock;

        public int Count => _tree.Count;

        public TranslatorCache()
        {
            _tree = new IntervalTree<ulong, T>();
            _treeLock = new ReaderWriterLockSlim();
        }

        public bool TryAdd(ulong address, ulong size, T value)
        {
            return AddOrUpdate(address, size, value, null);
        }

        public bool AddOrUpdate(ulong address, ulong size, T value, Func<ulong, T, T> updateFactoryCallback)
        {
            _treeLock.EnterWriteLock();
            bool result = _tree.AddOrUpdate(address, address + size, value, updateFactoryCallback);
            _treeLock.ExitWriteLock();

            return result;
        }

        public T GetOrAdd(ulong address, ulong size, T value)
        {
            _treeLock.EnterWriteLock();
            value = _tree.GetOrAdd(address, address + size, value);
            _treeLock.ExitWriteLock();

            return value;
        }

        public bool Remove(ulong address)
        {
            _treeLock.EnterWriteLock();
            bool removed = _tree.Remove(address) != 0;
            _treeLock.ExitWriteLock();

            return removed;
        }

        public void Clear()
        {
            _treeLock.EnterWriteLock();
            _tree.Clear();
            _treeLock.ExitWriteLock();
        }

        public bool ContainsKey(ulong address)
        {
            _treeLock.EnterReadLock();
            bool result = _tree.ContainsKey(address);
            _treeLock.ExitReadLock();

            return result;
        }

        public bool TryGetValue(ulong address, out T value)
        {
            _treeLock.EnterReadLock();
            bool result = _tree.TryGet(address, out value);
            _treeLock.ExitReadLock();

            return result;
        }

        public int GetOverlaps(ulong address, ulong size, ref ulong[] overlaps)
        {
            _treeLock.EnterReadLock();
            int count = _tree.Get(address, address + size, ref overlaps);
            _treeLock.ExitReadLock();

            return count;
        }

        public List<T> AsList()
        {
            _treeLock.EnterReadLock();
            List<T> list = _tree.AsList();
            _treeLock.ExitReadLock();

            return list;
        }
    }
}