Improve locking strategy.

Suggested by:	Dmitry S. Luhtionov