Factored out monitor locking/unlocking.