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
● Explicit yielding
● Asynchronous I/O
– No blocking
● Continuations
Threads
● Preemptive scheduling
● Synchronous I/O
– Blocking
● Save state, resume automatically
3
Threads vs. events?
Events
● Explicit yielding
● Asynchronous I/O
– No blocking
● Continuations
Threads
● Preemptive scheduling
● Synchronous I/O
– Blocking
● Save state, resume automatically
4
Threads vs. events?
Events
● Explicit yielding
● Asynchronous I/O
– No blocking
● Continuations
Threads
● Preemptive scheduling
● Synchronous I/O
– Blocking
● Save state, resume automatically
Task management
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
● Serial
● Cooperative
time
A B A C B ...
time
A CB ...
time
A CB ...B
YIE
LD!
7
Task management● A task encapsulates a control flow● Preemptive
– Processes interleave / overlap on uni/multiprocessor– Shared state, need synchronization / protection– Synchronous I/O
● Serial
● Cooperative
time
A CB ...
time
A CB ...B
YIE
LD!
8
Task management● A task encapsulates a control flow● Preemptive
– Processes interleave / overlap on uni/multiprocessor– Shared state, need synchronization / protection– Synchronous I/O
● Serial– Task runs to completion– Easier to guarantee integrity of data– I/O must be asynchronous
● Cooperative time
A CB ...B
YIE
LD!
9
Task management● A task encapsulates a control flow● Preemptive
– Processes interleave / overlap on uni/multiprocessor– Shared state, need synchronization / protection– Synchronous I/O
● Serial– 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
Hybrid stack 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.
Task management
Sta
ck m
anag
emen
t
cooperative preemptive
man
ual
auto
mat
ic
event-driven
multithreaded“sweet spot”
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?
Manual calling automatic
Given a manual stack management procedure A, and an automatic stack management procedure B.
A calls B, B blocks on I/O.
A
B
… I/O ...
B
A
time
16
Hybrid stack management
Fibers (Windows) are scheduled cooperatively.
Adaptors switch between fibers.
Main fiber runs a scheduler. Procedures expecting automatic stack management must run on a different fiber.
Each code style (manual vs. automatic stack management) is unaware of the other.
B
… I/O ...
B
time
A
A
Main fiber otherFiber
C
17
Hybrid stack management
Automatic calling manual
Given a manual stack management procedure A, and an automatic stack management procedure B.
B calls A, A requests I/O.
A
B
B
time
A
… I/O ...
B
18
Hybrid stack management
Solution:
Execute B and A on a new fiber.
A sees asynchronous I/O. Scheduler ensures B only gets one return.
A
B
B
Main fiber
otherFiberSched.
Sched.
Sched.
A
I/Orequest
I/Ocomplete
19
Outline
● Introduction: threads vs events?● Task management● Stack management● Hybrid stack management● Conclusions
20
Conclusions
● Threads vs events? Or stack management vs task management?
● Cooperative task management is good for reasoning about concurrency.
● Manual stack management can be cumbersome.
● A “hybrid” stack management system uses fibers to allow both manual and automatic stack management.
Task management
Sta
ck m
anag
emen
tcooperative preemptive
man
ual
auto
mat
ic
event-driven
multithreaded“sweet spot”
21