using spinlock to control image read/writes

I'm working on a fragment shader to read and write to image arrays. So that only one shader invocation can access a given element in the image at once, I've implemented a spinlock image to control access to the images. The problem is that the spinlock keeps running into deadlock conditions. Here's my fragment shader code:

My understanding of imageAtomicCompSwap() in the above context is that only one shader invocation will read a value of zero from the image. In the same atomic operation, the image value will be set to 1. This in effect will lock out all other invocations, causing them to spin in the while loop until the invocation that has the lock sets the image value back to 0.

Why would this code cause a deadlock?

When the deadlock happens on my Windows 7 system, I get this message in a window:

"The NVIDIA OpenGL driver lost connection with the display driver due to exceeding the Windows Time-Out limit and is unable to continue.
The application must close."

I did a little research and the Windows Time-Out limit is two seconds.

I was able to find a fix for this problem. Apparently the Nvidia GLSL compiler (maybe all GLSL compilers do this), optimize away certain parts of the code that it deems unnecessary (especially looping structures). I'm not sure exactly what the compiler was doing but when I changed my code to the following, it worked (notice the use of keepWaiting flag and entire function code block being inside the while loop):

Thanks for that - I have had problems like this that seem to be resolved only by fooling the optimizer. (Optimizers used to be bad many years ago on cpu compilers - I guess it just takes a while for them to sort out these sort of errors)