Lock mutex on function entry, defer unlock.