Re:A flexible, lightweight, spin-lock barrier
by gpderetta on Tuesday March 15, 2022 @03:13PM
(#30689848)
So, I understand that the difficulty is being able to reset the barrier without a quiescent period.
It seems to me that a counter being able to count to n is enough and there is no need of an explicit phase bit:
Wait(int *b, int n):
1: x = *b
if (x == n-1)
*b=0 //last, reset
else if cas(b, x, x+1)
2: if *b goto 2
else goto 1
For example, compare the thread counter part of the "v" value to the number of threads. If equal, do an atomic store which writes the opposite phase with a thread count of 0. If not equal, spin until the phase bit changes.
This approach avoids a division and can support threads that leave the barrier entirely. (And theoretically also entering the barrier, but that's always dicey regardless of the barrier implementation.)