home *** CD-ROM | disk | FTP | other *** search
- MPTHD(3C) "MP-UNIX" Programmer's Manual MPTHD(3C)
-
-
-
- NAME
- mpthd - cluster for managing multiple threads of execution
-
-
- SYNOPSIS
- #include "mpthd.h" /* (also link in mpthd libarary) */
-
- typedef struct _MPTHD MPTHD, PTR MPTHDID;
- typedef MPTHDID (*MPTHDFN)(MPTHDID callingmpthd);
-
- MPTHDID mpthd_setup(VOIDPTR buf, UINT size);
- void _mpthd_setup(MPTHDID mainmpthd);
-
- MPTHDID mpthd_init(VOIDPTR buff, UINT size, MPTHDFN mpthdfn);
- VOIDPTR mpthd_dinit(MPTHDID mpthdtodie);
-
- MPTHDID mpthd_me(void);
-
- MPTHDID mpthd_switch(MPTHDID mpthdtoswitchto);
-
- STRTYPE PTR mpthd_heap(MPTHDID mpthd, STRTYPE);
- STRTYPE PTR mpthd_myheap(STRTYPE);
-
-
- DESCRIPTION
-
- The mpthd cluster allows software switching of program control between
- multiple threads of execution. Threads are "light weight" processes. Unlike
- fork(2), they are quick to create, destroy, & switch between, and allow easy
- communication by sharing all global and dynamic variables.
-
- The mpthd_switch function suspends the current thread of execution, mpthd_me(),
- and switches into the new thread, mpthdtoswitchto. The first thread will
- resume when another thread switches back to it, and mpthd_switch will return
- this thread which restored control.
-
- Threads can be initialized using the mpthd_init(...) function. The initializer
- must specify the thdfn (thread function) which the thread is to execute when it
- is switched to, as well as the stack buff and its size in which the thread is
- to execute. When first switched to, the new thread will set up to
- allow/disallow interrupts (see mpsig(3C)) as its initializer had set up when
- the thread was initialized.
-
- The new thread will call mpthdfn with the previous thread as its argument; it
- will switch to the thread that mpthdfn returns, and repeat this process
- indefinitely. In other words,
-
- for (EVER) prevmpthd= mpthd_switch(mpthdfn(prevmpthd));
-
- mpthd_dinit is used to deinitialize threads. It will wait until mpthdtodie is
- not being executed, and will return the original buff pointer.
-
- mpthd_heap(MPTHDID mpthd, STRTYPE) returns a STRTYPE pointer to the thread's
- local heap, where STRTYPE is your own structure type. The local heap can be
- used by the thread for dynamically-allocated variables, but is most useful for
- passing arguments between threads without using global variables.
- mpthd_myheap(STRTYPE) is shorthand for mpthd_heap(mpthd_me(),STRTYPE).
-
- mpthd_setup(void PTR buf, UINT size) is used to setup the environment for
- mpthd's. It initializes a thread in buf as mpthd_init() does, and sets it
- up as the current (main()) thread (the main() function, however, uses
- a normal stack, not a thread stack). Internal _mpthd_setup does the
- setup, and is provided for higher-level multitaskers.
-
-
- RETURN VALUES & ERRORS
-
- Return values are as specified. Exceptions: mpthd_switch will return NIL iff
- it finds the mpthdtoswitchto structure/stack corrupted. Usually this is
- happens when a thread's stack is smaller than what its thdfn needs to execute.
- To help prevent this, mpthd_init and mpthd_setup will return NIL iff
- size < 3*sizeof(MPTHD). Also, mpthd_dinit(...) will return NIL iff a thread
- tries to deinitialize itself.
-
-
- NOTES
-
- The calls are robust. The interrupt enable/disable state is preserved between
- switches so switches can be made within signal handlers to implement
- pre-emptive multitasking.
-
- The implementation is highly portable; with little modification it will run on
- most machines/operating systems compiling "C" and supporting the standard
- setjmp and longjmp library functions. Using semaphores to control the
- execution of threads, the implementation can also be used on multiprocessor
- machines with a common address space.
-
- The multitasking interface is also designed to be portable. Using the same
- interface, similar functionality can be obtained with most stack-based
- languages.
-
-
- AVAILABILITY & BUGS
-
- The mpthd cluster has been tested under MS-DOS (Turbo C) and UNIX BSD 4.3
- (IBM RT, DEC VAX). It may be easily ported to other machines & op. systems.
-
-
- SEE ALSO
- mpsig(3C), mpsem(3C), and the mpthd.* files.
-
- "Building a Portable Multitasking Environment in "C":
- #1 - Switching Contexts" found in the file mpthd.tex
-
-
- AUTHOR
-
- Copyright (C) 1987 Michael Benjamin Parker (USA SS# 557-49-4130)
-
- This interface, code, and documentation may be freely distributed *at no cost*
- provided the copyright notices are preserved and modifications are forwarded
- to the author (for inclusion in future releases). All other rights reserved.
-
- mbparker@ATHENA.MIT.EDU / 721 E Walnut, Orange, CA 92667-6833 / 714-639-9497
-
-
- Created: 12/8/87 Release: 0.7 Version: 4/1/88
-
-