Driving Behavioral Change for Information Management through Data-Driven Gree...
protothread and its usage in contiki OS
1. 1
Protothreads –
Simplifying Programming of Memory-Constrain
ed Embedded Systems
By
Salah
Adopted from
Adam Dunkels et. al.,
http://dunkels.com/adam/pt/expansion.html
2. 2
Protothreads
uProtothreads are a extremely lightweight, stackless type of threads that
provides a blocking context on top of an event-driven system, without the
overhead of per-thread stacks.
uThe purpose of Protothreads is to implement sequential flow of control
without complex state machines or full multi-threading.
u Protothreads provides conditional blocking inside C functions..
3. 3
Why not just use multithreading?
Multithreading the basis of (almost) all embedded OS/RTOSes!
◦ WSN community: Mantis, BTNut (based on multithreading); Contiki (multithr
eading on a per-application basis)
Nothing wrong with multithreading
◦ Multiple stacks require more memory
◦ Networked = more concurrency than traditional embedded
◦ Can lead to more expensive hardware
◦ Preemption
◦ Threads: explicit locking; Protothreads: implicit locking
Protothreads are a new point in the design space
◦ Between event-driven and multithreaded
4. 4
Threads require per-thread stack memory
Four threads, each with its own stack
Thread 1 Thread 2 Thread 3 Thread 4
5. 5
Events require one stack
Four threads, each with its own stack
Thread 1 Thread 2 Thread 3 Thread 4
Eventhandler 1Eventhandler 2Eventhandler 3
Stack is reused for
every event handler
Threads require per-thread stack memory
● Four event handlers, one
stack
Eventhandler 4
6. 6
Protothreads require one stack
Four threads, each with its own stack
Thread 1 Thread 2 Thread 3 Thread 4
Threads require per-thread stack memory
● Four protothreads, one
stack
Events require one stack
● Four event handlers, one
stack
Protothread 1Protothread 2Protothread 3Protothread 4
Just like events
7. 7
Main features
u No machine specific code since the protothreads library is pure C
u Does not use error-prone functions such as longjmp()
u Very small RAM overhead - only two bytes per protothread
u Can be used with or without an OS
u Provides blocking wait without full multi-threading or stack-
switching
8. 8
Examples applications:
- Memory constrained systems
- Event-driven protocol stacks
- Deeply embedded systems
- Sensor network nodes
10. 10
Pt file structure
Data Structures
struct Pt
Initialization
#define PT_INIT(pt)
Initialize a protothread.
Declaration and definition
#define PT_THREAD(name_args)
Declaration of a protothread.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C functi
on implementing the protothread.
#define PT_END(pt)
Declare the end of a protothread.
11. 11
Pt file structure
Blocked wait
#define PT_WAIT_UNTIL(pt, condition)
Block and wait until condition is true.
#define PT_WAIT_WHILE(pt, cond)
Block and wait while condition is true.
Hierarchical protothreads
#define PT_WAIT_THREAD(pt, thread)
Block and wait until a child protothread completes.
#define PT_SPAWN(pt, child, thread)
Spawn a child protothread and wait until it exits.
12. 12
Pt file structure
Exiting and restarting
#define PT_RESTART(pt)
Restart the protothread.
#define PT_EXIT(pt)
Exit the protothread.
Calling a protothread
#define PT_SCHEDULE(f)
Schedule a protothread.
13. 13
Pt file structure
Yielding from a protothread
#define PT_YIELD(pt)
Yield from the current protothread.
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
Defines
#define PT_WAITING 0
#define PT_YIELDED 1
#define PT_EXITED 2
#define PT_ENDED 3
14. 14
Protothread scheduling
A protothread runs in a C function
We schedule a protothread by invoking its function
We can invoke the protothread from an event handler
◦ Protothreads as blocking event handlers
We can let the operating system invoke our protothreads
◦ Contiki
Protothreads can invoke other protothreads
◦ Can wait until a child protothread completes
◦ Hierarchical protothreads
16. 16
Simple example program using
protothread
--waits for a counter to reach a certain threshold,
-prints out a message, and resets the counter.
- This is done in a while() loop that runs forever.
- The counter is increased in the main() function.