< Summary

Information
Class: CounterpointCollective.Threading.ConcurrentDictionaryExtensions
Assembly: CounterpointCollective.Threading
File(s): /builds/counterpointcollective/prestoprimitives/Threading/ConcurrentDictionaryExtensions.cs
Line coverage
77%
Covered lines: 7
Uncovered lines: 2
Coverable lines: 9
Total lines: 38
Line coverage: 77.7%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Update(...)50%2277.77%

File(s)

/builds/counterpointcollective/prestoprimitives/Threading/ConcurrentDictionaryExtensions.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2
 3namespace CounterpointCollective.Threading
 4{
 5    public static class ConcurrentDictionaryExtensions
 6    {
 7        /// <summary>
 8        /// Atomic update. Will retry on concurrent writes.
 9        /// </summary>
 10        /// <typeparam name="TKey"></typeparam>
 11        /// <typeparam name="TValue"></typeparam>
 12        /// <param name="d">The dictionary to update.</param>
 13        /// <param name="k">The key to update.</param>
 14        /// <param name="update">May be called multiple times on concurrent writes</param>
 15        /// <param name="v">The initial value to check against.</param>
 16        /// <returns>The final value as written into <paramref name="d"/></returns>
 17        public static TValue Update<TKey, TValue>(
 18            this ConcurrentDictionary<TKey, TValue> d,
 19            TKey k,
 20            Func<TKey, TValue, TValue> update,
 21            TValue v
 22        )
 23        where TKey : notnull
 24        {
 10525            var res = update(k, v);
 10526            while (!d.TryUpdate(
 10527                k,
 10528                res,
 10529                v
 10530            ))
 31            {
 032                v = d[k];
 033                res = update(k, v);
 34            }
 10535            return res;
 36        }
 37    }
 38}