SlideShare a Scribd company logo
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
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
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 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
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
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
Examples applications:
- Memory constrained systems
- Event-driven protocol stacks
- Deeply embedded systems
- Sensor network nodes
9
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
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
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
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
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
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 example
int 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

More Related Content

PDF
The Linux Kernel Scheduler (For Beginners) - SFO17-421
PDF
The ideal and reality of NVDIMM RAS
PDF
Linux scheduler
PPTX
Staring into the eBPF Abyss
PDF
プログラマ目線から見たRDMAのメリットと その応用例について
PDF
Linux kernel tracing
PDF
Nwdafまとめ
PPTX
3GPP F1インターフェース(TS38.470-f50)の概要
The Linux Kernel Scheduler (For Beginners) - SFO17-421
The ideal and reality of NVDIMM RAS
Linux scheduler
Staring into the eBPF Abyss
プログラマ目線から見たRDMAのメリットと その応用例について
Linux kernel tracing
Nwdafまとめ
3GPP F1インターフェース(TS38.470-f50)の概要

What's hot (20)

PDF
HKG18-318 - OpenAMP Workshop
PDF
"SRv6の現状と展望" ENOG53@上越
PDF
Making Linux do Hard Real-time
PPTX
Contiki os timer tutorial
DOCX
Mikrotik basic configuration
PDF
Uboot startup sequence
PDF
IIJmio meeting 19 IIJ フルMVNO徹底解説
PDF
Interrupts
PPTX
3GPP TS 38.300-100まとめ
PDF
Mastering Real-time Linux
PPT
作業系統概論
PDF
Interview preparation workshop
PPTX
The TCP/IP Stack in the Linux Kernel
PDF
Introduction to Linux Drivers
PPTX
フロー技術によるネットワーク管理
PDF
Bilgisayar Mimarisi 02, Feza BUZLUCA
PPTX
FD.io VPP事始め
PDF
30分でわかる! コンピュータネットワーク
PDF
What multimodal foundation models cannot perceive
PPTX
Understanding DPDK
HKG18-318 - OpenAMP Workshop
"SRv6の現状と展望" ENOG53@上越
Making Linux do Hard Real-time
Contiki os timer tutorial
Mikrotik basic configuration
Uboot startup sequence
IIJmio meeting 19 IIJ フルMVNO徹底解説
Interrupts
3GPP TS 38.300-100まとめ
Mastering Real-time Linux
作業系統概論
Interview preparation workshop
The TCP/IP Stack in the Linux Kernel
Introduction to Linux Drivers
フロー技術によるネットワーク管理
Bilgisayar Mimarisi 02, Feza BUZLUCA
FD.io VPP事始め
30分でわかる! コンピュータネットワーク
What multimodal foundation models cannot perceive
Understanding DPDK
Ad

Similar to protothread and its usage in contiki OS (20)

PPT
Contiki introduction I.
PPT
Lecture 5: Software platforms and services
PDF
EEEM048_Lecture5_Software platforms and services .pdf
PPTX
MODULE IV embedded (1).pptx
PDF
Concurrent programming with RTOS
PPTX
Lec 9-os-review
PPTX
Real Time Operating Systems
PPTX
Wait-free data structures on embedded multi-core systems
PDF
seminarembedded-150504150805-conversion-gate02.pdf
PPTX
Embedded os
PDF
HIS 2017 Mark Batty-Industrial concurrency specification for C/C++
PPT
Threaded Programming
PDF
Brief Introduction to Parallella
PPT
Embedded systems
PPTX
CS345 09 - Ch04 Threads operating system1.pptx
PPTX
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
PDF
Rtos princples adn case study
PDF
Pthread
PDF
Esctp snir
Contiki introduction I.
Lecture 5: Software platforms and services
EEEM048_Lecture5_Software platforms and services .pdf
MODULE IV embedded (1).pptx
Concurrent programming with RTOS
Lec 9-os-review
Real Time Operating Systems
Wait-free data structures on embedded multi-core systems
seminarembedded-150504150805-conversion-gate02.pdf
Embedded os
HIS 2017 Mark Batty-Industrial concurrency specification for C/C++
Threaded Programming
Brief Introduction to Parallella
Embedded systems
CS345 09 - Ch04 Threads operating system1.pptx
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Rtos princples adn case study
Pthread
Esctp snir
Ad

More from Salah Amean (20)

PDF
ICT role in Yemen
PPT
WSN protocol 802.15.4 together with cc2420 seminars
PPTX
ContikiMAC : Radio Duty Cycling Protocol
PPTX
Location in ubiquitous computing, LOCATION SYSTEMS
PPT
Bonjour protocol
PPTX
Optimization Analysis
PPTX
Mobile apps-user interaction measurement & Apps ecosystem
PPTX
ict culturing conference presentation _presented 2013_12_07
PPT
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
PPT
Data mining :Concepts and Techniques Chapter 2, data
PPT
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
PPT
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
PPT
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
PPT
Data Mining: Concepts and techniques: Chapter 13 trend
PPT
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
PPT
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
PPT
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
PPT
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
PPT
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
PPT
Data Mining: Concepts and Techniques (3rd ed.) - Chapter 3 preprocessing
ICT role in Yemen
WSN protocol 802.15.4 together with cc2420 seminars
ContikiMAC : Radio Duty Cycling Protocol
Location in ubiquitous computing, LOCATION SYSTEMS
Bonjour protocol
Optimization Analysis
Mobile apps-user interaction measurement & Apps ecosystem
ict culturing conference presentation _presented 2013_12_07
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data mining :Concepts and Techniques Chapter 2, data
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques: Chapter 13 trend
Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) - Chapter 3 preprocessing

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
KodekX | Application Modernization Development
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Electronic commerce courselecture one. Pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Monthly Chronicles - July 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KodekX | Application Modernization Development
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Building Integrated photovoltaic BIPV_UPV.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Electronic commerce courselecture one. Pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto

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
  • 9. 9
  • 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
  • 15. 15 How do we implement 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.
  • 18. 18 C preprocessor expand the above code,
  • 21. 21 C-switch expansion-another example int 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