In one sense, yes, you can write the above code without a lock; however, I certainly agree with others in that there is a question of "why do away with the lock?" Optimizing to remove the lock may provide better performance, but is it worth your time and effort to specialize the code to the current requirements. With that said, __sync_bool_compare_and_swap
is a sufficient intrinsic to implement nearly any synchronization algorithm. For example:
do {
old = round_robin_next_;
if (old == hp_count() - 1) { new = 0; }
else { new = old + 1; }
} while (!__sync_bool_compare_and_swap(&round_robin_next_, old, new));
The intrinsic atomically evaluates whether the memory location still contains old, and if so, then it stores new into that location. If the location did not still contain old, then false is returned and the update is attempted again.