Skip to content

Commit 589e04d

Browse files
Merge pull request #6 from ShawnLaMountain/main
Replaced custom lock 'ReaderWriterNotifyLock' with 'System.Threading.ReaderWriterLockSlim'
2 parents cf5a932 + ecf0311 commit 589e04d

23 files changed

Lines changed: 151 additions & 423 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
[![CI](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CI.yml/badge.svg)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CI.yml)
33
[![CD](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CD.yml/badge.svg)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/actions/workflows/CD.yml)
44
[![Nuget](https://img.shields.io/nuget/v/ThunderDesign.Net-PCL.Threading)](https://www.nuget.org/packages/ThunderDesign.Net-PCL.Threading)
5+
[![License](https://img.shields.io/github/license/ThunderDesign/ThunderDesign.Net-PCL.Threading)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.Threading/blob/main/LICENSE)
56

67
Thread-Safe Objects

src/ThunderDesign.Net-PCL.Threading/Collections/DictionaryThreadSafe.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.Serialization;
4-
using System.Text;
4+
using System.Threading;
55
using ThunderDesign.Net.Threading.Interfaces;
6-
using ThunderDesign.Net.Threading.Threading;
76

87
namespace ThunderDesign.Net.Threading.Collections
98
{
@@ -23,14 +22,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
2322
{
2423
get
2524
{
26-
_ReaderWriterNotifyLock.EnterReadLock();
25+
_ReaderWriterLockSlim.EnterReadLock();
2726
try
2827
{
2928
return base.Comparer;
3029
}
3130
finally
3231
{
33-
_ReaderWriterNotifyLock.ExitReadLock();
32+
_ReaderWriterLockSlim.ExitReadLock();
3433
}
3534
}
3635
}
@@ -39,14 +38,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
3938
{
4039
get
4140
{
42-
_ReaderWriterNotifyLock.EnterReadLock();
41+
_ReaderWriterLockSlim.EnterReadLock();
4342
try
4443
{
4544
return base.Count;
4645
}
4746
finally
4847
{
49-
_ReaderWriterNotifyLock.ExitReadLock();
48+
_ReaderWriterLockSlim.ExitReadLock();
5049
}
5150
}
5251
}
@@ -55,14 +54,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
5554
{
5655
get
5756
{
58-
_ReaderWriterNotifyLock.EnterReadLock();
57+
_ReaderWriterLockSlim.EnterReadLock();
5958
try
6059
{
6160
return base.Keys;
6261
}
6362
finally
6463
{
65-
_ReaderWriterNotifyLock.ExitReadLock();
64+
_ReaderWriterLockSlim.ExitReadLock();
6665
}
6766
}
6867
}
@@ -71,14 +70,14 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
7170
{
7271
get
7372
{
74-
_ReaderWriterNotifyLock.EnterReadLock();
73+
_ReaderWriterLockSlim.EnterReadLock();
7574
try
7675
{
7776
return base.Values;
7877
}
7978
finally
8079
{
81-
_ReaderWriterNotifyLock.ExitReadLock();
80+
_ReaderWriterLockSlim.ExitReadLock();
8281
}
8382
}
8483
}
@@ -87,26 +86,26 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
8786
{
8887
get
8988
{
90-
_ReaderWriterNotifyLock.EnterReadLock();
89+
_ReaderWriterLockSlim.EnterReadLock();
9190
try
9291
{
9392
return base[key];
9493
}
9594
finally
9695
{
97-
_ReaderWriterNotifyLock.ExitReadLock();
96+
_ReaderWriterLockSlim.ExitReadLock();
9897
}
9998
}
10099
set
101100
{
102-
_ReaderWriterNotifyLock.EnterWriteLock();
101+
_ReaderWriterLockSlim.EnterWriteLock();
103102
try
104103
{
105104
base[key] = value;
106105
}
107106
finally
108107
{
109-
_ReaderWriterNotifyLock.ExitWriteLock();
108+
_ReaderWriterLockSlim.ExitWriteLock();
110109
}
111110
}
112111
}
@@ -115,127 +114,127 @@ public DictionaryThreadSafe(IDictionary<TKey, TValue> dictionary, IEqualityCompa
115114
#region methods
116115
public new virtual void Add(TKey key, TValue value)
117116
{
118-
_ReaderWriterNotifyLock.EnterWriteLock();
117+
_ReaderWriterLockSlim.EnterWriteLock();
119118
try
120119
{
121120
base.Add(key, value);
122121
}
123122
finally
124123
{
125-
_ReaderWriterNotifyLock.ExitWriteLock();
124+
_ReaderWriterLockSlim.ExitWriteLock();
126125
}
127126
}
128127

129128
public new virtual void Clear()
130129
{
131-
_ReaderWriterNotifyLock.EnterWriteLock();
130+
_ReaderWriterLockSlim.EnterWriteLock();
132131
try
133132
{
134133
base.Clear();
135134
}
136135
finally
137136
{
138-
_ReaderWriterNotifyLock.ExitWriteLock();
137+
_ReaderWriterLockSlim.ExitWriteLock();
139138
}
140139
}
141140

142141
public new bool ContainsKey(TKey key)
143142
{
144-
_ReaderWriterNotifyLock.EnterReadLock();
143+
_ReaderWriterLockSlim.EnterReadLock();
145144
try
146145
{
147146
return base.ContainsKey(key);
148147
}
149148
finally
150149
{
151-
_ReaderWriterNotifyLock.ExitReadLock();
150+
_ReaderWriterLockSlim.ExitReadLock();
152151
}
153152
}
154153

155154
public new bool ContainsValue(TValue value)
156155
{
157-
_ReaderWriterNotifyLock.EnterReadLock();
156+
_ReaderWriterLockSlim.EnterReadLock();
158157
try
159158
{
160159
return base.ContainsValue(value);
161160
}
162161
finally
163162
{
164-
_ReaderWriterNotifyLock.ExitReadLock();
163+
_ReaderWriterLockSlim.ExitReadLock();
165164
}
166165
}
167166

168167
public new Enumerator GetEnumerator()
169168
{
170-
_ReaderWriterNotifyLock.EnterReadLock();
169+
_ReaderWriterLockSlim.EnterReadLock();
171170
try
172171
{
173172
return base.GetEnumerator();
174173
}
175174
finally
176175
{
177-
_ReaderWriterNotifyLock.ExitReadLock();
176+
_ReaderWriterLockSlim.ExitReadLock();
178177
}
179178
}
180179

181180
[System.Security.SecurityCritical] // auto-generated_required
182181
public override void GetObjectData(SerializationInfo info, StreamingContext context)
183182
{
184-
_ReaderWriterNotifyLock.EnterReadLock();
183+
_ReaderWriterLockSlim.EnterReadLock();
185184
try
186185
{
187186
base.GetObjectData(info, context);
188187
}
189188
finally
190189
{
191-
_ReaderWriterNotifyLock.ExitReadLock();
190+
_ReaderWriterLockSlim.ExitReadLock();
192191
}
193192
}
194193

195194
public override void OnDeserialization(Object sender)
196195
{
197-
_ReaderWriterNotifyLock.EnterReadLock();
196+
_ReaderWriterLockSlim.EnterReadLock();
198197
try
199198
{
200199
base.OnDeserialization(sender);
201200
}
202201
finally
203202
{
204-
_ReaderWriterNotifyLock.ExitReadLock();
203+
_ReaderWriterLockSlim.ExitReadLock();
205204
}
206205
}
207206

208207
public new virtual bool Remove(TKey key)
209208
{
210209
bool result = false;
211-
_ReaderWriterNotifyLock.EnterWriteLock();
210+
_ReaderWriterLockSlim.EnterWriteLock();
212211
try
213212
{
214213
result = base.Remove(key);
215214
}
216215
finally
217216
{
218-
_ReaderWriterNotifyLock.ExitWriteLock();
217+
_ReaderWriterLockSlim.ExitWriteLock();
219218
}
220219
return result;
221220
}
222221

223222
public new bool TryGetValue(TKey key, out TValue value)
224223
{
225-
_ReaderWriterNotifyLock.EnterReadLock();
224+
_ReaderWriterLockSlim.EnterReadLock();
226225
try
227226
{
228227
return base.TryGetValue(key, out value);
229228
}
230229
finally
231230
{
232-
_ReaderWriterNotifyLock.ExitReadLock();
231+
_ReaderWriterLockSlim.ExitReadLock();
233232
}
234233
}
235234
#endregion
236235

237236
#region variables
238-
protected static readonly ReaderWriterNotifyLock _ReaderWriterNotifyLock = new ReaderWriterNotifyLock();
237+
protected static readonly ReaderWriterLockSlim _ReaderWriterLockSlim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
239238

240239
#endregion
241240
}

0 commit comments

Comments
 (0)