コード例 #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);
                            }
                        }
                    }
                }
            }
        }