2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
- using System ;
5
+ using Microsoft . Internal ;
6
6
using System . Collections . Generic ;
7
7
using System . Linq ;
8
- using System . Text ;
9
8
using System . Reflection ;
10
-
11
- using Microsoft . Internal ;
9
+ using System . Threading ;
12
10
13
11
namespace System . Composition . Convention
14
12
{
@@ -19,7 +17,7 @@ public class ConventionBuilder : AttributedModelProvider
19
17
{
20
18
private static readonly List < object > s_emptyList = new List < object > ( ) ;
21
19
22
- private readonly Lock _lock = new Lock ( ) ;
20
+ private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim ( LockRecursionPolicy . NoRecursion ) ;
23
21
private readonly List < PartConventionBuilder > _conventions = new List < PartConventionBuilder > ( ) ;
24
22
25
23
private readonly Dictionary < MemberInfo , List < Attribute > > _memberInfos = new Dictionary < MemberInfo , List < Attribute > > ( ) ;
@@ -161,13 +159,19 @@ public override IEnumerable<Attribute> GetCustomAttributes(Type reflectedType, S
161
159
if ( typeInfo != null )
162
160
{
163
161
var memberInfo = typeInfo as MemberInfo ;
164
- using ( new ReadLock ( _lock ) )
162
+ _lock . EnterReadLock ( ) ;
163
+ try
165
164
{
166
165
_memberInfos . TryGetValue ( memberInfo , out cachedAttributes ) ;
167
166
}
167
+ finally
168
+ {
169
+ _lock . ExitReadLock ( ) ;
170
+ }
168
171
if ( cachedAttributes == null )
169
172
{
170
- using ( new WriteLock ( _lock ) )
173
+ _lock . EnterWriteLock ( ) ;
174
+ try
171
175
{
172
176
//Double check locking another thread may have inserted one while we were away.
173
177
if ( ! _memberInfos . TryGetValue ( memberInfo , out cachedAttributes ) )
@@ -210,6 +214,10 @@ public override IEnumerable<Attribute> GetCustomAttributes(Type reflectedType, S
210
214
// We will have updated all of the MemberInfos by now so lets reload cachedAttributes wiuth the current store
211
215
_memberInfos . TryGetValue ( memberInfo , out cachedAttributes ) ;
212
216
}
217
+ finally
218
+ {
219
+ _lock . ExitWriteLock ( ) ;
220
+ }
213
221
}
214
222
}
215
223
else if ( member . IsMemberInfoForProperty ( ) || member . IsMemberInfoForConstructor ( ) || member . IsMemberInfoForMethod ( ) )
@@ -232,7 +240,8 @@ private List<Attribute> ReadMemberCustomAttributes(Type reflectedType, System.Re
232
240
bool getMemberAttributes = false ;
233
241
234
242
// Now edit the attributes returned from the base type
235
- using ( new ReadLock ( _lock ) )
243
+ _lock . EnterReadLock ( ) ;
244
+ try
236
245
{
237
246
if ( ! _memberInfos . TryGetValue ( member , out cachedAttributes ) )
238
247
{
@@ -247,16 +256,25 @@ private List<Attribute> ReadMemberCustomAttributes(Type reflectedType, System.Re
247
256
cachedAttributes = null ;
248
257
}
249
258
}
259
+ finally
260
+ {
261
+ _lock . ExitReadLock ( ) ;
262
+ }
250
263
251
264
if ( getMemberAttributes )
252
265
{
253
266
GetCustomAttributes ( null , reflectedType . GetTypeInfo ( ) as MemberInfo ) ;
254
267
255
268
// We should have run the rules for the enclosing parameter so we can again
256
- using ( new ReadLock ( _lock ) )
269
+ _lock . EnterReadLock ( ) ;
270
+ try
257
271
{
258
272
_memberInfos . TryGetValue ( member , out cachedAttributes ) ;
259
273
}
274
+ finally
275
+ {
276
+ _lock . ExitReadLock ( ) ;
277
+ }
260
278
}
261
279
262
280
return cachedAttributes ;
@@ -283,7 +301,8 @@ private List<Attribute> ReadParameterCustomAttributes(Type reflectedType, System
283
301
bool getMemberAttributes = false ;
284
302
285
303
// Now edit the attributes returned from the base type
286
- using ( new ReadLock ( _lock ) )
304
+ _lock . EnterReadLock ( ) ;
305
+ try
287
306
{
288
307
if ( ! _parameters . TryGetValue ( parameter , out cachedAttributes ) )
289
308
{
@@ -298,16 +317,25 @@ private List<Attribute> ReadParameterCustomAttributes(Type reflectedType, System
298
317
cachedAttributes = null ;
299
318
}
300
319
}
320
+ finally
321
+ {
322
+ _lock . ExitReadLock ( ) ;
323
+ }
301
324
302
325
if ( getMemberAttributes )
303
326
{
304
327
GetCustomAttributes ( null , reflectedType . GetTypeInfo ( ) as MemberInfo ) ;
305
328
306
329
// We should have run the rules for the enclosing parameter so we can again
307
- using ( new ReadLock ( _lock ) )
330
+ _lock . EnterReadLock ( ) ;
331
+ try
308
332
{
309
333
_parameters . TryGetValue ( parameter , out cachedAttributes ) ;
310
334
}
335
+ finally
336
+ {
337
+ _lock . ExitReadLock ( ) ;
338
+ }
311
339
}
312
340
313
341
return cachedAttributes ;
0 commit comments