More robust lock counting (bug 874)