public async Task CleanLocks(string masterId) { MastersLocks.TryGetValue(masterId, out ConcurrentDictionary <int, GStoreObjectIdentifier> masterLocks); if (masterLocks == null) { return; } IDictionary <int, Task <string> > tasks = new Dictionary <int, Task <string> >(); foreach (KeyValuePair <int, GStoreObjectIdentifier> locks in masterLocks) { int lockId = locks.Key; GStoreObjectIdentifier gStoreObjectIdentifier = locks.Value; ReaderWriterLockEnhancedSlim objectLock = GetObjectLock(gStoreObjectIdentifier); if (!objectLock.IsWriteLockValid(lockId)) { throw new Exception("Invalid lock"); } Task <string> task = ReadRecoveryController.ExecuteAsync(connectionManager, gStoreObjectIdentifier); tasks.Add(lockId, task); } foreach (KeyValuePair <int, Task <string> > taskPair in tasks) { Task <string> task = taskPair.Value; int lockId = taskPair.Key; masterLocks.TryGetValue(lockId, out GStoreObjectIdentifier gStoreObjectIdentifier); ReaderWriterLockEnhancedSlim objectLock = GetObjectLock(gStoreObjectIdentifier); string value = await task; if (value != null) { _ = AddOrUpdate(gStoreObjectIdentifier, value); } Console.WriteLine("Cleaned one lock"); objectLock.ExitWriteLock(lockId); } }
public async Task <ICollection <GStoreObjectReplica> > ReadAll() { ICollection <ReaderWriterLockEnhancedSlim> locks = ObjectLocks.Values; IDictionary <ReaderWriterLockEnhancedSlim, Task <int> > lockTasks = new Dictionary <ReaderWriterLockEnhancedSlim, Task <int> >(); foreach (ReaderWriterLockEnhancedSlim objectLock in locks) { lockTasks.Add(objectLock, Task.Run(objectLock.EnterReadLock)); } IDictionary <ReaderWriterLockEnhancedSlim, int> lockWithReadIdSet = new Dictionary <ReaderWriterLockEnhancedSlim, int>(); foreach (KeyValuePair <ReaderWriterLockEnhancedSlim, Task <int> > lockTask in lockTasks) { ReaderWriterLockEnhancedSlim objectLock = lockTask.Key; Task <int> task = lockTask.Value; lockWithReadIdSet.Add(objectLock, await task); } ISet <GStoreObjectReplica> values = new HashSet <GStoreObjectReplica>(); foreach (GStoreObject gStoreObject in DataStore.Values) { // should lock partitions during this operation string partitionId = gStoreObject.Identifier.PartitionId; GStoreObjectReplica gStoreObjectReplica = new GStoreObjectReplica(gStoreObject, connectionManager.IsMasterForPartition(partitionId)); values.Add(gStoreObjectReplica); } foreach (KeyValuePair <ReaderWriterLockEnhancedSlim, int> lockWithReadId in lockWithReadIdSet) { ReaderWriterLockEnhancedSlim objectLock = lockWithReadId.Key; int lockId = lockWithReadId.Value; objectLock.ExitReadLock(lockId); } return(values); }
public bool IsLocked(GStoreObjectIdentifier gStoreObjectIdentifier) { ReaderWriterLockEnhancedSlim objectLock = GetObjectLock(gStoreObjectIdentifier); return(objectLock.IsWriteLocked()); }