Thread local

series of commits to make torch/TH thread-safe