public void DelockTest_1Lock_2Tasks_0Complete() { const string key = "aKey"; var counter = new[] { 0L }; var locks = new MultiLock(1, 10, true); Task <long> task1 = new Task <long>(() => { return(locks.LockAndLoad(key, () => Interlocked.Increment(ref counter[0]))); }); Task <long> task2 = new Task <long>(() => { return(locks.LockAndLoad(key, () => { task1.Start(); task1.Wait(); return Interlocked.Increment(ref counter[0]); })); }); try { task2.Start(); task2.Wait(); } finally { Assert.AreEqual(0L, Interlocked.Read(ref counter[0])); } }
public void GetLockNumber_Deterministic_Random() { const int numberOfLocks = 1000; const int numberOfKeys = 100000; const int maxKeyLength = 100; const int iterations = 3; multiLock = new MultiLock(numberOfLocks); var keys = GetRandomStrings(numberOfKeys, maxKeyLength).ToList(); Print("keys (first 10): ", keys.Select(k => $"\"{k}\"").Take(10)); var lockNumbers0 = keys.Select(GetLockNumber).ToList(); Print("lockNumbers0 (first 10): ", lockNumbers0.Take(10)); for (int cnt = 1; cnt < iterations; cnt++) { var lockNumbersN = keys.Select(GetLockNumber).ToList(); Print($"lockNumbers{cnt} (first 10): ", lockNumbersN.Take(10)); CollectionAssert.AreEqual(lockNumbers0, lockNumbersN); } }
public WaitResult(MultiLock <TKey> multiLock, LockItem lockItem, TKey key, bool acquired) { MultiLock = multiLock; LockItem = lockItem; Key = key; LockAcquired = acquired; }
public async Task DelockTestAsync_1Lock_1Task_2Complete() { const string key = "aKey"; var counter = new[] { 0L }; var locks = new MultiLock(1, 10, true); await locks.LockAndLoadAsync(key, async() => await ExternalIncPosZero(locks, key, counter)); Assert.AreEqual(2L, Interlocked.Read(ref counter[0])); }
public void LockAndLoadAsync_WaitsForLock() { const int numberOfLocks = 512; const string key = "key1"; multiLock = new MultiLock(numberOfLocks, doEnableReentrantLocking: false); long[] entered = { 0L }; long[] locked = { 0L }; multiLock.Take(key); var runResult = Task.Run(() => { #pragma warning disable 1998 var _ = multiLock.LockAndLoadAsync(key, async() => { var result = Interlocked.Increment(ref entered[0]); var ln = GetLockNumber(key); var l = GetLock(ln); if (l.CurrentCount > 0) { Interlocked.Increment(ref locked[0]); } return(result); }); _.Wait(); #pragma warning restore 1998 }); // Give the task time to start Thread.Sleep(100); try { Assert.IsTrue(Interlocked.Read(ref entered[0]) == 0L, "Should wait for lock"); } finally { multiLock.Release(key); } runResult.Wait(100); Assert.IsTrue(Interlocked.Read(ref entered[0]) != 0L, "Should have gotten lock"); Assert.IsTrue(Interlocked.Read(ref locked[0]) == 0L, "Lock should have been taken"); var lockNumber = GetLockNumber(key); var lck = GetLock(lockNumber); Assert.IsTrue(lck.CurrentCount > 0, "Lock should have been released"); }
public void DelockTest_1Lock_1Task_2Complete() { const string key = "aKey"; var counter = new[] { 0L }; var locks = new MultiLock(1, 10, true); locks.LockAndLoad(key, () => { locks.LockAndLoad(key, () => Interlocked.Increment(ref counter[0])); return(Interlocked.Increment(ref counter[0])); }); Assert.AreEqual(2L, Interlocked.Read(ref counter[0])); }
public async Task DelockTestAsync_1Lock_1Task_DisabledReentrance_0Complete() { const string key = "aKey"; var counter = new[] { 0L }; var locks = new MultiLock(1, 10, true, doEnableReentrantLocking: false); try { await locks.LockAndLoadAsync(key, async() => await ExternalIncPosZero(locks, key, counter)); } finally { Assert.AreEqual(0L, Interlocked.Read(ref counter[0])); } }
public void LockAndLoadAsync_WaitsForLock_Timeout_NoThrow() { const int numberOfLocks = 512; const string key = "key1"; multiLock = new MultiLock(numberOfLocks, 10, false, doEnableReentrantLocking: false); long[] entered = { 0L }; long[] locked = { 0L }; multiLock.Take(key); var lockNumber = GetLockNumber(key); var lck = GetLock(lockNumber); try { #pragma warning disable 1998 var _ = multiLock.LockAndLoadAsync(key, async() => { var result = Interlocked.Increment(ref entered[0]); var ln = GetLockNumber(key); var l = GetLock(ln); if (l.CurrentCount > 0) { Interlocked.Increment(ref locked[0]); } return(result); }); _.Wait(); #pragma warning restore 1998 } finally { var lockCount = lck.CurrentCount; multiLock.Release(key); Assert.IsTrue(Interlocked.Read(ref entered[0]) != 0L, "Should have timed out and entered"); Assert.IsTrue(lockCount == 0, "Lock should have been taken"); Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released");; } }
protected ObjectCache( String name, System.Runtime.Caching.ObjectCache innerCache, InMemoryPolicy policy) { this.name = name; this.policy = policy; this.innerCache = innerCache; this.locks = !this.policy.DoNotLock ? new MultiLock( this.policy.NumberOfLocks ?? 50, this.policy.LockTimeoutMilliseconds != null && this.policy.LockTimeoutMilliseconds > 0 ? this.policy.LockTimeoutMilliseconds : null, this.policy.DoThrowExceptionOnTimeout ?? true) : null; this.notiferName = this.policy?.SyncProvider; this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName); }
public void DelockTest_1Lock_1Task_DisabledReentrance_0Complete() { const string key = "aKey"; var counter = new[] { 0L }; var locks = new MultiLock(1, 10, true, doEnableReentrantLocking: false); try { locks.LockAndLoad(key, () => { locks.LockAndLoad(key, () => Interlocked.Increment(ref counter[0])); return(Interlocked.Increment(ref counter[0])); }); } finally { Assert.AreEqual(0L, Interlocked.Read(ref counter[0])); } }
public void TakeAndRelease_CheckLock() { const int numberOfLocks = 13; const string key = "theKey"; multiLock = new MultiLock(numberOfLocks); var lockNumber = GetLockNumber(key); var lck = GetLock(lockNumber); multiLock.Take(key); try { Assert.IsTrue(lck.CurrentCount == 0, "Lock should have been taken"); } finally { multiLock.Release(key); } Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released"); }
public void LockAndLoad_WaitsForLock_Timeout_Throw() { const int numberOfLocks = 512; const string key = "key1"; multiLock = new MultiLock(numberOfLocks, 10, true, doEnableReentrantLocking: false); long[] entered = { 0L }; long[] locked = { 0L }; multiLock.Take(key); var lockNumber = GetLockNumber(key); var lck = GetLock(lockNumber); try { multiLock.LockAndLoad(key, () => { var result = Interlocked.Increment(ref entered[0]); var ln = GetLockNumber(key); var l = GetLock(ln); if (l.CurrentCount > 0) { Interlocked.Increment(ref locked[0]); } return(result); }); } finally { var lockCount = lck.CurrentCount; multiLock.Release(key); Assert.IsTrue(lockCount == 0, "Lock should have been taken"); Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released"); } }
internal MainWindow() { log.Info("Initializing main window..."); Instance = this; WoWLaunchLock = new MultiLock(); InitializeComponent(); StyleManager.Style = Settings2.Instance.StyleColor; Icon = Resources.ApplicationIcon; Closing += MainFormClosing; notifyIconMain.Icon = Resources.ApplicationIcon; tabControl.SelectedIndex = 0; linkEditWowAccounts.Location = new Point(metroTabPage1.Size.Width / 2 - linkEditWowAccounts.Size.Width / 2, linkEditWowAccounts.Location.Y); cmbboxAccSelect.Location = new Point(metroTabPage1.Size.Width / 2 - cmbboxAccSelect.Size.Width / 2, cmbboxAccSelect.Location.Y); progressBarAddonsBackup.Size = linkBackup.Size; progressBarAddonsBackup.Location = linkBackup.Location; progressBarAddonsBackup.Visible = false; SetupControls(); SetupEvents(); SetTooltips(); PostInvoke(AfterInitializingAsync); log.Info("MainWindow is constructed"); }
private (int, double) RunLockNumberDistributionTest( ushort numberOfLocks, Func <int, int, IEnumerable <String> > keysGenerator, int numberOfKeysPerLock, int maxKeyLength) { int numberOfKeys = numberOfLocks * numberOfKeysPerLock; multiLock = new MultiLock(numberOfLocks); var keys = keysGenerator(numberOfKeys, maxKeyLength).ToList(); Print("keys (first 10): ", keys.Select(k => $"\"{k}\"").Take(10)); var lockNumbers = keys.Select(GetLockNumber).ToList(); Print("lockNumbers (first 10): ", lockNumbers.Take(10)); Assert.AreEqual(numberOfKeys, lockNumbers.Count, "number of locks"); Assert.IsTrue(lockNumbers.All(l => l < numberOfLocks), "range check"); var usedLocks = lockNumbers.Distinct().ToList(); var zeros = Enumerable.Repeat(0, numberOfLocks - usedLocks.Count); var usageCount = lockNumbers.GroupBy(ln => ln).Select(g => g.Count()).ToList(); usageCount.AddRange(zeros); var distributionVsExpected = Math.Sqrt( usageCount .Select(x => Math.Pow(numberOfKeysPerLock - x, 2)) .Sum() ) / usageCount.Count; return(usedLocks.Count, distributionVsExpected); }
protected ObjectCache( String name, System.Runtime.Caching.ObjectCache innerCache, InMemoryPolicy policy) { this.name = name; this.Policy = policy; this.innerCache = innerCache; this.Locks = !this.Policy.DoNotLock ? new MultiLock( this.Policy.NumberOfLocks ?? 50, this.Policy.LockTimeoutMilliseconds != null && this.Policy.LockTimeoutMilliseconds > 0 ? this.Policy.LockTimeoutMilliseconds : null, this.Policy.DoThrowExceptionOnTimeout ?? true) : null; if (this.Policy.OnSyncProviderFailure != null) { if (string.IsNullOrEmpty(this.Policy.SyncProvider)) { throw new ApplicationException($"{name}.OnSyncProviderFailure requires SyncProvider to be defined"); } var cacheItemPolicy = ToRuntimePolicy(this.Policy); var syncProviderFailureCacheItemPolicy = ToRuntimePolicy(this.Policy.OnSyncProviderFailure); if (syncProviderFailureCacheItemPolicy.AbsoluteExpiration >= cacheItemPolicy.AbsoluteExpiration && syncProviderFailureCacheItemPolicy.SlidingExpiration >= cacheItemPolicy.SlidingExpiration) { throw new ApplicationException($"{name}.OnSyncProviderFailure expiry policy needs to be more restrictive"); } } this.notiferName = this.Policy.SyncProvider; this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName, invalidateOnStateChange: this.Policy.OnSyncProviderFailure?.InvalidateOnProviderStateChange ?? false); }
public void SetUp() { testObj = new MultiLock(); key = new object(); secondKey = new object(); }
/// <summary> /// ctor /// </summary> public ConcurrentFileSystemStorageProvider() { Storage = new FileSystemStorageProvider(); Locks = new MultiLock <string>(1); }
private async Task <long> ExternalIncPosZero(MultiLock locks, string key, long[] counter) { await locks.LockAndLoadAsync(key, async() => await InternalIncPosZero(counter)); return(Interlocked.Increment(ref counter[0])); }
void IDisposable.Dispose() { MultiLock.ReleaseLockItem(LockItem, Key); LockItem.Semaphore.Release(); }