Attempt some thread safety.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>