forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedVariable.cs
85 lines (76 loc) · 1.89 KB
/
SharedVariable.cs
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
// Copyright(c) 2020 pypy. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System.Collections.Generic;
using System.Threading;
namespace VRCX
{
public class SharedVariable
{
public static readonly SharedVariable Instance;
private readonly ReaderWriterLockSlim m_MapLock;
private readonly Dictionary<string, string> m_Map;
static SharedVariable()
{
Instance = new SharedVariable();
}
public SharedVariable()
{
m_MapLock = new ReaderWriterLockSlim();
m_Map = new Dictionary<string, string>();
}
public void Clear()
{
m_MapLock.EnterWriteLock();
try
{
m_Map.Clear();
}
finally
{
m_MapLock.ExitWriteLock();
}
}
public string Get(string key)
{
m_MapLock.EnterReadLock();
try
{
if (m_Map.TryGetValue(key, out string value) == true)
{
return value;
}
}
finally
{
m_MapLock.ExitReadLock();
}
return null;
}
public void Set(string key, string value)
{
m_MapLock.EnterWriteLock();
try
{
m_Map[key] = value;
}
finally
{
m_MapLock.ExitWriteLock();
}
}
public bool Remove(string key)
{
m_MapLock.EnterWriteLock();
try
{
return m_Map.Remove(key);
}
finally
{
m_MapLock.ExitWriteLock();
}
}
}
}