aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Sm/SmRegistry.cs
blob: 3919eaae9156040afb3949fde87d0d9ea6c9b333 (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
using Ryujinx.HLE.HOS.Kernel.Ipc;
using System.Collections.Concurrent;
using System.Threading;

namespace Ryujinx.HLE.HOS.Services.Sm
{
    class SmRegistry
    {
        private readonly ConcurrentDictionary<string, KPort> _registeredServices;
        private readonly AutoResetEvent _serviceRegistrationEvent;

        public SmRegistry()
        {
            _registeredServices = new ConcurrentDictionary<string, KPort>();
            _serviceRegistrationEvent = new AutoResetEvent(false);
        }

        public bool TryGetService(string name, out KPort port)
        {
            return _registeredServices.TryGetValue(name, out port);
        }

        public bool TryRegister(string name, KPort port)
        {
            if (_registeredServices.TryAdd(name, port))
            {
                _serviceRegistrationEvent.Set();
                return true;
            }

            return false;
        }

        public bool Unregister(string name)
        {
            return _registeredServices.TryRemove(name, out _);
        }

        public bool IsServiceRegistered(string name)
        {
            return _registeredServices.TryGetValue(name, out _);
        }

        public void WaitForServiceRegistration()
        {
            _serviceRegistrationEvent.WaitOne();
        }
    }
}