locking.c: Refactor the main locking flow.