Пример #1
0
 public TransactionInformation(Transaction transaction, TKey key, TValue previousValue, bool newEntry, ConcurrentTransactionalDictionary <TKey, TValue> owner)
 {
     this.key           = key;
     this.NewEntry      = newEntry;
     this.owner         = owner;
     this.PreviousValue = previousValue;
     this.transaction   = transaction;
     this.transaction.TransactionCompleted += Fx.ThunkTransactionEventHandler((object s, TransactionEventArgs e) => {
         bool status   = e.Transaction.TransactionInformation.Status == TransactionStatus.Committed;
         bool flag     = false;
         TValue tValue = default(TValue);
         lock (this.owner.syncLock)
         {
             this.owner.pendingTransactions.Remove(this.key);
             if (!status)
             {
                 if (!this.NewEntry)
                 {
                     this.owner.internalDictionary[this.key] = this.PreviousValue;
                 }
                 else
                 {
                     this.owner.internalDictionary.Remove(this.key);
                 }
             }
             if (this.waiters != null)
             {
                 flag = this.owner.internalDictionary.TryGetValue(this.key, out tValue);
             }
         }
         if (this.waiters != null)
         {
             while (this.waiters.Count > 0)
             {
                 ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter transactionalAsyncWaiter = this.waiters.Dequeue();
                 ActionItem.Schedule((object w) => ((ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter)w).Signal(flag, tValue), transactionalAsyncWaiter);
             }
         }
     });
 }
Пример #2
0
        public IAsyncResult BeginTryGetValue(TKey key, TimeSpan timeout, AsyncCallback callback, object state)
        {
            ConcurrentTransactionalDictionary <TKey, TValue> .TransactionInformation transactionInformation;
            TValue       tValue;
            IAsyncResult asyncResult;
            string       localIdentifier;
            Transaction  current = Transaction.Current;

            if (current != null)
            {
                localIdentifier = current.TransactionInformation.LocalIdentifier;
            }
            else
            {
                localIdentifier = null;
            }
            string str = localIdentifier;

            lock (this.syncLock)
            {
                if (!this.pendingTransactions.TryGetValue(key, out transactionInformation) || string.Equals(transactionInformation.TransactionId, str, StringComparison.OrdinalIgnoreCase))
                {
                    bool flag = this.internalDictionary.TryGetValue(key, out tValue);
                    return(new CompletedAsyncResult <bool, TValue>(flag, tValue, callback, state));
                }
                else
                {
                    ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter transactionalAsyncWaiter = new ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter(callback, state);

                    transactionInformation.Waiters.Enqueue(transactionalAsyncWaiter);
                    transactionalAsyncWaiter.StartTimer(timeout);
                    asyncResult = transactionalAsyncWaiter;
                }
            }
            return(asyncResult);
        }
Пример #3
0
 public static new Tuple <bool, TValue> End(IAsyncResult result)
 {
     ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter transactionalAsyncWaiter = AsyncResult.End <ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter>(result);
     return(new Tuple <bool, TValue>(transactionalAsyncWaiter.keyFound, transactionalAsyncWaiter.@value));
 }
Пример #4
0
            private static void OnWaiterSignaled(IAsyncResult result)
            {
                ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter asyncState = (ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter)result.AsyncState;
                Exception timeoutException = null;

                try
                {
                    if (!AsyncWaiter.End(result))
                    {
                        timeoutException = new TimeoutException();
                    }
                }
                catch (OperationCanceledException operationCanceledException)
                {
                    timeoutException = operationCanceledException;
                }
                asyncState.Complete(false, timeoutException);
            }
Пример #5
0
 public void Clear()
 {
     lock (this.syncLock)
     {
         foreach (ConcurrentTransactionalDictionary <TKey, TValue> .TransactionInformation value in this.pendingTransactions.Values)
         {
             if (!value.HasWaiters)
             {
                 continue;
             }
             while (value.Waiters.Count > 0)
             {
                 ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter transactionalAsyncWaiter = value.Waiters.Dequeue();
                 ActionItem.Schedule((object w) => ((ConcurrentTransactionalDictionary <TKey, TValue> .TransactionalAsyncWaiter)w).Cancel(), transactionalAsyncWaiter);
             }
         }
         this.internalDictionary.Clear();
         this.pendingTransactions.Clear();
     }
 }