Пример #1
0
        internal bool Read(FlagArray flags, ref int owner, out LockSlot <T> slot)
        {
            if (Read(ref owner, out slot))
            {
                return(true);
            }
            var resultLock = -1;

            foreach (var flag in flags.Flags)
            {
                LockSlot <T> testSlot;
                if (!_slots.TryGet(flag, out testSlot))
                {
                    continue;
                }
                if (ReferenceEquals(slot, null) || slot.CompareTo(testSlot) < 0)
                {
                    slot       = testSlot;
                    resultLock = flag;
                }
            }
            if (Interlocked.CompareExchange(ref owner, resultLock, -1) != -1)
            {
                return(Read(ref owner, out slot));
            }
            if (slot == null)
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
        internal bool Read(FlagArray flags, ref int owner, out LockSlot <T> slot)
        {
            if (Read(ref owner, out slot))
            {
                return(true);
            }
            var resultLock = -1;

            foreach (var flag in flags.Flags)
            {
                if (!_slots.TryGet(flag, out var testSlot))
                {
                    continue;
                }

                if (slot?.CompareTo(testSlot) >= 0)
                {
                    continue;
                }

                slot       = testSlot;
                resultLock = flag;
            }
            if (Interlocked.CompareExchange(ref owner, resultLock, -1) != -1)
            {
                return(Read(ref owner, out slot));
            }
            return(slot != null);
        }
Пример #3
0
 public bool Equals(LockSlot <T> other)
 {
     if (other == null)
     {
         return(false);
     }
     return(_versionToken.Equals(other._versionToken));
 }
Пример #4
0
 public int CompareTo(LockSlot <T> other)
 {
     if (ReferenceEquals(other, null))
     {
         return(1);
     }
     return(_versionToken.CompareTo(other._versionToken));
 }
Пример #5
0
 private bool TryClaimFreeSlot(out LockSlot <T> slot)
 {
     if (_closedSlots.TryTake(out slot))
     {
         slot.Open(_version.AdvanceNewToken());
         return(true);
     }
     return(false);
 }
Пример #6
0
 internal bool ClaimSlot(out LockSlot <T> slot)
 {
     if (TryClaimFreeSlot(out slot))
     {
         return(true);
     }
     if (_slots.Count < _slots.Capacity)
     {
         var index = Interlocked.Increment(ref _index) & (Capacity - 1);
         slot = _slots.Get(index);
         return(true);
     }
     slot = null;
     return(false);
 }
Пример #7
0
        private bool Read(ref int owner, out LockSlot <T> slot)
        {
            slot = null;
            var got = owner;

            if (got == -1)
            {
                return(false);
            }
            if (_slots.TryGet(got, out var found) && found.IsOpen)
            {
                slot = found;
                return(true);
            }
            Interlocked.CompareExchange(ref owner, -1, got);
            return(false);
        }
Пример #8
0
 internal void Uncapture(LockSlot <T> slot)
 {
     Interlocked.CompareExchange(ref _owner, -1, slot.Id);
     _capture[slot.Id] = false;
 }
Пример #9
0
 internal void Capture(LockSlot <T> slot)
 {
     _capture[slot.Id] = true;
 }
Пример #10
0
 internal void Close(LockSlot <T> slot)
 {
     _closedSlots.TryAdd(slot);
 }
Пример #11
0
 public bool Equals(LockSlot <T> other)
 {
     return(!(other is null) && _versionToken.Equals(other._versionToken));
 }
Пример #12
0
 public int CompareTo(LockSlot <T> other)
 {
     return(other is null ? 1 : _versionToken.CompareTo(other._versionToken));
 }