In implementing the context switch (see code in mp*.h and mp*.c files), we use the powerful programming methodology of ``clustering'' similar code and data into one modular unit with defined interfaces.2 In this case, we create a thread cluster for initializing, manipulating, and deinitializing threads of execution. The interface for the thread cluster is in the file mpthd.h; you should include mpthd.h in your programs when you wish to have multiple threads of execution. The implementation and representation for the threads is in the file mpthd.c; your programs should not know about this file, and not make any assumptions about how threads are work beyond their interface. These good programming disciplines help build clean, well-defined interfaces between program modules – a big help when your testing, debugging, or modifying your code.
More generally, most *.h files are cluster interfaces (except
the sections marked INTERNAL USE ONLY), and their respective
*.c file (if it exists) the cluster implementation. Within the
clusters, all datatypes are in upper case (eg, MPTHD) and, if
associated with a cluster, are referenced externally by name (pointer)
and internally by value (contents); on the other hand, all procedures
are in lower case, and prefixed with the major datatype they operate
on (eg,
mpthd). To help prevent naming conflicts, all
global names are prefixed with the initials of their creator. In this
case, every name begins with mp for Mike Parker. There are
quite a number of other programming disciplines adhered to within the
code not relevant to context switching but worth looking at.
Though your programs may not know how threads are implemented, I expect a few humans are curious. So if you haven't already, take a moment now and read the overview and functional specification of the thread cluster in mpthd.h, and briefly scan its implementation in mpthd.c. Also glance at the two major clusters it depends on, mpsig.h and mpsem.h. Here's a map to help guide your way (see figure).
* * *
Since we can often understand what a program does (or can do) by understanding how it represents its data, I will cover the data structure of the thread cluster first. However, I will also cover its code structure, defining the functions available to access this data and how they work.