Enable build support by adding .onedev-buildspec.yml
await | Loading last commit info... | |
cmake | ||
demo | ||
dev | ||
examples | ||
tests | ||
third_party | ||
tutorial | ||
workloads | ||
.clang-format | ||
.clang-tidy | ||
.gitignore | ||
CMakeLists.txt | ||
README.md |
README.md
Await
Concurrency for C++ 🚀 with functional flavour λ
Prologue
You don't have to be an engineer to be a racing driver, but you do have to have Mechanical Sympathy – Jackie Stewart, 🏎️ racing driver
Features
- Orthogonal basis for concurrency: executors × fibers × futures × cancellation
- Scalable work-stealing task scheduler (executor) –
tasks::pools::fast::ThreadPool
- Transparent stackful fibers via
tasks::fibers::Pool
(backed bypools::fast::ThreadPool
) with automatic pooling - Functional futures with extensive combinator language and fluent API
- Futures are lazy ⟹ no dynamic memory allocations and synchronization required
- Transparent cooperative cancellation (interruption), wait-free for sequential composition / lock-free for parallel composition
- Structured concurrency (short-circuiting) via parallel futures combinators (
All
,FirstOf
, etc) andfibers::WaitGroup
(nursery) - Support for task contexts, transparent context propagation
- Deterministic systematic tests and stress-tests with fault-injection via 🧵
Twist
framework
Contents
- Executors
- Thread Pools (
pools
)pools::compute::ThreadPool
with shared blocking queue for independent CPU-bound tasks- Scalable work-stealing
pools::fast::ThreadPool
for fibers / stackless coroutines (IO-bound tasks)
Strand
(asynchronous mutex)ManualExecutor
for deterministic testing- Transparent fibers (
fibers
)fibers::Pool
fibers::ManualExecutor
- Thread Pools (
- Futures
- Types (
types
)- concepts
SomeFuture
,Future<T>
,UnitFuture
BoxedFuture<T>
EagerFuture<T>
- concepts
- Constructors (
make
)Submit
/Spawn
/Invoke
Contract
+Promise<T>
(eager)After
Value
Just
Never
- Combinators (
combine
)- Sequential composition (
seq
)Apply
– applies user λ to future valueMap
/AndThen
/OrElse
– monadic mappers for monadic value typeT
Flatten
– flattens nested futures (for asynchronous mappers)FlatMap
=Map
+Flatten
Via
– sets executor for following mappersSched
/Yield
– hints for schedulerWith
– attaches user-defined contextBox
– erases concreteFuture
typeStart
(orForce
) – converts toEagerFuture
, starts operationPrune
(orNoInline
) – prevents future inlining, releases memory / resources earlyClone
– splits future value into two copiesToUnit
– erases value type toUnit
OnComplete
/OnCancel
/Anyway
– asynchronous alternative todefer
/ RAIIWithTimeout
– attaches timeoutSequence
– runs two asynchronous operations in sequence
- Parallel composition (
par
)All
FirstOf
Quorum
Join
Interrupt
- Sequential composition (
- Terminators (
run
)Await
– synchronously unwrapsResult
from futureUnwrap
– optimistically unwraps readyResult
without waiting, ~operator *
forstd::optional
Go
– starts and detaches future
- Operators (
syntax
)- pipe:
Just() | Via(pool) | Apply([]{})
- sequence:
f >> g
~Sequence(f, g)
~Await(f) ; g
- bang:
!f
~f | Start()
- unwrap:
*f
~f | Unwrap()
- go:
~f
~f | Go()
- or:
f or g
~FirstOf(f, g)
- join:
f and g
~Join(f, g)
- sum:
f + g
~All(f, g)
- pipe:
- Types (
- Stackful Fibers
- Scheduling (
sched
)Yield
TeleportTo
+TeleportGuard
SleepFor
- Synchronization (
sync
)Mutex
(lock-free)OneShotEvent
(lock-free)WaitGroup
(lock-free)Semaphore
CondVar
Channel<T>
+Select
- Scheduling (
- Timers
- Generic
Await
algorithm (~co_await
)Await(Future)
Examples
Tutorial
- Implementing your own Future for
AsyncEvent
- Cancellation for
AsyncEvent
Inspiration
- Your Server as a Function, Futures: Twitter vs Scala, Twitter Futures
- The Incomplete Guide to Lazy Evaluation (in Haskell)
- Project Loom Proposal, State of Loom: Part 1, Part 2
- Notes on structured concurrency, or: Go statement considered harmful
- Timeouts and cancellation for humans
- Unified Executors
- Asymmetric Transfer
- Go scheduler: Implementing language with lightweight concurrency
- Асинхронность в программировании
Requirements
Supported | |
---|---|
Architecture | x86-64, ARMv8-A-64 |
Operating System | Linux, MacOS |
Compiler | Clang++ (≥ 13) |
Dependencies
- Wheels – core components, test framework
- Sure – context switching for stackful fibers / coroutines
- Twist – fault injection framework for concurrency
- Carry – user context
Third-party
Build
# Clone repo
git clone https://gitlab.com/Lipovsky/await.git
cd await
mkdir build && cd build
# Generate build files
cmake -DAWAIT_TESTS=ON -DAWAIT_EXAMPLES=ON -DCMAKE_CXX_COMPILER=/usr/bin/clang++-13 -DCMAKE_C_COMPILER=/usr/bin/clang-13 ..
# Build example
make await_example_futures
# Run example
./examples/futures/await_example_futures
CMake options
Flags
AWAIT_TESTS
– Enable testsAWAIT_EXAMPLES
– Enable examplesAWAIT_DEVELOPER
– Developer mode: tests + examples +-Werror
AWAIT_MIMALLOC
– Usemimalloc
memory allocator