public void Remember_ContextAndScopeAndReference_ShouldSubscripeToDisposedEventWhenScopeImplementsINotifyWhenDisposedAndClearCacheForTheScopeUponDispose() { var context = _contextMock1.Object; var scope = new DisposableScope(); var cache = CreateCache(); var instance = new object(); var instanceReference1 = new InstanceReference { Instance = instance }; var instanceReference2 = new InstanceReference { Instance = instance }; Remember(cache, _contextMock1, new object(), _bindingConfigurationMock1.Object, instanceReference1); Remember(cache, _contextMock1, scope, _bindingConfigurationMock1.Object, instanceReference2); _pipelineMock.InSequence(_mockSequence).Setup(p => p.Deactivate(_contextMock1.Object, instanceReference2)); Assert.Equal(2, cache.Count); scope.Dispose(); Assert.Equal(1, cache.Count); Assert.Null(cache.TryGet(context, scope)); _pipelineMock.Verify(p => p.Deactivate(_contextMock1.Object, instanceReference2)); }
/// <summary> /// Asynchronously locks against the given key. /// </summary> /// <param name="key"> /// The key that identifies the current object. /// </param> /// <returns> /// The disposable <see cref="Task"/>. /// </returns> public Task <IDisposable> LockAsync(object key) { DisposableScope releaser = new DisposableScope( key, s => { SemaphoreSlim locker; if (SemaphoreSlims.TryRemove(s, out locker)) { locker.Release(); locker.Dispose(); } }); Task <IDisposable> releaserTask = Task.FromResult(releaser as IDisposable); SemaphoreSlim semaphore = SemaphoreSlims.GetOrAdd(key, new SemaphoreSlim(1, 1)); Task waitTask = semaphore.WaitAsync(); return(waitTask.IsCompleted ? releaserTask : waitTask.ContinueWith( (_, r) => (IDisposable)r, releaser, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); }
/// <summary> /// Asynchronously locks against the given key. /// </summary> /// <param name="key"> /// The key that identifies the current object. /// </param> /// <returns> /// The disposable <see cref="Task"/>. /// </returns> public Task<IDisposable> LockAsync(object key) { DisposableScope releaser = new DisposableScope( key, s => { SemaphoreSlim locker; if (SemaphoreSlims.TryRemove(s, out locker)) { locker.Release(); locker.Dispose(); } }); Task<IDisposable> releaserTask = Task.FromResult(releaser as IDisposable); SemaphoreSlim semaphore = SemaphoreSlims.GetOrAdd(key, new SemaphoreSlim(1, 1)); Task waitTask = semaphore.WaitAsync(); return waitTask.IsCompleted ? releaserTask : waitTask.ContinueWith( (_, r) => (IDisposable)r, releaser, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); }
private static void Main(string[] args) { using (_proxy) { _proxy = new DisposableScope(); } }
public static IDisposable GetIdentifiersOfMappedDocuments(this Index self, string startsWith, int start, int take, out IEnumerable <string> docIds) { if (self.Type.IsMapReduce() == false) { throw new NotSupportedException("Getting doc ids for map indexes is not supported"); } using (var scope = new DisposableScope()) { scope.EnsureDispose(self._contextPool.AllocateOperationContext(out TransactionOperationContext indexContext)); RavenTransaction tx; scope.EnsureDispose(tx = indexContext.OpenReadTransaction()); var tree = tx.InnerTransaction.ReadTree(MapReduceIndexBase <MapReduceIndexDefinition, IndexField> .MapPhaseTreeName); if (tree == null) { docIds = Enumerable.Empty <string>(); return(scope); } TreeIterator it; scope.EnsureDispose(it = tree.Iterate(false)); docIds = IterateKeys(it, startsWith, start, take, indexContext); return(scope.Delay()); } }
private WeakReference RememberDisposableScope(Cache cache, Mock <IContext> contextMock, IBindingConfiguration bindingConfiguration, InstanceReference instanceReference) { var disposableScope = new DisposableScope(); cache.Remember(contextMock.Object, disposableScope, instanceReference); return(new WeakReference(disposableScope)); }
public static IDisposable Show(Form parentForm) { var f = new InitializingUserPage(parentForm); var ret = new DisposableScope(f); new Thread(() => f.ShowDialog()).Start(); return(ret); }
internal IDisposable Push(object state) { lock ("scope") { var newScope = new DisposableScope(state.ToString(), this); m_AsyncSopes.Value.Add(newScope); return(newScope); } }
public IDisposable ReadPostponedActions(out IEnumerable <NotificationTableValue> actions, DateTime cutoff) { using (var scope = new DisposableScope()) { scope.EnsureDispose(_contextPool.AllocateOperationContext(out TransactionOperationContext context)); scope.EnsureDispose(context.OpenReadTransaction()); actions = ReadPostponedActionsByPostponedUntilIndex(context, cutoff); return(scope.Delay()); } }
public IDisposable ReadActionsOrderedByCreationDate(out IEnumerable <NotificationTableValue> actions) { using (var scope = new DisposableScope()) { scope.EnsureDispose(_contextPool.AllocateOperationContext(out TransactionOperationContext context)); scope.EnsureDispose(context.OpenReadTransaction()); actions = ReadActionsByCreatedAtIndex(context); return(scope.Delay()); } }
public IDisposable Read(string id, out NotificationTableValue value) { using (var scope = new DisposableScope()) { RavenTransaction tx; scope.EnsureDispose(_contextPool.AllocateOperationContext(out TransactionOperationContext context)); scope.EnsureDispose(tx = context.OpenReadTransaction()); value = Get(id, context, tx); return(scope.Delay()); } }
public IDisposable Push(object state) { lock ("scope") { if (m_AsyncSopes.Value == null) { m_AsyncSopes.Value = new List <DisposableScope>(); } var newScope = new DisposableScope(state.ToString(), this); m_AsyncSopes.Value.Add(newScope); return(newScope); } }
/// <summary> /// Locks against the given key. /// </summary> /// <param name="key"> /// The key that identifies the current object. /// </param> /// <returns> /// The disposable <see cref="Task"/>. /// </returns> public IDisposable Lock(object key) { DisposableScope releaser = new DisposableScope( key, s => { SemaphoreSlim locker; if (SemaphoreSlims.TryRemove(s, out locker)) { locker.Release(); locker.Dispose(); } }); SemaphoreSlim semaphore = SemaphoreSlims.GetOrAdd(key, new SemaphoreSlim(1, 1)); semaphore.Wait(); return releaser; }
/// <summary> /// Locks against the given key. /// </summary> /// <param name="key"> /// The key that identifies the current object. /// </param> /// <returns> /// The disposable <see cref="Task"/>. /// </returns> public IDisposable Lock(object key) { DisposableScope releaser = new DisposableScope( key, s => { SemaphoreSlim locker; if (SemaphoreSlims.TryRemove(s, out locker)) { locker.Release(); locker.Dispose(); } }); SemaphoreSlim semaphore = SemaphoreSlims.GetOrAdd(key, new SemaphoreSlim(1, 1)); semaphore.Wait(); return(releaser); }
public static IDisposable Lock(object key) { // Get the current info or create a new one var info = activeLocks.AddOrUpdate(key, (k) => new LockInfo(), (k, v) => v.Enter() ? v : new LockInfo()); DisposableScope releaser = new DisposableScope(() => { if (info.Exit()) { // Only remove this exact info, in case another thread has // already put its own info into the dictionary ((ICollection <KeyValuePair <object, LockInfo> >)activeLocks) .Remove(new KeyValuePair <object, LockInfo>(key, info)); } }); return(releaser); }
public static IDisposable GetReduceTree(this Index self, string[] docIds, out IEnumerable <ReduceTree> trees) { using (var scope = new DisposableScope()) { scope.EnsureDispose(self._contextPool.AllocateOperationContext(out TransactionOperationContext indexContext)); RavenTransaction tx; scope.EnsureDispose(tx = indexContext.OpenReadTransaction()); var mapPhaseTree = tx.InnerTransaction.ReadTree(MapReduceIndexBase <MapReduceIndexDefinition, IndexField> .MapPhaseTreeName); if (mapPhaseTree == null) { trees = Enumerable.Empty <ReduceTree>(); return(scope); } var reducePhaseTree = tx.InnerTransaction.ReadTree(MapReduceIndexBase <MapReduceIndexDefinition, IndexField> .ReducePhaseTreeName); if (reducePhaseTree == null) { trees = Enumerable.Empty <ReduceTree>(); return(scope); } var mapEntries = new List <FixedSizeTree>(docIds.Length); foreach (var docId in docIds) { FixedSizeTree mapEntriesTree; scope.EnsureDispose(mapEntriesTree = mapPhaseTree.FixedTreeFor(docId.ToLower(), sizeof(long))); mapEntries.Add(mapEntriesTree); } FixedSizeTree typePerHash; scope.EnsureDispose(typePerHash = reducePhaseTree.FixedTreeFor(MapReduceIndexBase <MapReduceIndexDefinition, IndexField> .ResultsStoreTypesTreeName, sizeof(byte))); trees = IterateTrees(self, mapEntries, reducePhaseTree, typePerHash, indexContext, scope); return(scope.Delay()); } }
private static IEnumerable <ReduceTree> IterateTrees(Index self, List <FixedSizeTree> mapEntries, Tree reducePhaseTree, FixedSizeTree typePerHash, TransactionOperationContext indexContext, DisposableScope scope) { var reduceKeys = new HashSet <ulong>(); var idToDocIdHash = new Dictionary <long, string>(); foreach (var tree in mapEntries) { foreach (var mapEntry in MapReduceIndexBase <MapReduceIndexDefinition, IndexField> .GetMapEntries(tree)) { reduceKeys.Add(mapEntry.ReduceKeyHash); idToDocIdHash[mapEntry.Id] = tree.Name.ToString(); } } foreach (var reduceKeyHash in reduceKeys) { MapReduceResultsStore store; var mapReduceIndex = self as MapReduceIndex; if (mapReduceIndex != null) { store = mapReduceIndex.CreateResultsStore(typePerHash, reduceKeyHash, indexContext, false); } else { store = ((AutoMapReduceIndex)self).CreateResultsStore(typePerHash, reduceKeyHash, indexContext, false); } using (store) { ReduceTree tree; switch (store.Type) { case MapResultsStorageType.Tree: tree = RenderTree(store.Tree, reduceKeyHash, idToDocIdHash, self, indexContext); break; case MapResultsStorageType.Nested: tree = RenderNestedSection(store.GetNestedResultsSection(reducePhaseTree), reduceKeyHash, idToDocIdHash, self, indexContext); break; default: throw new ArgumentOutOfRangeException(store.Type.ToString()); } scope.EnsureDispose(tree); yield return(tree); } } }
public AsyncLock() { _releaser = new DisposableScope(() => _semaphore.Release()); _releaserTask = Task.FromResult(_releaser as IDisposable); }