Cooperative Task Management without Manual Stack Management
Cooperative Task Management without Manual Stack Management or, Even-driven Programming is Not the Opposite of Threaded Programming A. Adya, J. Howell...
Cooperative Task Management without Manual Stack Management or, Even-driven Programming is Not the Opposite of Threaded Programming A. Adya, J. Howell, M. Theimer, W. Bolosky and J. Douceur
Presented by Will Landecker
1
Outline ●
Introduction: threads vs events?
●
Task management
●
Stack management
●
Hybrid stack management
●
Conclusions
2
Threads vs. events? Events
Threads
●
Explicit yielding
●
Preemptive scheduling
●
Asynchronous I/O
●
Synchronous I/O
–
●
No blocking
–
Continuations
●
3
Blocking
Save state, resume automatically
Threads vs. events? Events
Threads
●
Explicit yielding
●
Preemptive scheduling
●
Asynchronous I/O
●
Synchronous I/O
–
●
No blocking
–
Continuations
●
4
Blocking
Save state, resume automatically
Threads vs. events? Events
Threads
Task management ●
Explicit yielding
●
Preemptive scheduling
●
Asynchronous I/O
●
Synchronous I/O
–
●
No blocking
–
Continuations
●
Blocking
Save state, resume automatically
Stack management 5
Outline ●
Introduction: threads vs events?
●
Task management
●
Stack management
●
Hybrid stack management
●
Conclusions
6
Task management ●
A task encapsulates a control flow
●
Preemptive A
●
time B
A
C
Serial
time A
●
...
B
B
C
Cooperative
...
time B 7
YIELD!
A
C
B
...
Task management ●
A task encapsulates a control flow
●
Preemptive – – –
●
Processes interleave / overlap on uni/multiprocessor Shared state, need synchronization / protection Synchronous I/O
Serial
time A
●
B
C
Cooperative
...
time B 8
YIELD!
A
C
B
...
Task management ●
A task encapsulates a control flow
●
Preemptive – – –
●
Serial – – –
●
Processes interleave / overlap on uni/multiprocessor Shared state, need synchronization / protection Synchronous I/O Task runs to completion Easier to guarantee integrity of data I/O must be asynchronous
Cooperative
time B 9
YIELD!
A
C
B
...
Task management ●
A task encapsulates a control flow
●
Preemptive – – –
●
Serial – – –
●
Processes interleave / overlap on uni/multiprocessor Shared state, need synchronization / protection Synchronous I/O Task runs to completion Easier to guarantee integrity of data I/O must be asynchronous
Cooperative – –
Explicit yield: similar guarantees about data integrity I/O must still be asynchronous 10
Outline ●
Introduction: threads vs events?
●
Task management
●
Stack management
●
Hybrid stack management
●
Conclusions
11
Stack management ●
Manual stack management – – – –
●
Rip tasks into pseudo-atomic event handlers Explicitly save task state to heap Messages, continuations Code becomes difficult to maintain and read as software evolves
Automatic stack management – – – –
1 task 1 procedure Can ignore calls to I/O Control returned automatically Maintainable, readable code 12
Outline ●
Introduction: threads vs events?
●
Task management
●
Stack management
●
Hybrid stack management
●
Conclusions
13
automatic
“sweet spot”
manual
Stack management
Hybrid stack management
event-driven
multithreaded
cooperative preemptive Task management
Claim: combine maintainability and readability of automatic stack management with reasoning benefits of cooperative task management. In reality: New system allows both manual and automatic stack management. 14
Hybrid stack management New system allows both manual and automatic stack management. What could go wrong?
15
Hybrid stack management New system allows both manual and automatic stack management. What could go wrong?
A
B
Manual calling automatic … I/O ...
Given a manual stack management procedure A, and an automatic stack management procedure B.
time
A calls B, B blocks on I/O.
B
16
A
Hybrid stack management Main fiber
Fibers (Windows) are scheduled cooperatively.
otherFiber
A
Adaptors switch between fibers. B
Main fiber runs a scheduler. Procedures expecting automatic stack management must run on a different fiber.
C
B
Each code style (manual vs. automatic stack management) is unaware of the other. time
17
… I/O ...
A
Hybrid stack management Automatic calling manual
B
Given a manual stack management procedure A, and an automatic stack management procedure B.
A
… I/O ...
B
B calls A, A requests I/O. A
time
18
B
Hybrid stack management Main fiber
Solution:
Sched.
otherFiber
Execute B and A on a new fiber.
B
A sees asynchronous I/O. Scheduler ensures B only gets one return.
A I/O request
Sched. I/O complete
A B Sched. 19
Outline ●
Introduction: threads vs events?
●
Task management
●
Stack management
●
Hybrid stack management
●
Conclusions
20
●
●
Cooperative task management is good for reasoning about concurrency. Manual stack management can be cumbersome.
automatic
●
Threads vs events? Or stack management vs task management? Stack management
●
“sweet spot”
manual
Conclusions
event-driven
multithreaded
cooperative preemptive Task management
A “hybrid” stack management system uses fibers to allow both manual and automatic stack management. 21