Threading fixes

Valgrind has a thread error detection tool called Helgrind.  This series adds special support to ignore known-safe threading issues and also fixes several non-safe threading problems.