Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Commit

Permalink
Added locks to ObjectDataCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
christophano committed May 23, 2018
1 parent 14f584f commit cb18ba4
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Effort.Extra/ObjectDataCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Effort.Extra
/// </summary>
internal class ObjectDataCollection : KeyedCollection<Guid, ObjectData>
{
private readonly object locker = new object();

/// <summary>
/// Extracts the key from the specified element.
/// </summary>
Expand All @@ -28,9 +30,12 @@ protected override Guid GetKeyForItem(ObjectData item)
/// <param name="data">The data.</param>
public void AddOrUpdate(ObjectData data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (Contains(data.Identifier)) Remove(data.Identifier);
Add(data);
lock (locker)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (Contains(data.Identifier)) Remove(data.Identifier);
Add(data);
}
}

/// <summary>
Expand All @@ -41,8 +46,11 @@ public void AddOrUpdate(ObjectData data)
/// <returns><c>true</c>, if the key exists, otherwise <c>false</c>.</returns>
public bool TryGetValue(Guid key, out ObjectData data)
{
data = Contains(key) ? this[key] : null;
return data != null;
lock (locker)
{
data = Contains(key) ? this[key] : null;
return data != null;
}
}
}
}

0 comments on commit cb18ba4

Please sign in to comment.