aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Nv/NvHostCtrl/NvHostSyncPt.cs
blob: d27f7c53481c26f42c55d6a92dbb351dccdbe2da (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
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace Ryujinx.HLE.HOS.Services.Nv.NvHostCtrl
{
    class NvHostSyncpt
    {
        public const int SyncptsCount = 192;

        private int[] _counterMin;
        private int[] _counterMax;

        private long _eventMask;

        private ConcurrentDictionary<EventWaitHandle, int> _waiters;

        public NvHostSyncpt()
        {
            _counterMin = new int[SyncptsCount];
            _counterMax = new int[SyncptsCount];

            _waiters = new ConcurrentDictionary<EventWaitHandle, int>();
        }

        public int GetMin(int id)
        {
            return _counterMin[id];
        }

        public int GetMax(int id)
        {
            return _counterMax[id];
        }

        public int Increment(int id)
        {
            if (((_eventMask >> id) & 1) != 0)
            {
                Interlocked.Increment(ref _counterMax[id]);
            }

            return IncrementMin(id);
        }

        public int IncrementMin(int id)
        {
            int value = Interlocked.Increment(ref _counterMin[id]);

            WakeUpWaiters(id, value);

            return value;
        }

        public int IncrementMax(int id)
        {
            return Interlocked.Increment(ref _counterMax[id]);
        }

        public void AddWaiter(int threshold, EventWaitHandle waitEvent)
        {
            if (!_waiters.TryAdd(waitEvent, threshold))
            {
                throw new InvalidOperationException();
            }
        }

        public bool RemoveWaiter(EventWaitHandle waitEvent)
        {
            return _waiters.TryRemove(waitEvent, out _);
        }

        private void WakeUpWaiters(int id, int newValue)
        {
            foreach (KeyValuePair<EventWaitHandle, int> kv in _waiters)
            {
                if (MinCompare(id, newValue, _counterMax[id], kv.Value))
                {
                    kv.Key.Set();

                    _waiters.TryRemove(kv.Key, out _);
                }
            }
        }

        public bool MinCompare(int id, int threshold)
        {
            return MinCompare(id, _counterMin[id], _counterMax[id], threshold);
        }

        private bool MinCompare(int id, int min, int max, int threshold)
        {
            int minDiff = min - threshold;
            int maxDiff = max - threshold;

            if (((_eventMask >> id) & 1) != 0)
            {
                return minDiff >= 0;
            }
            else
            {
                return (uint)maxDiff >= (uint)minDiff;
            }
        }
    }
}