Fix TLS keys and safetyness.

TLS function-keys are replaced by static variables.
Unsafe blocks are removed.