public void RemoveParticipants(int participantCount)
{
// check dispose
ThrowIfDisposed();
// Validate input
if (participantCount < 1)
{
throw new ArgumentOutOfRangeException(nameof(participantCount), participantCount,
SR.Barrier_RemoveParticipants_NonPositive_ArgumentOutOfRange);
}
// in case of this is called from the PHA
if (_actionCallerID != 0 && Environment.CurrentManagedThreadId == _actionCallerID)
{
throw new InvalidOperationException(SR.Barrier_InvalidOperation_CalledFromPHA);
}
SpinWait spinner = new SpinWait();
while (true)
{
int currentTotal = _currentTotalCount;
int total;
int current;
bool sense;
GetCurrentTotal(currentTotal, out current, out total, out sense);
if (total < participantCount)
{
throw new ArgumentOutOfRangeException(nameof(participantCount),
SR.Barrier_RemoveParticipants_ArgumentOutOfRange);
}
if (total - participantCount < current)
{
throw new InvalidOperationException(SR.Barrier_RemoveParticipants_InvalidOperation);
}
// If the remaining participats = current participants, then finish the current phase
int remaingParticipants = total - participantCount;
if (remaingParticipants > 0 && current == remaingParticipants)
{
if (SetCurrentTotal(currentTotal, 0, total - participantCount, !sense))
{
FinishPhase(sense);
break;
}
}
else
{
if (SetCurrentTotal(currentTotal, current, total - participantCount, sense))
{
break;
}
}
spinner.SpinOnce();
}
}