protothread and its usage in contiki OS

21
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

Transcript of protothread and its usage in contiki OS

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

ProtothreadsuProtothreads 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

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

Threads require per-thread stack memory

Four threads, each with its own stack

Thread 1 Thread 2 Thread 3 Thread 4

5

Events require one stackFour 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

Protothreads require one stackFour 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

Main featuresu 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

Examples applications:

- Memory constrained systems

- Event-driven protocol stacks

- Deeply embedded systems

- Sensor network nodes

9

10

Pt file structure

Data Structuresstruct 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 function implementing the protothread.

#define PT_END(pt)Declare the end of a protothread.

11

Pt file structureBlocked 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

Pt file structureExiting 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

Pt file structureYielding 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

Protothread schedulingA 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

15

How do we implement protothreads?

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.

17

Example’s coding

18

C preprocessor expand the above code,

19

C-switch expansion

20

Blocked wait macro

21

C-switch expansion-another exampleint a_protothread(struct pt *pt) {

PT_BEGIN(pt);

PT_WAIT_UNTIL(pt, condition1);

if(something) {

PT_WAIT_UNTIL(pt, condition2);

}

PT_END(pt);

}

int a_protothread(struct pt *pt) {

switch(pt->lc) { case 0:

pt->lc = 5; case 5:

if(!condition1) return 0;

if(something) {

pt->lc = 10; case 10:

if(!condition2) return 0;

}

} return 1;

}Line numbers