Академический Документы
Профессиональный Документы
Культура Документы
Outline
Process
Events
Process Scheduler
Context Switching
Protothread
Process
Process structure
Process Thread
Preemptive
Cooperative
Process Structure
Process 1
Process 2
Process 3
next
next
next
thread
thread
thread
Function
pointer
Process
Thread
Function
pointer
Process
Thread
Function
pointer
Process
Thread
State of protothread
Process state
Poll is needed
(flag)
Process Thread
Example:
Process Programming
PROCESS macro
PROCESS_THREAD macro
PROCESS_BEGIN macro
PROCESS_END macro
PROCESS_THREAD
macro
PT_THREAD: char
PROCESS_BEGIN macro
PROCESS_END macro
core/sys/process.c
process_start
core/sys/process.c
exit_process
core/sys/process.c
Remove the
process from the
process list
call_process
core/sys/process.c
Cooperative
Run sequentially
Illustration [1]
Preemptive
[1] https://github.com/contiki-os/contiki/wiki/Processes
Event
(A)
b)
c)
https://github.com/contiki-os/contiki/wiki/Processes#The_Process_Scheduler
(B)
Type of event
(e.g., PROCESS_EVENT_INIT,
PROCESS_EVENT_MSG)
Receiving process
Event queue
process_post_synch
core/sys/process.c
The process is
called
process_post
core/sys/process.c
Process Scheduler
Purpose
How?
When?
Argument
Event identifier
https://github.com/contiki-os/contiki/wiki/Processes#The_Process_Scheduler
do_event (1)
core/sys/process.c
do_event (2)
core/sys/process.c
Polling
process_poll
core/sys/process.c
Set needspoll
do_poll
core/sys/process.c
process_run
core/sys/process.c
This function is executed
indefinitely
Call do_event
Context [1]:
the contents of a CPUs registers and program counter at any point in
time
[1] http://clinuxpro.com/context-switch
[2] http://en.wikipedia.org/wiki/Context_switch
[1] http://clinuxpro.com/context-switch
[1]
[2]
[3]
[4]
http://www.freertos.org/implementation/a00020.html
http://www.freertos.org/implementation/a00021.html
http://www.freertos.org/implementation/a00022.html
http://www.freertos.org/implementation/a00024.html
5
6
[5] http://www.freertos.org/implementation/a00025.html
[6] http://www.freertos.org/implementation/a00026.html
Problem of thread
Event-Driven Model
Event-driven model
Implemented in TinyOS
Shortcomings of event-driven
model
Hill, R. Szewczyk, A. Woo, S. Hollar, D. Culler, and K. Pister. System architecture directions for networked sensors. [ASPLOS 2000]
Levis, S. Madden, D. Gay, J. Polastre, R. Szewczyk, A. Woo, E. Brewer, and D. Culler. The Emergence of Networking Abstractions and Techniques in TinyOS. [NSDI 2004]
Protothread
Protothread
Implemented in ContikiOS
PT_WAIT_UNTIL(condition)
From thread: inherited the blocking wait semantics (Compare to traditional thread,
protothreads are very lightweight)
From events: inherited the stacklessness and the low memory overhead
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb
Ali
Protothread Example
Stack in Protothread
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Advantages of Protothreads
State Machine
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Event-Based Implementation of
RSC
Messy
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Protothread Implementation of
RSC
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Local Continuations
Low
When
Similar
stack
Only
Operations
SET
RESUME
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb
Ali
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
PT_WAIT_UNTIL(pt, condition1);
pt->lc = 5; case 5:
if(!condition1) return 0;
if(something) {
if(something) {
PT_WAIT_UNTIL(pt, condition2);
pt->lc = 10; case 10:
if(!condition2) return 0;
}
PT_END(pt);
}
Line numbers}
} return 1;
Process 1
Process 2
Process 3
next
next
next
pt
pt
pt
thread
thread
thread
Function
pointer
Process
Thread
Function
pointer
Process
Thread
Function
pointer
Process
Thread
PROCESS_BEGIN macro
PROCESS_END macro
core/sys/process.c
Yielding in Protothreads
core/sys/pt.h
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb
Ali
Pseudocode for
hierarchical Protothread
example
[1] Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems. Adam Dunkels , Oliver Schmidt, Thiemo Voigt , Muneeb Ali
Implementation of PT_SPAWN
pt struct of a
child protothread
processthread
(function) of a child
protothread
core/sys/pt.h