making torch thread-safe, part of the patch inspired by @pasky

This pull request is different from earlier work by @pasky https://github.com/pasky/torch in the following ways:
* For generating random tensors, it caches the thread-local-storage pointer so that you can avoid mutex bottlenecks
* It works with Windows (haven't tested it, but should work)
* PRNG works with Android (where __thread is not supported, instead I use pthread_set/getspecific), error handlers wont yet.

I only got the chance to test it on OSX Mavericks, please test it on multiple platforms before considering merging it.