TH error handlers: Take a context-providing data variable

This allows us to get rid of the hacky globalL global variable.
Make all global variables live in thread-local storage

We use the gcc extension __thread to specify the TLS storage class.