xpcc
Protothreads

Lightweight, stackless threads. More...

Classes

class  xpcc::pt::Protothread
 A very lightweight, stackless thread. More...
 
class  xpcc::pt::Semaphore
 Counting semaphore. More...
 

Macros

#define PT_BEGIN()
 Declare start of protothread. More...
 
#define PT_END()
 Stop protothread and end it. More...
 
#define PT_YIELD()
 Yield protothread till next call to its run().
 
#define PT_WAIT_WHILE(condition)
 Cause protothread to wait while given condition is true.
 
#define PT_WAIT_UNTIL(condition)
 Cause protothread to wait until given condition is true.
 
#define PT_WAIT_THREAD(child)
 Cause protothread to wait until given child protothread completes.
 
#define PT_SPAWN(child)
 Restart and spawn given child protothread and wait until it completes.
 
#define PT_CALL(resumable)
 Calls a given resumable function and returns whether it completed successfully or not.
 
#define PT_RESTART()
 Reset protothread to start from the beginning. More...
 
#define PT_EXIT()
 Stop and exit from protothread.
 

Detailed Description

Protothreads are extremely lightweight stackless threads designed for severely memory constrained systems, such as small embedded systems or wireless sensor network nodes. Protothreads provide linear code execution for event-driven systems implemented in C. Protothreads can be used with or without an underlying operating system to provide blocking event-handlers.

Protothreads provide sequential flow of control without complex state machines or full multi-threading.

As they implement some kind of cooperative multithreading Protothreads are non-preemptable. Therefore, a context switch can only take place on blocking operations. Therefore you don't need complex synchronisation. Protothreads are also stackless, which means member variables of the xpcc::Protothread subclasses are needed for preserving variables across context switches.

A protothread runs within a single function (xpcc::Protothread::run()) and cannot span over other functions. A protothread may call normal functions, but cannot block inside a called function. Blocking inside nested function calls is instead made by spawning a separate protothread for each potentially blocking function.

The protothread concept was developed by Adam Dunkels and Oliver Schmidt.

Originally ported to C++ for use by Hamilton Jet (www.hamiltonjet.co.nz) by Ben Hoyt, but stripped down for public release. See his blog entry about it for more information: http://blog.micropledge.com/2008/07/protothreads/

See also
http://www.sics.se/~adam/pt/

Macro Definition Documentation

#define PT_BEGIN ( )
Warning
Use at start of the run() implementation!
#define PT_END ( )
Warning
Use at end of the run() implementation!
#define PT_RESTART ( )

In the next executing cycle the protothread will restart its execution at its PT_BEGIN.