Split out TLS code into separate modules.

This allows the TLS code to be re-used by partial ports like EGL.