Merge in locking fixes into the main line.