-
Notifications
You must be signed in to change notification settings - Fork 5
/
LockeableObjectFactory.cs
29 lines (27 loc) · 1.18 KB
/
LockeableObjectFactory.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
using System;
using System.Collections.Concurrent;
namespace Bardock.Utils.Sync
{
/// <summary>
/// Creates objects based on instances of TSeed that can be used to acquire an exclusive lock.
/// Instanciate one factory for every use case you might have.
/// Inspired by Eugene Beresovsky's solution: http://stackoverflow.com/a/19375402
/// </summary>
/// <typeparam name="TSeed">Type of the object you want lock on</typeparam>
public class LockeableObjectFactory<TSeed>
{
private readonly ConcurrentDictionary<TSeed, object> _lockeableObjects = new ConcurrentDictionary<TSeed, object>();
/// <summary>
/// Creates or uses an existing object instance by specified seed
/// </summary>
/// <param name="seed">
/// The object used to generate a new lockeable object.
/// The default EqualityComparer is used to determine if two seeds are equal.
/// The same object instance is returned for equal seeds, otherwise a new object is created.
/// </param>
public object Get(TSeed seed)
{
return _lockeableObjects.GetOrAdd(seed, valueFactory: x => new object());
}
}
}