Longjmping back, and back again; Coroutines in C

Tim Olson tim at proton.amd.com
Wed Nov 22 02:01:52 AEST 1989


In article <457 at enea.se> jerker at helios.se (Jerker W}gberg) writes:
| 
| I am trying to implement coroutines using plain C. My
| application is not time critical, so there is no need for speed,
| being portable is far more important. It would be great if there
| was a way to switch stacks of the processes using just C.

Sorry, but there just isn't a way that is portable to all
architectures; there is almost always some assembly-language involved.

| 2. Are there more machines out there that probably will throw up
|    when executing this code, apart from CPU's where the stack is
|    growing upward instead of downward. This can be taken care of,
|    but I did not want to clutter the example below.

Here's an example of one:  The Am29000 (32-bit RISC processor) uses a
large number of registers as a "stack cache", and spills and fills
these to/from an area in memory when it overflows.  Another (separate)
memory stack holds non-scalar data, such as arrays and large
structures.  Your routine would only attempt to swap the memory stack,
leaving the register stack cache alone.

	-- Tim Olson
	Advanced Micro Devices
	(tim at amd.com)



More information about the Comp.lang.c mailing list