next up previous contents
Next: Threads & Scheduling Up: Nachos Threads Previous: Nachos Threads

Mechanics of Thread Switching

Switching the CPU from one thread to another involves suspending the current thread, saving its state (e.g., registers), and then restoring the state of the thread being switched to. The thread switch actually completes at the moment a new program counter is loaded into PC; at that point, the CPU is no longer executing the thread switching code, it is executing code associated with the new thread.

The routine Switch(oldThread, nextThread) actually performs a thread switch. Switch saves all of oldThread's state (oldThread is the thread that is executing when Switch is called), so that it can resume executing the thread later, without the thread knowing it was suspended. Switch does the following:

  1. Save all registers in oldThread's context block.
  2. What address should we save for the PC? That is, when we later resume running the just-suspended thread, where do we want it to continue execution? We want execution to resume as if the call to Switch() had returned via the normal procedure call mechanism. Specifically, we want to resume execution at the instruction immediately following the call to Switch(). Thus, instead of saving the current PC, we place the return address (found on the stack in the thread's activation record) in the thread's context block. When the thread is resumed later, the resuming address loaded into the PC will be the instruction immediately following the ``call'' instruction that invoked Switch() earlier.

    Note: It is crucial that Switch() appear to be a regular procedure call to whoever calls it. That way, threads may call Switch() whenever they want. The call will appear to return just like a normal procedure call except that the return does not take place right away. Only after the scheduler decides to resume execution of the switched thread will it run again.

  3. Once the current thread's state has been saved, load new values into the registers from the context block of the next thread.
  4. At what exact point has a context switch taken place? When the current PC is replaced by the saved PC found in the process table. Once the saved PC is loaded, Switch() is no longer executing; we are now executing instructions associated with the new thread, which should be the instruction immediately following the call to Switch(). As soon as the new PC is loaded, a context switch has taken place.

The routine Switch() is written in assembly language because it is a machine-depended routine. It has to manipulate registers, look into the thread's stack, etc.

Note: After returning from Switch, the previous thread is no longer running. Thread nextThread is running now. But because it also called Switch() previously, it will return to the ``right place'' (the instruction after the call to Switch()). Thus, it looks like Switch() is a ``normal'' procedure call, but in fact, a thread switch has taken place.


next up previous contents
Next: Threads & Scheduling Up: Nachos Threads Previous: Nachos Threads

Thomas Narten
Mon Feb 3 15:00:27 EST 1997