fix bad return value of gcc atomic compare exchange intrinsic, tidy up read write lock code a bit