示例#1
0
        private bool AddClientHoldingLock(ForsetiClient client)
        {
            while (true)
            {
                for (int i = 0; i < _clientsHoldingThisLock.Length; i++)
                {
                    AtomicReferenceArray <ForsetiClient> holders = _clientsHoldingThisLock[i];
                    if (holders == null)
                    {
                        holders = AddHolderArray(i);
                    }

                    for (int j = 0; j < holders.length(); j++)
                    {
                        ForsetiClient c = holders.get(j);
                        if (c == null)
                        {
                            // TODO This means we do CAS on each entry, very likely hitting a lot of failures until we
                            // TODO find a slot. We should look into better strategies here.
                            // TODO One such strategy could be binary searching for a free slot, and then linear scan
                            // TODO after that if the CAS fails on the slot we found with binary search.
                            if (holders.compareAndSet(j, null, client))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
        }
示例#2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReleaseSharedLock()
        public virtual void ShouldReleaseSharedLock()
        {
            // Given
            ForsetiClient clientA = mock(typeof(ForsetiClient));
            SharedLock    @lock   = new SharedLock(clientA);

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

            // Then
            assertThat(@lock.NumberOfHolders(), equalTo(0));
            assertThat(@lock.UpdateLock, equalTo(false));
        }
示例#3
0
 public override void CopyHolderWaitListsInto(SimpleBitSet waitList)
 {
     foreach (AtomicReferenceArray <ForsetiClient> holders in _clientsHoldingThisLock)
     {
         for (int j = 0; holders != null && j < holders.length(); j++)
         {
             ForsetiClient client = holders.get(j);
             if (client != null)
             {
                 client.CopyWaitListTo(waitList);
             }
         }
     }
 }
示例#4
0
 public override int DetectDeadlock(int clientId)
 {
     foreach (AtomicReferenceArray <ForsetiClient> holders in _clientsHoldingThisLock)
     {
         for (int j = 0; holders != null && j < holders.length(); j++)
         {
             ForsetiClient client = holders.get(j);
             if (client != null && client.IsWaitingFor(clientId))
             {
                 return(client.Id());
             }
         }
     }
     return(-1);
 }
示例#5
0
 private bool ClientHoldsThisLock(ForsetiClient client)
 {
     foreach (AtomicReferenceArray <ForsetiClient> holders in _clientsHoldingThisLock)
     {
         for (int j = 0; holders != null && j < holders.length(); j++)
         {
             ForsetiClient current = holders.get(j);
             if (current != null && current.Equals(client))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
示例#6
0
        public override bool Equals(object o)
        {
            if (this == o)
            {
                return(true);
            }
            if (o == null || this.GetType() != o.GetType())
            {
                return(false);
            }

            ForsetiClient that = ( ForsetiClient )o;

            return(_clientId == that._clientId);
        }
示例#7
0
        /// <summary>
        /// Create a new client to use to grab and release locks.
        /// </summary>
        public override Org.Neo4j.Kernel.impl.locking.Locks_Client NewClient()
        {
            // We check this volatile closed flag here, which may seem like a contention overhead, but as the time
            // of writing we apply pooling of transactions and in extension pooling of lock clients,
            // so this method is called very rarely.
            if (_closed)
            {
                throw new System.InvalidOperationException(this + " already closed");
            }

            ForsetiClient forsetiClient = _clientPool.acquire();

            forsetiClient.Reset();
            return(forsetiClient);
        }
示例#8
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));
        }
示例#9
0
 public override void CollectOwners(ISet <ForsetiClient> owners)
 {
     foreach (AtomicReferenceArray <ForsetiClient> ownerArray in _clientsHoldingThisLock)
     {
         if (ownerArray != null)
         {
             int len = ownerArray.length();
             for (int i = 0; i < len; i++)
             {
                 ForsetiClient owner = ownerArray.get(i);
                 if (owner != null)
                 {
                     owners.Add(owner);
                 }
             }
         }
     }
 }
示例#10
0
 public virtual bool TryAcquireUpdateLock(ForsetiClient client)
 {
     while (true)
     {
         int refs = _refCount.get();
         if (refs > 0)
         {
             if (_refCount.compareAndSet(refs, refs | _updateLockFlag))
             {
                 _updateHolder = client;
                 return(true);
             }
         }
         else
         {
             return(false);
         }
     }
 }
示例#11
0
        public override string DescribeWaitList()
        {
            StringBuilder sb = new StringBuilder("SharedLock[");

            foreach (AtomicReferenceArray <ForsetiClient> holders in _clientsHoldingThisLock)
            {
                bool first = true;
                for (int j = 0; holders != null && j < holders.length(); j++)
                {
                    ForsetiClient current = holders.get(j);
                    if (current != null)
                    {
                        sb.Append(first ? "" : ", ").Append(current.DescribeWaitList());
                        first = false;
                    }
                }
            }
            return(sb.Append("]").ToString());
        }
示例#12
0
        public virtual bool Acquire(ForsetiClient client)
        {
            // First, bump refcount to make sure no one drops this lock on the floor
            if (!AcquireReference())
            {
                return(false);
            }

            // Then add our wait list to the pile of things waiting in case if we are not there yet
            // if we already waiting we will release a reference to keep counter in sync
            if (!ClientHoldsThisLock(client))
            {
                // try to add client to a clients that holding current lock.
                return(AddClientHoldingLock(client));
            }
            else
            {
                ReleaseReference();
                return(false);
            }
        }
示例#13
0
        private void RemoveClientHoldingLock(ForsetiClient client)
        {
            foreach (AtomicReferenceArray <ForsetiClient> holders in _clientsHoldingThisLock)
            {
                if (holders == null)
                {
                    break;
                }

                for (int j = 0; j < holders.length(); j++)
                {
                    ForsetiClient current = holders.get(j);
                    if (current != null && current.Equals(client))
                    {
                        holders.set(j, null);
                        return;
                    }
                }
            }

            throw new System.InvalidOperationException(client + " asked to be removed from holder list, but it does not hold " + this);
        }
示例#14
0
 internal SharedLock(ForsetiClient client)
 {
     AddClientHoldingLock(client);
 }
示例#15
0
 internal ExclusiveLock(ForsetiClient owner)
 {
     this._owner = owner;
 }
示例#16
0
 public virtual bool Release(ForsetiClient client)
 {
     RemoveClientHoldingLock(client);
     return(ReleaseReference());
 }
示例#17
0
 public abstract bool shouldAbort(ForsetiClient clientThatsAsking, ForsetiClient clientWereDeadlockedWith);
示例#18
0
 public ReleaseExclusiveLocksAndClearSharedVisitor(ForsetiClient outerInstance)
 {
     this._outerInstance = outerInstance;
 }
示例#19
0
 public virtual void CleanUpdateHolder()
 {
     _updateHolder = null;
 }
示例#20
0
 public ReleaseSharedDontCheckExclusiveVisitor(ForsetiClient outerInstance)
 {
     this._outerInstance = outerInstance;
 }