Stackless `protothreads using computed gotos

Published on: 2006-1-18

Home | Archive

Stackless `protothreads' using computed goto's

2006-01-18T22:51:00

The GNU Compiler offers an interesting extension by which you can store the address of a label in a pointer variable.

main()
{
    void *p;
    p = &&L1;
    goto *p;
    printf("hello\n");
    L1: printf("world\n");
}
The `protothread library', available from this site, uses this GCC extension and some simple preprocessor magic to develop a very low overhead non-preemptive multithreading system suitable for use in severly memory constrained systems. There is also an alternate implementation based on a clever use of the switch-case statement. The source code is recommended reading for people interested in creative C coding! I was able to use the code unchanged for writing a toy non-preemptive tasking system for my ARM7 CPU.