示例#1
0
        public override bool TryExclusiveLock(ResourceType resourceType, long resourceId)
        {
            _hasLocks = true;
            _stateHolder.incrementActiveClients(this);

            try
            {
                ConcurrentMap <long, ForsetiLockManager.Lock> lockMap = _lockMaps[resourceType.TypeId()];
                MutableLongIntMap heldLocks = _exclusiveLockCounts[resourceType.TypeId()];

                int heldCount = heldLocks.getIfAbsent(resourceId, -1);
                if (heldCount != -1)
                {
                    // We already have a lock on this, just increment our local reference counter.
                    heldLocks.put(resourceId, Math.incrementExact(heldCount));
                    return(true);
                }

                // Grab the global lock
                ForsetiLockManager.Lock @lock;
                if ((@lock = lockMap.putIfAbsent(resourceId, _myExclusiveLock)) != null)
                {
                    if (@lock is SharedLock && _sharedLockCounts[resourceType.TypeId()].containsKey(resourceId))
                    {
                        SharedLock sharedLock = ( SharedLock )@lock;
                        if (sharedLock.TryAcquireUpdateLock(this))
                        {
                            if (sharedLock.NumberOfHolders() == 1)
                            {
                                heldLocks.put(resourceId, 1);
                                return(true);
                            }
                            else
                            {
                                sharedLock.ReleaseUpdateLock();
                                return(false);
                            }
                        }
                    }
                    return(false);
                }

                heldLocks.put(resourceId, 1);
                return(true);
            }
            finally
            {
                _stateHolder.decrementActiveClients();
            }
        }
示例#2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldUpgradeToUpdateLock()
        public virtual void ShouldUpgradeToUpdateLock()
        {
            // Given
            ForsetiClient clientA = mock(typeof(ForsetiClient));
            ForsetiClient clientB = mock(typeof(ForsetiClient));

            SharedLock @lock = new SharedLock(clientA);

            @lock.Acquire(clientB);

            // When
            assertTrue(@lock.TryAcquireUpdateLock(clientA));

            // Then
            assertThat(@lock.NumberOfHolders(), equalTo(2));
            assertThat(@lock.UpdateLock, equalTo(true));
        }
示例#3
0
        /// <summary>
        /// Attempt to upgrade a share lock that we hold to an exclusive lock. </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private boolean tryUpgradeToExclusiveWithShareLockHeld(org.neo4j.storageengine.api.lock.LockTracer tracer, org.neo4j.storageengine.api.lock.LockWaitEvent priorEvent, org.neo4j.storageengine.api.lock.ResourceType resourceType, long resourceId, SharedLock sharedLock, int tries, long waitStartMillis) throws org.neo4j.storageengine.api.lock.AcquireLockTimeoutException
        private bool TryUpgradeToExclusiveWithShareLockHeld(LockTracer tracer, LockWaitEvent priorEvent, ResourceType resourceType, long resourceId, SharedLock sharedLock, int tries, long waitStartMillis)
        {
            if (sharedLock.TryAcquireUpdateLock(this))
            {
                LockWaitEvent waitEvent = null;
                try
                {
                    // Now we just wait for all clients to release the the share lock
                    while (sharedLock.NumberOfHolders() > 1)
                    {
                        AssertValid(waitStartMillis, resourceType, resourceId);
                        if (waitEvent == null && priorEvent == null)
                        {
                            waitEvent = tracer.WaitForLock(true, resourceType, resourceId);
                        }
                        WaitFor(sharedLock, resourceType, resourceId, true, tries++);
                    }

                    return(true);
                }
                catch (Exception e)
                {
                    sharedLock.ReleaseUpdateLock();
                    if (e is DeadlockDetectedException || e is LockClientStoppedException)
                    {
                        throw ( Exception )e;
                    }
                    throw new TransactionFailureException("Failed to upgrade shared lock to exclusive: " + sharedLock, e);
                }
                finally
                {
                    if (waitEvent != null)
                    {
                        waitEvent.Close();
                    }
                    ClearWaitList();
                    _waitingForLock = null;
                }
            }
            return(false);
        }