示例#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 ThreadSafeWorkingMemory(bool supportHotSwap, int lockTimeOut)
        {
            this.lockTimeOut = lockTimeOut;

            if (supportHotSwap) lockStrategy = new ReaderWriterLockStrategy();
            else lockStrategy = new NoLockStrategy();
        }
示例#3
0
 public void TestWriteLockTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (new ThreadedWriter(l))
             using (WriteLock w = new WriteLock(l, 0))
                 Assert.IsFalse(w.HasWriteLock);
 }
示例#4
0
 public void TestReadLockTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (new ThreadedWriter(l))
             using (ReadLock r = new ReadLock(l, 0))
                 Assert.IsFalse(r.HasReadLock);
 }
        public override void TestWriteCounter()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                Assert.AreEqual(0, l.WriteVersion);

                Assert.IsTrue(l.TryRead(0));
                l.ReleaseRead();
                Assert.AreEqual(0, l.WriteVersion);

                Assert.IsTrue(l.TryWrite(0));
                Assert.AreEqual(0, l.WriteVersion);
                l.ReleaseWrite();
                Assert.AreEqual(0, l.WriteVersion);

                using (l.Write())
                {
                    Assert.AreEqual(0, l.WriteVersion);
                    Assert.IsTrue(l.TryWrite(0));
                    // Once a nested write lock is acquired the real lock is obtained.
                    Assert.AreEqual(1, l.WriteVersion);
                    l.ReleaseWrite();
                    Assert.AreEqual(1, l.WriteVersion);
                }

                Assert.IsTrue(l.TryWrite(0));
                l.ReleaseWrite();
                Assert.AreEqual(1, l.WriteVersion);
            }
        }
示例#6
0
            protected Node CreateRoot(NodeHandle rootHandle)
            {
                NodeHandle hChild;

                using (NodeTransaction t = BeginTransaction())
                {
                    using (NodePin child = t.Create(LockType.Insert, true))
                    {
                        hChild = child.Handle;
                        t.Commit();
                    }
                }

                object   refobj;
                RootNode rootNode = new RootNode(rootHandle.StoreHandle);

                ILockStrategy lck = CreateLock(rootHandle, out refobj);

                using (lck.Write(Options.LockTimeout))
                {
                    using (NodePin rootPin = new NodePin(rootHandle, lck, LockType.Insert, LockType.Insert, refobj, rootNode, null))
                        using (NodeTransaction t = BeginTransaction())
                        {
                            rootNode = (RootNode)t.BeginUpdate(rootPin);
                            rootNode.ReplaceChild(0, null, hChild);
                            t.Commit();
                        }
                }

                return(rootNode);
            }
示例#7
0
 public void TestYouCanDisposeWriteLock()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (IDisposable w = l.Write())
         {
             w.Dispose();//since the using statement has the same boxed pointer to w, we are allowed to dispose
         }
 }
示例#8
0
 public void TestYouCanDisposeReadLock()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (IDisposable r = ReadLock.Acquire(l, 0))
         {
             r.Dispose();//since the using statement has the same boxed pointer to r, we are allowed to dispose
         }
 }
示例#9
0
 public void TestTryWrite()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         Assert.IsTrue(l.TryWrite(0));
         l.ReleaseWrite();
     }
 }
 public StorageCache(INodeStorage store, int sizeLimit)
 {
     _store     = store;
     _sizeLimit = sizeLimit;
     _lock      = new SimpleReadWriteLocking();
     _cache     = new Dictionary <IStorageHandle, StorageInfo>();
     _ordered   = new Queue <IStorageHandle>();
 }
 /// <summary> Acquires the lock within the timeout or throws TimeoutException </summary>
 /// <exception cref="System.TimeoutException"/>
 public static WriteLock Acquire(ILockStrategy lck, int timeout)
 {
     if (!lck.TryWrite(timeout))
     {
         throw new TimeoutException();
     }
     return(new WriteLock(lck, true));
 }
示例#12
0
 public void TestIdiotWriterUsesDispose()
 {
     using (ILockStrategy l = LockFactory.Create())
         using (WriteLock w = new WriteLock(l, 0))
         {
             Assert.IsTrue(w.HasWriteLock);
             ((IDisposable)w).Dispose(); //You cannot do this, the using statement has a 'copy' of ReadLock, don't call dispose.
         }
 }
示例#13
0
        public void TestWriteRecursion()
        {
            ILockStrategy l = LockFactory.Create();

            using (l.Write())
                using (l.Write())
                    using (l.Write())
                    { }
        }
 public void TestThreadedReadTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (new ThreadedWriter(l))
             using (l.Read(0))
             { }
     }
 }
