Exemplo n.º 1
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);
            }
            protected override void LoadStorage()
            {
                bool isNew;

                _root = new NodeHandle(Storage.OpenRoot(out isNew));
                _root.SetCacheEntry(LockFactory.Create());
                if (isNew)
                {
                    CreateRoot(_root);
                }

                Node rootNode;

                if (Storage.TryGetNode(_root.StoreHandle, out rootNode, NodeSerializer))
                {
                    _root.SetCacheEntry(LockFactory.Create());
                }

                Assert(rootNode != null, "Unable to load storage root.");
            }
Exemplo n.º 3
0
            public override ILockStrategy CreateLock(NodeHandle handle, out object refobj)
            {
                NodeWithLock nlck = new NodeWithLock(null, LockFactory.Create());

                handle.SetCacheEntry(nlck);
                refobj = nlck;
                bool acquired = nlck.Lock.TryWrite(base.Options.LockTimeout);

                DeadlockException.Assert(acquired);
                return(nlck.Lock);
            }
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                ILockStrategy lck;

                if (!child.TryGetCache(out lck))
                {
                    using (_lock.Write(base.Options.LockTimeout))
                    {
                        if (!_list.TryGetValue(child.StoreHandle, out lck))
                        {
                            _list.Add(child.StoreHandle, lck = LockFactory.Create());
                            child.SetCacheEntry(lck);
                        }
                    }
                }

                bool success;

                if (ltype == LockType.Read)
                {
                    success = lck.TryRead(base.Options.LockTimeout);
                }
                else
                {
                    success = lck.TryWrite(base.Options.LockTimeout);
                }
                DeadlockException.Assert(success);
                try
                {
                    Node node;
                    success = Storage.TryGetNode(child.StoreHandle, out node, NodeSerializer);
                    Assert(success && node != null);

                    return(new NodePin(child, lck, ltype, ltype, lck, node, null));
                }
                catch
                {
                    if (ltype == LockType.Read)
                    {
                        lck.ReleaseRead();
                    }
                    else
                    {
                        lck.ReleaseWrite();
                    }
                    throw;
                }
            }
Exemplo n.º 5
0
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                NodeWithLock nlck;

                if (!child.TryGetCache(out nlck))
                {
                    child.SetCacheEntry(nlck = new NodeWithLock(null, LockFactory.Create()));
                }

                bool acquired;

                if (ltype == LockType.Read)
                {
                    acquired = nlck.Lock.TryRead(base.Options.LockTimeout);
                }
                else
                {
                    acquired = nlck.Lock.TryWrite(base.Options.LockTimeout);
                }
                DeadlockException.Assert(acquired);
                try
                {
                    if (nlck.Node == null)
                    {
                        using (new SafeLock <DeadlockException>(nlck, base.Options.LockTimeout))
                            Storage.TryGetNode(child.StoreHandle, out nlck.Node, NodeSerializer);
                    }

                    Assert(nlck.Node != null);
                    return(new NodePin(child, nlck.Lock, ltype, ltype, nlck, nlck.Node, null));
                }
                catch
                {
                    if (ltype == LockType.Read)
                    {
                        nlck.Lock.ReleaseRead();
                    }
                    else
                    {
                        nlck.Lock.ReleaseWrite();
                    }
                    throw;
                }
            }
            public override ILockStrategy CreateLock(NodeHandle handle, out object refobj)
            {
                ILockStrategy lck;

                using (_lock.Write(base.Options.LockTimeout))
                {
                    if (!_list.TryGetValue(handle.StoreHandle, out lck))
                    {
                        _list.Add(handle.StoreHandle, lck = LockFactory.Create());
                        handle.SetCacheEntry(lck);
                    }
                }

                refobj = null;
                bool acquired = lck.TryWrite(base.Options.LockTimeout);

                DeadlockException.Assert(acquired);
                return(lck);
            }