Move thread allocation into state.

This is to give threads a way to reference each other and themselves by
thread index.
Add vm_thread::thread_index() to get a thread's index.