示例#15
0
 public void NoWriteIncrement()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         Assert.AreEqual(0, l.WriteVersion);
         using (l.Write())
             Assert.AreEqual(0, l.WriteVersion);
         Assert.AreEqual(0, l.WriteVersion);
     }
 }
        public virtual void TestReaderAllowsReader()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                using (new ThreadedReader(l))
                    Assert.IsTrue(l.TryRead(0));

                l.ReleaseRead();
            }
        }
 public virtual void TestWriteToReadRecursion()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write())
             using (l.Read())
                 using (l.Read())
                 { }
     }
 }
示例#18
0
 public void TestWriteThenReadWithTimeout()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Write(0))
         { }
         using (l.Read(0))
         { }
     }
 }
示例#19
0
 public void TestReadThenWrite()
 {
     using (ILockStrategy l = LockFactory.Create())
     {
         using (l.Read())
         { }
         using (l.Write())
         { }
     }
 }
示例#20
0
 public ThreadSafeWorkingMemory(bool supportHotSwap)
 {
     if (supportHotSwap)
     {
         lockStrategy = new ReaderWriterLockStrategy();
     }
     else
     {
         lockStrategy = new NoLockStrategy();
     }
 }
 public NodePin(NodeHandle handle, ILockStrategy lck, LockType ltype, LockType lockHeld, object refobj, Node original, Node updated)
 {
     Assert(original == null || original.IsReadOnly);
     _handle    = handle;
     _lock      = lck;
     _ltype     = ltype;
     _lockHeld  = lockHeld;
     _ptr       = original;
     _temp      = updated;
     _reference = refobj;
 }
示例#22
0
            public NodePin Create(LockType ltype, bool isLeaf)
            {
                IStorageHandle storeHandle = _cache.Storage.Create();
                NodeHandle     handle      = new NodeHandle(storeHandle);
                object         refobj;
                ILockStrategy  lck = _cache.CreateLock(handle, out refobj);

                int     size = isLeaf ? _cache.Options.MaximumValueNodes : _cache.Options.MaximumChildNodes;
                NodePin pin  = new NodePin(handle, lck, ltype, LockType.Insert, refobj, null, new Node(handle.StoreHandle, size));

                NodePin.Append(ref _created, pin);
                return(pin);
            }
示例#23
0
 protected void DoubleWriteLock()
 {
     using (ILockStrategy strategy = this.GetLockStrategy())
     {
         using (var writeLock1 = strategy.GetWriteLock())
         {
             using (var writeLock2 = strategy.GetWriteLock())
             {
                 // do nothing
             }
         }
     }
 }
示例#24
0
文件: Chain.cs 项目: DaisyFx/DaisyFx
 public Chain(string name, ILockStrategy lockStrategy, IConnector <T> rootConnector,
              IConnector[] connectors, ISourceConnector <T>[] sourceConnectors, IServiceProvider serviceProvider,
              Queue <InitDelegate> initQueue)
 {
     _serviceProvider  = serviceProvider;
     _logger           = serviceProvider.GetRequiredService <ILogger <IChain> >();
     _initQueue        = initQueue;
     Name              = name;
     LockStrategy      = lockStrategy;
     _root             = rootConnector;
     Connectors        = connectors;
     _sourceConnectors = sourceConnectors;
 }
示例#25
0
        public ThreadSafeWorkingMemory(bool supportHotSwap, int lockTimeOut)
        {
            this.lockTimeOut = lockTimeOut;

            if (supportHotSwap)
            {
                lockStrategy = new ReaderWriterLockStrategy();
            }
            else
            {
                lockStrategy = new NoLockStrategy();
            }
        }
示例#26
0
 public void DoubleWriteLockThrows()
 {
     using (ILockStrategy strategy = GetLockStrategy())
     {
         using (var writeLock1 = strategy.GetWriteLock())
         {
             using (var writeLock2 = strategy.GetWriteLock())
             {
                 //do nothing
             }
         }
     }
 }
示例#27
0
        public void TestThreadedTryWrite()
        {
            using (ILockStrategy l = LockFactory.Create())
            {
                Assert.IsTrue(l.TryWrite(0));
                l.ReleaseWrite();

                using (new ThreadedWriter(l))
                    Assert.IsFalse(l.TryWrite(0));

                Assert.IsTrue(l.TryWrite(0));
                l.ReleaseWrite();
            }
        }
示例#28
0
      // IDisposable
      protected virtual void Dispose(bool disposing)
      {
          if (!_isDisposed)
          {
              if (disposing)
              {
                  // dispose managed state (managed objects).
              }

              _lockStrategy.Dispose();
              _lockStrategy = null;
          }
          _isDisposed = true;
      }
