Cooperative Multithreading Library

This document describes th and how to use it in an assembly-language program.

Introduction

This is my latest attempt at cooperative multithreading for the Z80 processor. The goals of this version are:

Cooperative multithreading works best with I/O-bound threads. It can also work with compute-bound threads if they yield frequently enough to allow other threads to run.

Cooperative vs Preemptive Multithreading

With preemptive multithreading, a thread can run as long as it does not block and still has time left in its time-slice. With cooperative multithreading, a thread can run as long as it does not block. A cooperative thread, therefore, could run indefinitely.

Cooperative multithreading has both advantages and disadvantages over preemptive. Here are a few:

Advantages of cooperative

Disadvantages of cooperative

Using th

To use th, include the file "th.asm" at the entry point of your program—typically at address 0xD748 on the TI-86—and define a label named "main" where the main program starts (like in a C program). Also, define THREAD_MAX to the maximum number of threads that will run simultaneously in the program.

Example Programs

You can find two example programs that use multithreading in the files testth.asm and produce.asm.

Another application of th_sleep and th_wake is for implementing timers: a thread sleeps on a timer until it's expired, and a timer thread (or an interrupt handler) wakes threads when a timer expires.

Glossary

Block
See Sleep.
Cancel
To stop a thread from running.
Compute-bound
Primarily using the processor (CPU) for most of the time, such as long computations that do not input nor output anything.
CPU-bound
See Compute-bound.
Event
Something that can occur, such as a thread exiting, a data pipe being written to, etc. In this implementation, an event is simply a 16-bit number and can be the address of a data structure or any other arbitrary number, though typically it is the former.
Exit status
An integer indicating a thread's status when it exits. The value has meaning only to the application; for example, an exit status of 0 could indicate success, and other values could indicate different errors that might occur in the thread.
I/O-bound
Primarily waiting for input or output, such as reading from or writing to a data pipe.
Join
Wait for a thread to complete or be canceled. This is a synchronization technique. When a thread exits on its own (i.e., it is finished), a thread can obtains its exit status by "joining" the finished thread.
Sleep
To wait for an event.
Thread
Short for "thread of execution". One of two or more simultaneously-running tasks. Similar to a process in a multi-tasking operating system.
Wake
To notify a thread that an event occurred.
Yield
To allow other threads to execute. With cooperative multi-threading, processor-bound threads must yield so other threads can run.
Zombie
A thread that has exited but is floating around, waiting for another thread to join it. This is the same concept as a zombie process in Unix systems.

Bugs

While I have tested th for bugs and performance, there may be some bugs lurking in the code.

If you find a bug, try to make a small test case that triggers it (the smaller the test case, the better). Make sure it's actually a bug in the library and not in your own code—parallel computing can be tricky sometimes, after all. Then contact the author with the test case along with any details you know about the bug (such as symptoms). Better yet, fix the bug and send it back to the author.

Contacting the author

You can contact the author of this library at abbrev@gmail.com.

License

Copyright © 2007 Christopher Williams

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

$Id: th.html,v 1.5 2007/05/02 22:56:17 christop Exp $