%**start of header %\magnification=\magstep1 \input format.tex \input macros.tex \input pstricks \input pst-beta \def\tp{}\mark{\tp} %**end of header \def\Cstructure#1{\variable{#1}} We use the Pthreads library \Cite{pthreads} which offers user level threads on multiple platforms. Other thread packages that are freely available include Quick Threads \Cite{quickthreads} and the Pthreads library from the PART team at Florida State University. The MIT Pthreads library developed by Chris Provenzano seems to be the most mature and widely used. It should run on the following platforms \vbox{\halign{#\hfil \cr the i386 processor running NetBSD-1.x, FreeBSD-2.x, BSDOS-2.0, Linux-1.2 and Linux-1.3 \cr the r2000 (DECstation) running Ultrix-4.2 \cr the Sparc running NetBSD-1.x, SunOS-4.1.3, Solaris-2.3, and Solaris-2.4 \cr the alpha running OSF-2.3 and OSF-3.x \cr the SGI running IRIX-5.2 \cr the HPPA running HP/UX-9.x. \cr }} A properly designed version of a multi-threaded \S\ would require passing the thread specific information (data structures) through the calling hierarchy. For instance, \func{Eval} would be called relative to a specific instance of a thread and \Cstructure{Evaluator} structure. Each subsequent function call invoked by this \func{Eval} call would be passed this structure and memory frames etc. would be constructed relative to this \Cstructure{Evaluator}'s tree. The effort involved in rewriting the code for this model of evaluation is overwhelming and practically guarantees the addition of some complex bugs. Additionally, it is more appropriate to undertake this project in the context of a larger project - namely to permit threading, etc. for multiple operating systems (e.g. Windows NT, Windows 95 and Unix). We can avoid the need to modify many function signatures (to include the \Cstructure{Evaluator} instance) by maintaining a global instance of an \Cstructure{Evaluator} which contained the ``current'' evaluator. This would again require modifications to a large quantity of code in the way that the currently global variables are accessed. (E.g. references to \variable{Nframe} would become \variable{global_evaluator->Nframe}.) Again, this would add bugs to the existing version of the program. So what we propose to do is to maintain multiple variables which contain the values of the fields within the current evaluator. In other words, when an evaluator becomes ``current'', we swap the elements in its structure onto the global variables (e.g. \command{Nframe = current_evaluator->Nframe}) and proceed from there. This is the same idea used in the Pthreads library and the function \func{longjmp} where the stack is cached into a user-level structure (as opposed to a kernel structure) and another stack version is swapped onto the process's unique stack. In order to do this, we have identify the occurences within the process where an evaluator becomes ``current''. This can be done by modifying the Pthreads library to invoke specific code each time its scheduler swaps the current thread off the top of the queue and another thread onto the queue top. This can be achieved quite generally to handle the swapping of structures other than the \Cstructure{Evaluator} onto global variables. The manner in which we achieve this swapping is to create what amounts to a hash key (via \func{pthread_key_create}) and inserting a function into the thread's hash table to be called when this thread is swapped (via \func{pthread_setspecific}). To enable this behavior, we add two small code segments directly to the thread library. In the case of the MIT Pthreads library, we change \func{pthread_init} to call a local initialization function which creates the hash key for use by any of the threads that require variable swapping. \begintt int local_thread_init(void) { /* Initialize the key used to store and access the function which will be called when the scheduler switches between threads. This allows us to essentially perform some operations to swap the values of global variables. It's a hack to avoid rewriting tooooo many S functions. */ pthread_key_create(&thread_switch_hook_key,NULL); return(1); } \endtt and the insertion of the code \begintt fd_init(); local_thread_init(); /* Start the scheduler */ machdep_set_thread_timer(&(pthread_run->machdep_data)); \endtt Next we arrange for the swapping to take place whenever a the top of the thread queue is modified. This is done by modifying the function \func{machdep_restore_state} in the file \file{machdep.c}. \begintt void machdep_restore_state(void) { #ifdef USE_SWITCH if(pthread_run->specific_data_count > 0) { extern pthread_key_t thread_switch_hook_key; typedef void (*SwitchFunc)(void); SwitchFunc f; f = (SwitchFunc) pthread_getspecific(thread_switch_hook_key); if(f) { (*f)(); } } #endif longjmp(pthread_run->machdep_data.machdep_state, 1); } \endtt In addition to this, we may choose to run the thread switch hook function when the thread starts automatically from within the pthreads library. We have been able to compile and run the example program \file{fullcount.c} using both the Pthreads library and the \file{libc.5.3.12} for Linux which uses much of the Pthreads code. Additionally, we have been able to compile and run a complete distribution of \S\ based on \file{libc.5.3.12} To do this, we define the pre-processor variable \value{_MIT_POSIX_THREADS} (via \command{-D_MIT_POSIX_THREADS}) for the compilation stage. The link stage involves adding the \file{libpthreads.so} to the \file{ld} command. \bye