示例#29
0
        public ContentState(ContentStorage content)
        {
            //_executionLock = new SimpleReadWriteLocking();
            _executionLock = IgnoreLocking.Instance;
            _rsaKeyPair    = ReadKeyFile();
            _content       = content ?? ReadCurrent();

            _channel = new IpcEventChannel(Path.Combine(Settings.RegistryPath, "IISChannel"),
                                           BitConverter.ToString(Hash.MD5(Encoding.UTF8.GetBytes(StoragePath)).ToArray()));

            _channel.OnError += (o, e) => Log.Error(e.GetException());
            _channel[Events.ContentUpdate].OnEvent += OnContentUpdated;
            _channel[Events.CompletionAck].OnEvent += (o, e) => { };
            _channel.StartListening();
        }
示例#30
0
        public ThreadedWriter(ILockStrategy lck)
        {
            _started  = new ManualResetEvent(false);
            _complete = new ManualResetEvent(false);

            _lck      = lck;
            _delegate = HoldLock;
            _async    = _delegate.BeginInvoke(null, null);
            if (!_started.WaitOne(1000, false))
            {
                _delegate.EndInvoke(_async);
                Assert.Fail("Unable to acquire lock");
            }
            Assert.IsTrue(_locked);
        }
示例#31
0
            public override void ResetCache()
            {
                _keepAlive.Clear();
                _cache.Clear();
                _cacheLock = new ReaderWriterLocking();
                _root      = GetCache(_root.Handle, true);

                bool isnew;

                Assert(_root.Handle.StoreHandle.Equals(Storage.OpenRoot(out isnew)));
                if (isnew)
                {
                    _root.Node = CreateRoot(_root.Handle);
                }
            }
        public ThreadedWriter(ILockStrategy lck)
        {
            _started = new ManualResetEvent(false);
            _complete = new ManualResetEvent(false);

            _lck = lck;
            _delegate = HoldLock;
            _async = _delegate.BeginInvoke(null, null);
            if (!_started.WaitOne(1000, false))
            {
                _delegate.EndInvoke(_async);
                Assert.Fail("Unable to acquire lock");
            }
            Assert.IsTrue(_locked);
        }
示例#33
0
        public ContentState(ContentStorage content)
        {
            //_executionLock = new SimpleReadWriteLocking();
            _executionLock = IgnoreLocking.Instance;
            _rsaKeyPair = ReadKeyFile();
            _content = content ?? ReadCurrent();

            _channel = new IpcEventChannel(Path.Combine(Settings.RegistryPath, "IISChannel"),
                BitConverter.ToString(Hash.MD5(Encoding.UTF8.GetBytes(StoragePath)).ToArray()));

            _channel.OnError += (o, e) => Log.Error(e.GetException());
            _channel[Events.ContentUpdate].OnEvent += OnContentUpdated;
            _channel[Events.CompletionAck].OnEvent += (o, e) => { };
            _channel.StartListening();
        }
示例#34
0
        public TextQueue(string queueFile, bool synchronized)
        {
            Stream output = new FileStream(queueFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
            output.Seek(0, SeekOrigin.End);
            _write = new StreamWriter(output, Encoding.UTF8);

            Stream input = new FileStream(queueFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            _read = new StreamReader(input, Encoding.UTF8);

            if (synchronized)
            {
                _writeLock = new SimpleReadWriteLocking();
                _readLock = new ExclusiveLocking();
            }
            else
            {
                _writeLock = _readLock = IgnoreLocking.Instance;
            }
        }
 public ThreadedReader(ILockStrategy lck)
     : base(lck)
 {
 }
 /// <summary> Tracks an existing read lock on a resource </summary>
 public ReadLock(ILockStrategy lck, bool locked)
 {
     _lock = lck;
     _hasLock = locked;
 }
示例#37
0
 public ThreadSafeWorkingMemory(bool supportHotSwap)
 {
     if (supportHotSwap) lockStrategy = new ReaderWriterLockStrategy();
     else lockStrategy = new NoLockStrategy();
 }
 /// <summary> Tracks an existing read lock on a resource </summary>
 public WriteLock(ILockStrategy lck, bool locked)
 {
     _lock = lck;
     _hasLock = locked;
 }
 /// <summary> Acquires a read lock on the resource </summary>
 public WriteLock(ILockStrategy lck, int timeout)
 {
     _lock = lck;
     _hasLock = lck.TryWrite(timeout);
 }
 /// <summary> Acquires a read lock on the resource </summary>
 public ReadLock(ILockStrategy lck, int timeout)
 {
     _lock = lck;
     _hasLock = lck.TryRead(timeout);
 }
 /// <summary> Acquires the lock within the timeout or throws TimeoutException </summary>
 /// <exception cref="System.TimeoutException"/>
 public static WriteLock Acquire(ILockStrategy lck, int timeout)
 {
     if(!lck.TryWrite(timeout)) throw new TimeoutException();
     return new WriteLock(lck, true);
 }