Refactor threadlocal code.
Store execution context on a threadlocal.