MySQL concurrency with GET_LOCK

Vi Veri Veniversum Vivus Vici

I trying to use GET_LOCK for application-level blocking in my application. So I research how to do it in propert way. As I can see, IS_FREE_LOCK function is non-atomic, but GET_LOCK with second parameter (timeout) equal to 0 is works very good. So we should to use two checking steps: IS_FREE_LOCK as first and then GET_LOCK($lockId, 0) === 1.

First, we need to create code like this (its not ideal, and hasnt error handling: its a just proof-o-concept):

As you can see, lot of sсripts can to catch lock as "free" at same time, but only one can actually catch it with GET_LOCK. Anyway, a lot of iterations are skipped by IS_FREE_LOCK check step - its fast and enough to skip non-concurrency scripts.