SlideShare une entreprise Scribd logo
1  sur  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
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

Contenu connexe

Tendances

Hardware multithreading
Hardware multithreadingHardware multithreading
Hardware multithreading
Fraboni Ec
 
Multivector and multiprocessor
Multivector and multiprocessorMultivector and multiprocessor
Multivector and multiprocessor
Kishan Panara
 
CPU vs. GPU presentation
CPU vs. GPU presentationCPU vs. GPU presentation
CPU vs. GPU presentation
Vishal Singh
 

Tendances (20)

Real time Scheduling in Operating System for Msc CS
Real time Scheduling in Operating System for Msc CSReal time Scheduling in Operating System for Msc CS
Real time Scheduling in Operating System for Msc CS
 
Computer architecture multi core processor
Computer architecture multi core processorComputer architecture multi core processor
Computer architecture multi core processor
 
Hardware multithreading
Hardware multithreadingHardware multithreading
Hardware multithreading
 
Chapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatzChapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatz
 
Open MPI
Open MPIOpen MPI
Open MPI
 
Multi core processors
Multi core processorsMulti core processors
Multi core processors
 
Unit 5 Advanced Computer Architecture
Unit 5 Advanced Computer ArchitectureUnit 5 Advanced Computer Architecture
Unit 5 Advanced Computer Architecture
 
Pipeline hazards in computer Architecture ppt
Pipeline hazards in computer Architecture pptPipeline hazards in computer Architecture ppt
Pipeline hazards in computer Architecture ppt
 
File systems for Embedded Linux
File systems for Embedded LinuxFile systems for Embedded Linux
File systems for Embedded Linux
 
Pipeline hazard
Pipeline hazardPipeline hazard
Pipeline hazard
 
Multivector and multiprocessor
Multivector and multiprocessorMultivector and multiprocessor
Multivector and multiprocessor
 
CPU vs. GPU presentation
CPU vs. GPU presentationCPU vs. GPU presentation
CPU vs. GPU presentation
 
Cluster computing
Cluster computingCluster computing
Cluster computing
 
Presentation on graphics processing unit (GPU)
Presentation on graphics processing unit (GPU)Presentation on graphics processing unit (GPU)
Presentation on graphics processing unit (GPU)
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Network security Lab manual
Network security Lab manual Network security Lab manual
Network security Lab manual
 
Chapter 13 - I/O Systems
Chapter 13 - I/O SystemsChapter 13 - I/O Systems
Chapter 13 - I/O Systems
 
Operating System-Ch8 memory management
Operating System-Ch8 memory managementOperating System-Ch8 memory management
Operating System-Ch8 memory management
 
Layers and types of cloud
Layers and types of cloudLayers and types of cloud
Layers and types of cloud
 
Parallel computing
Parallel computingParallel computing
Parallel computing
 

Similaire à protothread and its usage in contiki OS

Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
Akshay Nagpurkar
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
Shuo Chen
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
Analysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) DatagramsAnalysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) Datagrams
Emily Jones
 

Similaire à protothread and its usage in contiki OS (20)

Pthread
PthreadPthread
Pthread
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementation
 
P-Threads
P-ThreadsP-Threads
P-Threads
 
Lec13 multidevice
Lec13 multideviceLec13 multidevice
Lec13 multidevice
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
 
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki LinnakangasPG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Cassandra in Operation
Cassandra in OperationCassandra in Operation
Cassandra in Operation
 
Securing Applications and Pipelines on a Container Platform
Securing Applications and Pipelines on a Container PlatformSecuring Applications and Pipelines on a Container Platform
Securing Applications and Pipelines on a Container Platform
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
Linux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, futureLinux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, future
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Study notes for CompTIA Certified Advanced Security Practitioner
Study notes for CompTIA Certified Advanced Security PractitionerStudy notes for CompTIA Certified Advanced Security Practitioner
Study notes for CompTIA Certified Advanced Security Practitioner
 
Analysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) DatagramsAnalysis Of Internet Protocol ( IP ) Datagrams
Analysis Of Internet Protocol ( IP ) Datagrams
 
Pthreads linux
Pthreads linuxPthreads linux
Pthreads linux
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
P threads
P threadsP threads
P threads
 

Plus de Salah Amean

Plus de Salah Amean (20)

ICT role in Yemen
ICT role in Yemen ICT role in Yemen
ICT role in Yemen
 
Contiki os timer tutorial
Contiki os timer tutorialContiki os timer tutorial
Contiki os timer tutorial
 
WSN protocol 802.15.4 together with cc2420 seminars
WSN protocol 802.15.4 together with cc2420 seminars WSN protocol 802.15.4 together with cc2420 seminars
WSN protocol 802.15.4 together with cc2420 seminars
 
ContikiMAC : Radio Duty Cycling Protocol
ContikiMAC : Radio Duty Cycling ProtocolContikiMAC : Radio Duty Cycling Protocol
ContikiMAC : Radio Duty Cycling Protocol
 
Location in ubiquitous computing, LOCATION SYSTEMS
Location in ubiquitous computing, LOCATION SYSTEMSLocation in ubiquitous computing, LOCATION SYSTEMS
Location in ubiquitous computing, LOCATION SYSTEMS
 
Bonjour protocol
Bonjour protocolBonjour protocol
Bonjour protocol
 
Optimization Analysis
Optimization AnalysisOptimization Analysis
Optimization Analysis
 
Mobile apps-user interaction measurement & Apps ecosystem
Mobile apps-user interaction measurement & Apps ecosystemMobile apps-user interaction measurement & Apps ecosystem
Mobile apps-user interaction measurement & Apps ecosystem
 
ict culturing conference presentation _presented 2013_12_07
 ict culturing conference presentation _presented 2013_12_07 ict culturing conference presentation _presented 2013_12_07
ict culturing conference presentation _presented 2013_12_07
 
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...Data Mining:  Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
Data Mining: Concepts and Techniques_ Chapter 6: Mining Frequent Patterns, ...
 
Data mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, dataData mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, data
 
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic ConceptsData Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
 
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
 
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methodsData Mining: Concepts and techniques classification _chapter 9 :advanced methods
Data Mining: Concepts and techniques classification _chapter 9 :advanced methods
 
Data Mining: Concepts and techniques: Chapter 13 trend
Data Mining: Concepts and techniques: Chapter 13 trendData Mining: Concepts and techniques: Chapter 13 trend
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 11,Review: Basic Cluster Analys...Data Mining: Concepts and techniques: Chapter 11,Review: Basic Cluster Analys...
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 10. Cluster Analysis: Basic Conc...Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
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 chapter 07 : Advanced Frequent Pattern M...Data Mining: Concepts and Techniques chapter 07 : Advanced Frequent Pattern M...
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 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
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 _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
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
  • 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