Replacement for modifyMVarMasked for GHC <= 7.4.