Use scoped locks instead of CRITICAL_BLOCK

Backported from bitcoin commit f8dcd5ca6f55ad49807cf7491c1f153f6158400e