private static object GetOrAdd(string key) { CountedLock padlock = m_waitLock.GetOrAdd(key, LockFactory); padlock.Increment(); return(padlock); }
// Returns the semaphore associated to the key. Either creates // a new one and adds to the map, or returns the already existing // one (additionally incrementing its referenced count) private SemaphoreSlim AcquireLock(string key) { CountedLock exclusiveLock; lock (_locks) { if (_locks.TryGetValue(key, out exclusiveLock)) { exclusiveLock.Increment(); } else { exclusiveLock = new CountedLock(); _locks[key] = exclusiveLock; } } return(exclusiveLock.Lock); }