public RemoveParticipants ( int participantCount ) : void | ||
participantCount | int | The number of additional participants to remove from the barrier. |
return | void |
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();
}
}
/// <summary> /// Test RemoveParticipants /// </summary> /// <param name="initialCount">The initial barrier participants count</param> /// <param name="participantsToRemove">The aprticipants that will be added</param> /// <param name="exceptionType">Type of the exception in case of invalid input, null for valid cases</param> /// <returns>Tru if the test succeeded, false otherwise</returns> private static bool RunBarrierTest5_RemoveParticipants(int initialCount, int participantsToRemove, Type exceptionType) { TestHarness.TestLog("RemoveParticipants(" + initialCount + "," + participantsToRemove + ")"); Barrier b = new Barrier(initialCount); Exception exception = null; try { b.RemoveParticipants(participantsToRemove); if (b.ParticipantCount != initialCount - participantsToRemove) { TestHarness.TestLog("RemoveParticipants failed, total participant was not decreased"); return false; } } catch (Exception ex) { exception = ex; } if (exception != null && exceptionType == null) { TestHarness.TestLog("RemoveParticipants failed, unexpected exception has been thrown."); return false; } if (exception != null && !Type.Equals(exceptionType, exception.GetType())) { TestHarness.TestLog("RemoveParticipants failed, exceptions types do not match."); return false; } TestHarness.TestLog("RemoveParticipants succeeded"); return true; }