Document

Cooperative Task
Management without Manual
Stack management
Hanyun Tao
EECS 582 – W16
1
The War of Programming Model
Event Driven v.s Multithreaded
• Berkeley:
• Stanford:
EECS 582 – W16
2
Event Driven Programming Model
• Easy to use when reasoning about concurrency
• Code difficult to read
• Code difficult to maintain
• Good for single-processor (cooperative concurrency)
EECS 582 – W16
3
Multithreaded Programming Model
• Code is more readable
• Code is easier to maintain
• Difficult to reason about concurrency with threads
• Good for multi-processors (true concurrency)
EECS 582 – W16
4
Goal:Get the Best from both World!
EECS 582 – W16
5
Definition of Terms





Task Management
Stack Management
I/O Management
Conflict Management
Data Partitioning
EECS 582 – W16
6
Task Management
• Preemptive Task Management
• Tasks can interleave on uniprocessor or overlap on multiprocessor
• Serial Task Management
• One at a time, no conflict
• Cooperative Task Management
• Only yields control to other tasks at a well-defined point
• Such as wait for long-running I/O
EECS 582 – W16
7
Stack Management
• Manual Stack Management
• Inherent in Cooperative Task Management
• Finishing a “single” tasks may require to rip stack for multiple time
• Automatic Stack Management
• Inherent in pre-emptive Task Management
• Downside: Hidden concurrency assumptions
• Solution: Static & Dynamic Check
EECS 582 – W16
8
Other Management Models
• I/O Management
• Asynchronous versus Synchronous I/O Management
• Conflict Management
• Concerned with avoiding resource conflicts
• Serial & Cooperative (between yield point) – Atomic, no need to avoid
conflict
• Preemptive Task Management – need synchronization primitives
• Data Partitioning
• The more you break up your shared state, the fewer conflicts you
should have
EECS 582 – W16
9
The Sweet Spot
• Cooperative tasks are the most desirable when reasoning about
concurrency
• usually associated with event-driven programming
• Automatic stack management is the most desirable when
reading/maintaining code
• Usually associated with threaded or serial programming
EECS 582 – W16
10
The Sweet Spot
EECS 582 – W16
11
Solution
• Build a system that uses Cooperative Task
Management without Manual Stack Management
• Pulls together the best of both worlds
• Ease of reasoning about concurrency
• Readable/maintainable code
EECS 582 – W16
12
Implementation Detail
• Use fibers
• User level thread package
• Many-to-many threading solution for Windows Platform
• Non-preemptive
• Scheduled cooperatively – one fiber hands control off to another fiber
• Scheduling multiple fiber on a single thread
EECS 582 – W16
13
Main Fiber
Implementation Detail
Secondary Fiber
Scheduler
Return contro
Scheduler
• A Scheduler runs on a MainFiber
• Schedule both manual stack management
code and automatic stack management
code
• Code written with manual stack
management always runs on a fiber other
than MainFiber
• Compute-Only functions can run on any
fiber
Ta
State:
A=1. B=2, C=3
Call T
a
l to Main Fib
Ca
ll I/
Oa
)
er (Scheduler
Ty
Tz
I/O Returns
Resu
me T
a
Ta
State:
A=1, B=2, C=3
k
m
Co
pl
e
et
s
ta
an
d
r
n
ur
et
co
ro
nt
o
lt
m
ai
n
fib
er
Scheduler
EECS 582 – W16
14
nd
wa
it
Manual Calling Automatic
• Uses a Continuation to Fiber Adapter (CFA):
• Glues the notion of a continuation to the notion of a blocking call
• Handles the details of the continuation: rips itself into two, schedules
the second half within the first and calls the blocking function on the
second fiber
• Second half of CFA uses continuation to call the original task’s
continuation
EECS 582 – W16
15
Manual Calling Automatic
EECS 582 – W16
16
Automatic Calling Manual
• Uses a Fiber to Continuation Adapter (FCA)
•
•
•
•
FCA executes on secondary thread
May short-circuit if no blocking occurred
Glues the notion of a blocking call to one that uses a continuation
Handles the details of the transformation: creates a new
“fiberContinuation” which causes MainFiber to switch back to the
secondary fiber, executes the manual stack function and switches back
to the main thread
• FiberContinuation simply resumes the suspended fiber
EECS 582 – W16
17
Automatic Calling
Manual
EECS 582 – W16
18
Conclusion
• Clarify the Debate of Event Driven & Multithreaded Model by
Identifying two separate Concepts
• Task Management
• Stack Management
• Presents a hybrid model that adapts between code with
automatic and with manual stack management
• Enabling cooperation among disparate programmers & code bases
EECS 582 – W16
19