long count = GetCount(head, headHead, tail, tailTail);
if (index > array.Length - count)
{
- throw new ArgumentException(); // TODO: finish this
+ throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall"));
}
// Copy the items to the target array
// We can dequeue from this slot if it's been filled by an enqueuer, which
// would have left the sequence number at pos+1.
- if (sequenceNumber == currentHead + 1)
+ int diff = sequenceNumber - (currentHead + 1);
+ if (diff == 0)
{
// We may be racing with other dequeuers. Try to reserve the slot by incrementing
// the head. Once we've done that, no one else will be able to read from this slot,
return true;
}
}
- else if (sequenceNumber < currentHead + 1)
+ else if (diff < 0)
{
// The sequence number was less than what we needed, which means this slot doesn't
// yet contain a value we can dequeue, i.e. the segment is empty. Technically it's
// We can peek from this slot if it's been filled by an enqueuer, which
// would have left the sequence number at pos+1.
- if (sequenceNumber == currentHead + 1)
+ int diff = sequenceNumber - (currentHead + 1);
+ if (diff == 0)
{
result = resultUsed ? _slots[slotsIndex].Item : default(T);
return true;
}
- else if (sequenceNumber < currentHead + 1)
+ else if (diff < 0)
{
// The sequence number was less than what we needed, which means this slot doesn't
// yet contain a value we can peek, i.e. the segment is empty. Technically it's
// The slot is empty and ready for us to enqueue into it if its sequence
// number matches the slot.
- if (sequenceNumber == currentTail)
+ int diff = sequenceNumber - currentTail;
+ if (diff == 0)
{
// We may be racing with other enqueuers. Try to reserve the slot by incrementing
// the tail. Once we've done that, no one else will be able to write to this slot,
return true;
}
}
- else if (sequenceNumber < currentTail)
+ else if (diff < 0)
{
// The sequence number was less than what we needed, which means this slot still
// contains a value, i.e. the segment is full. Technically it's possible that multiple