[comp.lang.c] threads in user mode

mouse@mcgill-vision.UUCP (der Mouse) (04/12/88)

In article <269@cantuar.UUCP>, james@cantuar.UUCP (J. Collier) writes:
> Peter da Silva (peter@sugar.UUCP) writes:
>> I'd like to see the following functions become standard in 'C':
>> ....
>> COROUTINE -- Build a jmp_buf for a coroutine.
> Correct me if I'm wrong, but I find that on some machines (well, on
> BSD Vaxen anyway) setjmp()/longjmp() can't be used to implement
> coroutines because longjmp() unwinds the stack destructively while
> checking for botches.  A small amount of in-line assembly language is
> therefore necessary for transferring control.

It doesn't *have* to be inline.  Certainly not on a VAX, and probably
not on other machines.

> Pre-empting threads which share a common data space are probably not
> a good idea for most purposes - the synchronisation problems usually
> outweigh any advantages.  [...]  Threads with explicit sleep() calls
> and round-robin transfer [...] are one way of organising things.

I once wrote a hack which used SIGALRM to switch among several threads
running in the same data space (with different stacks, of course).
This was under 4.3BSD on a VAX.  It required a little assembly assist
(about forty lines) and depended on knowing the details of the signal
delivery mechanism.

But it allows any of the above.  Use setitimer() to request periodic
SIGALRMs and your scheduler code can implement pre-emption, or you can
use kill() to send SIGALRM to yourself and use this to build an
explicit sleep routine.

I never developed it; it's a solution in search of a problem at the
moment (it was written more as proof-of-concept than anything else).
If anyone wants it, to use or read, feel free to send me mail.

					der Mouse

			uucp: mouse@mcgill-vision.uucp
			arpa: mouse@larry.mcrcim.mcgill.edu