Stackless `protothreads using computed gotos

Published on: 2006-1-18

Home | Archive

Stackless `protothreads' using computed goto's


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

    void *p;
    p = &&L1;
    goto *p;
    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.