Cooperative Task Management without Manual Stack management Hanyun Tao EECS 582 – W161
The War of Programming Model Event Driven v.s Multithreaded Berkeley: Stanford: EECS 582 – W162
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 – W163
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 – W164
Goal : Get the Best from both World! EECS 582 – W165
Definition of Terms Task Management Stack Management I/O Management Conflict Management Data Partitioning EECS 582 – W166
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 – W167
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 – W168
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 – W169
The Sweet Spot EECS 582 – W1610 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
The Sweet Spot EECS 582 – W1611
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 – W1612
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 – W1613
Implementation Detail 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 EECS 582 – W1614
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 – W1615
Manual Calling Automatic EECS 582 – W1616
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 – W1617
Automatic Calling Manual EECS 582 – W1618
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 – W1619