Is my spin lock implementation correct and optimal?
Looks fine to me. Btw, here is the textbook implementation that is more efficient even in the contended case. void lock(volatile int *exclusion) { while (__sync_lock_test_and_set(exclusion, 1)) while (*exclusion) ; }