Creating Our Own Fibers 114 #[derive(Partial Eq, Eq, Debug)] enum State { Available, Running, Ready, } struct Thread { stack: Vec<u8>, ctx: Thread Context, state: State, } #[derive(Debug, Default)] #[repr(C)] struct Thread Context { rsp: u64, r15: u64, r14: u64, r13: u64, r12: u64, rbx: u64, rbp: u64, } Runtime is going to be our main entry point. We are basically going to create a very small runtime with a very simple scheduler and switch between our threads. The runtime holds an array of Thread structs and a current field to indicate which thread we are currently running. Thread holds data for a thread. The stack is similar to what we saw in our first example in earlier chapters. The ctx field is a context representing the data our CPU needs to resume where it left off on a stack and a state field that holds our thread state. State is an enum representing the states our threads can be in: Available means the thread is available and ready to be assigned a task if needed Running means the thread is running Ready means the thread is ready to move forward and resume execution Thread Context holds data for the registers that the CPU needs to resume execution on a stack.