<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//en">

<!–Converted with LaTeX2HTML 2022 (Released January 1, 2022) –> <HTML lang="en"> <HEAD> <TITLE>Contents of MPTHD: The Context Switching Layer</TITLE>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> <META NAME="viewport" CONTENT="width=device-width, initial-scale=1.0"> <META NAME="Generator" CONTENT="LaTeX2HTML v2022">

<LINK REL="STYLESHEET" HREF="MPTSK.css">

<LINK REL="next" HREF="node13_mn.html"> <LINK REL="previous" HREF="node11_mn.html"> <LINK REL="up" HREF="node9_mn.html"> <LINK REL="next" HREF="node13_mn.html"> </HEAD>

<BODY bgcolor="#ffffff" text="#000000" link="#9944EE" vlink="#0000ff" alink="#00ff00">

<H2><A ID="SECTION00043000000000000000"> MPTHD: The Context Switching Layer</A> </H2>

<P> Essential to any multitasker is the ability to switch the processor's context from one process to another. Traditional multitaskers do this by having interrupt handlers save the state of processor by pushing it on the stack when they are invoked. This is an efficient but complex and machine dependent approach. The MPTHD layer achieves the same effect in a portable way that is easy to understand.

<P> The MPTHD layer introduces the notion of an thread, and independent thread of execution. Like a normal process, a thread has its own stack on which it executes. With its own stack, the state of the thread can be saved, and then the thread later resumed (see illustration of internal MPTHD datatype).

<P> To perform this context switch, the thread must call the <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() function. Now the <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() function is not your ordinary function. The <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() function is special. Like an ordinary function, the <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() can return to you. Or it can return to anyone else you specify! Indeed, the <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() function can return to <EM>any</EM> active call of <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">(). You specify as an argument the thread you want it to return to, and returns from <EM> that</EM> thread's call of <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">(). In fact it tells that thread (via its returned argument) the thread that switched to them. (See illustration).

<P>

<DIV class="CENTER"><A ID="102"></A> <TABLE> <CAPTION class="BOTTOM"><STRONG>Figure:</STRONG> MPTHD: Context Switching Illustration</CAPTION> <TR><TD><IMG STYLE="height: 314.00ex; " SRC="img5.png" ALT="

\begin{figure}\begin{verbatim}Thread A Control Flow Thread B
-------...
...t :
: V :
Output: ''Hello, there!''
------\end{verbatim}
\end{figure}
"></TD></TR> </TABLE> </DIV>

<P> The <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() also works fine in a multiprocessor environment. If another processor is executing a thread when you switch to it, it will wait until the other processor switches out of the thread before you switch in. And if you switch to an invalid thread or one with a corrupted stack, <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() returns to you telling you that it came from <EM>you</EM> (not some other thread), which you may interpret as an error.

<P> The <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() interface is also portable. In many cases it can be directly implemented with ``C'''s standard <I>setjmp</I>() and <I>longjmp</I>() functions. In other languages, the function is quickly coded in assembly and ``linked in.'' Most every stack based-machines can support it (see internal representation).

<P>

<DIV class="CENTER"><A ID="108"></A> <TABLE> <CAPTION class="BOTTOM"><STRONG>Figure:</STRONG> MPTHD: Thread Datatype Internals</CAPTION> <TR><TD><IMG STYLE="height: 314.00ex; " SRC="img6.png" ALT="

\begin{figure}\par
\begin{verbatim}Semaphore (to control multiprocessor...
...
jmp_buf (for saving the thread's execution state)\end{verbatim}
\end{figure}
"></TD></TR> </TABLE> </DIV>

<P> With a few additional support functions, threads can be easily created and destroyed (see MAN page for exact details).

<P> Notice that the initialization function requires several (three) arguments: the memory buffer used to store the thread, the size of the buffer, and the function to execute. All this is awkward to specify when we set up a thread, so suppose we store a default size in some global variable. But, remember, we're in a concurrent environment: different tasks may change the default value, and different tasks may want different defaults. Therefore, the thread maintains a heap, private to that task. Instead, we can set up a global constant, an <EM>offset</EM> into the <EM>current thread's</EM> heap, containing our default buffer size. That way, each task has its own defaults, but refer to them with the <EM>same</EM> variable name. MPTHD automatically copies the parent's heap when initializing a new thread, so defaults are inherited. This simple feature can also be used for passing arguments from the parent to the child thread.

<P> So far so good. Now suppose we also don't want to specify a the buffer storing a thread &ndash; suppose, instead, MPTHD called malloc to dynamically allocate storage for us. Ahh, but storage is a <EM> resource</EM>. Not all languages have this resource (limiting portability). More importantly, dynamic memory allocation sometimes takes time &ndash; it must be interruptable. A high-level scheduler may switch into another thread which allocates memory while our thread is doing the same! Because memory allocation is a resource, this feature will have to wait for higher levels.

<P> In short, the MPTHD cluster, with the <!– MATH mpthd$\_switch$() –> <I>mpthd</I><IMG STYLE="height: 25.39ex; vertical-align: 151.40ex; " SRC="img4.png" ALT="$\_switch$">() function, is a simple and portable way to do context switching. The package provides examples of how it can be used, such as a tank game in which entrants submit ``C'' programs to control their tanks. Each ``program'' runs as an independent thread, with a ``referee'' thread scheduling the battle!

<P>

<HR>

</BODY> </HTML>