using System; using System.Collections.Generic; using System.Threading; namespace ScreenConnect; public class LockManager { private HashSet locks = new HashSet(); public HashSet GetAcquiredLockKeys() { lock (locks) { return new HashSet(locks); } } public int GetAcquiredLockCount() { lock (locks) { return locks.Count; } } public bool IsLockAvailable(T key) { lock (locks) { return !locks.Contains(key); } } public IDisposable TryAcquireSelfReleasingLock(T key) { if (!TryAcquireLock(key)) { return null; } return new ProcDisposable(delegate { ReleaseLock(key); }); } public IDisposable AcquireSelfReleasingLock(T key) { AcquireLock(key); return new ProcDisposable(delegate { ReleaseLock(key); }); } public bool TryAcquireLock(T key) { lock (locks) { return locks.Add(key); } } public void AcquireLock(T key) { lock (locks) { while (!locks.Add(key)) { Monitor.Wait(locks); } } } public void ReleaseLock(T key) { lock (locks) { locks.Remove(key); Monitor.PulseAll(locks); } } }