示例#1
0
        public void TestDebugFactoryRecursiveOptions()
        {
            DebugLockFactory <SimpleReadWriteLocking> factory = new DebugLockFactory <SimpleReadWriteLocking>(
                false, 0, 1, false, 1);

            using (ILockStrategy lck = factory.Create())
            {
                using (lck.Write())
                    using (lck.Write()) //second lock, allow recurse 1 time as per constructor
                    {
                        try { using (lck.Write()) { Assert.Fail(); } }
                        catch (Exception ex)
                        {
                            Assert.IsTrue(ex is DebugAssertionFailedException);//nesting prohibited by debug lock
                        }
                    }

                using (lck.Read())
                    using (lck.Read()) //second lock, allow recurse 1 time as per constructor
                    {
                        try { using (lck.Read()) { Assert.Fail(); } }
                        catch (Exception ex)
                        {
                            Assert.IsTrue(ex is DebugAssertionFailedException);//nesting prohibited by debug lock
                        }
                    }
            }
        }
 public virtual void TestWriteToReadRecursion()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write())
             using (l.Read())
                 using (l.Read())
                 { }
     }
 }
示例#3
0
 /// <summary>
 /// Gets or sets the element at the specified index.
 /// </summary>
 public T this[int index]
 {
     get
     {
         using (_lock.Read())
             return(_store[index]);
     }
     set
     {
         using (_lock.Write())
             _store[index] = value;
     }
 }
示例#4
0
            CacheEntry GetCache(NodeHandle handle, bool isNew)
            {
                Utils.WeakReference <CacheEntry> weakRef;
                CacheEntry entry = null;

                if (!isNew)
                {
                    if (handle.TryGetCache(out weakRef) && weakRef != null && weakRef.TryGetTarget(out entry))
                    {
                        return(entry);
                    }

                    using (_cacheLock.Read(base.Options.LockTimeout))
                    {
                        if (_cache.TryGetValue(handle, out weakRef))
                        {
                            if (!weakRef.TryGetTarget(out entry))
                            {
                                using (new SafeLock <DeadlockException>(weakRef))
                                {
                                    if (!weakRef.TryGetTarget(out entry))
                                    {
                                        weakRef.Target = entry = new CacheEntry(this, handle);
                                    }
                                    handle.SetCacheEntry(weakRef);
                                }
                            }
                        }
                    }
                }
                if (entry == null)
                {
                    using (_cacheLock.Write(base.Options.LockTimeout))
                    {
                        if (!_cache.TryGetValue(handle, out weakRef))
                        {
                            _cache.Add(handle, weakRef = new Utils.WeakReference <CacheEntry>(entry = new CacheEntry(this, handle)));
                            handle.SetCacheEntry(weakRef);
                        }
                        else
                        {
                            if (!weakRef.TryGetTarget(out entry))
                            {
                                using (new SafeLock <DeadlockException>(weakRef))
                                {
                                    if (!weakRef.TryGetTarget(out entry))
                                    {
                                        weakRef.Target = entry = new CacheEntry(this, handle);
                                    }
                                    handle.SetCacheEntry(weakRef);
                                }
                            }
                        }
                    }
                }
                Assert(entry != null, "Cache entry is null");
                _keepAlive.Add(entry);
                return(entry);
            }
 public void TestThreadedReadTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (new ThreadedWriter(l))
             using (l.Read(0))
             { }
     }
 }
示例#6
0
 public bool TryDequeue(out string value)
 {
     using (_writeLock.Read())
         using (_readLock.Write())
         {
             value = _read.ReadLine();
         }
     return(value != null);
 }
示例#7
0
 public void TestReadThenWrite()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Read())
         { }
         using (l.Write())
         { }
     }
 }
示例#8
0
 public void TestWriteThenReadWithTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write(0))
         { }
         using (l.Read(0))
         { }
     }
 }
示例#9
0
 public ReadLock BeginRequest()
 {
     try
     {
         return(_executionLock.Read(LockTimeout));
     }
     catch (Exception e)
     {
         throw new CorruptApplicationDomainException(e);
     }
 }
示例#10
0
 /// <summary>
 /// Gets or sets the element with the specified key.
 /// </summary>
 public TValue this[TKey key]
 {
     get
     {
         using (_lock.Read())
             return(_store[key]);
     }
     set
     {
         using (_lock.Write())
             _store[key] = value;
     }
 }
            public bool TryGetNode <TNode>(IStorageHandle handle, out TNode tnode, ISerializer <TNode> serializer)
            {
                if (_serializer == null)
                {
                    _serializer = (ISerializer <Node>)serializer;
                }
                StorageInfo info;
                Node        node;

                using (_lock.Read())
                {
                    if (_cache.TryGetValue(handle, out info))
                    {
                        tnode = (TNode)(object)info.Node;
                        return(true);
                    }
                }
                using (_lock.Write())
                {
                    if (_cache.TryGetValue(handle, out info))
                    {
                        tnode = (TNode)(object)info.Node;
                        Interlocked.Increment(ref info.RefCount);
                        _ordered.Enqueue(handle);
                        return(true);
                    }

                    if (!_store.TryGetNode(handle, out tnode, serializer))
                    {
                        return(false);
                    }
                    node = (Node)(object)tnode;
                    CacheAdd(handle, info = new StorageInfo(handle, node));

                    tnode = (TNode)(object)info.Node;
                    Interlocked.Increment(ref info.RefCount);
                    _ordered.Enqueue(handle);
                }
                return(true);
            }
 public void ReadToWriteFails()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (l.Read())
             Assert.IsFalse(l.TryWrite(10));
 }
示例#13
0
 public void TestReadWithTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (l.Read(0))
         { }
 }