Skip to content

Commit

Permalink
Updated ConcurrentDic calls to be atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzhandler committed Aug 5, 2019
1 parent c7f5d5b commit 5c73294
Showing 1 changed file with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,43 +299,36 @@ private class ContractDictionary
public bool TryRemoveContract(string contract) =>
_dictionary.TryRemove(contract, out var _);

public Func<object> GetFactorý(string contract)
{
return GetFactories(contract)
public Func<object> GetFactory(string contract) =>
GetFactories(contract)
.LastOrDefault();
}

public IEnumerable<Func<object>> GetFactories(string contract)
{
if (_dictionary.TryGetValue(contract, out var collection))
{
return collection;
}

return Enumerable.Empty<Func<object>>();
}
public IEnumerable<Func<object>> GetFactories(string contract) =>
_dictionary.TryGetValue(contract, out var collection)
? collection
: Enumerable.Empty<Func<object>>();

public void AddFactory(string contract, Func<object> factory)
{
var collection = _dictionary.GetOrAdd(contract, _ => new List<Func<object>>());
collection.Add(factory);
}

public void RemoveLastFactory(string contract)
{
if (_dictionary.TryGetValue(contract, out var collection))
public void AddFactory(string contract, Func<object> factory) =>
_dictionary.AddOrUpdate(contract, _ => new List<Func<object>> { factory }, (_, list) =>
{
var lastIndex = collection.Count - 1;
if (lastIndex > 0)
{
collection.RemoveAt(lastIndex);
if (collection.Count == 0)
{
_dictionary.TryRemove(contract, out var _);
}
}
}
}
list.Add(factory);
return list;
});

public void RemoveLastFactory(string contract) =>
_dictionary.AddOrUpdate(contract, default(IList<Func<object>>), (_, list) =>
{
var lastIndex = list.Count - 1;
if (lastIndex > 0)
{
list.RemoveAt(lastIndex);
}
// TODO if list empty remove contract entirely
// need to find how to atomically update or remove
// https://github.com/dotnet/corefx/issues/24246
return list;
});
}

private class ContractDictionary<T> : ContractDictionary
Expand Down

0 comments on commit 5c73294

Please sign in to comment.