20% better than ReentrantLock? Inline classes are awesome

I don’t understand how

assert(ref.get() === thread) { "reentrancy is not supported" }

works.

If

if (ref.compareAndSet(null, thread)) { 

goes to the else branch then ref.get() may return

  • either another Thread reference (which holds the lock currently), so the assert() will fail
  • or it may return even null (if the lock was unlocked between the if and the ref.get() calls), so the assert() will also fail