Fix use of TLS variables and use __gshared instead.