Revert "Remove atomic flag"

As pointed out by @keithduncan, the wait/signal calls need to be balanced,
which was removed by this commit.

This reverts commit 6d4f595c33728915e3a76ff7638a4bc48b699e35.