This directory contains the implementation of gRPC's promise library, a framework for asynchronous programming.
The promise library provides a set of tools for writing asynchronous code in a composable and easy-to-understand way. It is based on the concept of a "promise," which is an object that represents the eventual result of an asynchronous operation. Promises are polled to advance their state, and can be chained together to form complex asynchronous workflows. They are designed to be lightweight and efficient, with a focus on minimizing memory allocations and avoiding virtual function calls.
promise.h: Defines the corePromiseclass and related helpers. APromiseis a functor that returns aPoll<T>. Most code will not use Promise directly, as it's type erased and introduces unnecessary indirect function calls and memory allocations.poll.h: DefinesPoll<T>, the return type of a promise. APollcan be eitherPendingorReadywith a value of typeT.party.h/party.cc: APartyis a tool that we use to execute promises concurrently not parallelly.activity.h/activity.cc: DefinesActivity, the execution context for promises. AnActivityis responsible for running a promise to completion, and provides a mechanism for waking up a suspended promise when it's ready to make progress.
Promise combinators are designed to be composable and can be safely nested to build more complex asynchronous logic.
- Conditionals
if.h:Ifis a combinator that conditionally executes one of two promises. It can use a promise or a boolean as a predicate.switch.h:Switchis a combinator that allows for switching between different promises based on a condition.
- Join
join.h:Joinis a combinator that takes a number of promises and returns a new promise that resolves to a tuple of the results when all of the input promises have resolved.try_join.h:TryJoinis a combinator that is similar toJoin, but it resolves with an error if any of the input promises resolve with an error.
- Loop
loop.h:Loopis a combinator that repeatedly executes a promise.for_each.h:ForEachis a combinator that iterates over a sequence and applies a function to each element, returning a promise that resolves when all of the function calls have completed.
map.h:Mapis a combinator that applies a synchronous function to the result of a promise.match_promise.h:Matchis a combinator that allows for pattern matching on the resolved type of a promise.- Races
race.h:Raceis a combinator that races multiple promises against each other.prioritized_race.h:PrioritizedRaceis a combinator that races multiple promises against each other, but with a priority given to the first promise.- All raced promises must resolve to the same type.
- Sequences
Seqseq.h:Seqis a combinator that executes a sequence of promises one after another. It resolves to the return type of the last promise in the sequence.try_seq.h:TrySeqis a combinator that is similar toSeq, but it stops executing if any of the promises in the sequence resolve with an error.
all_ok.h:AllOkis a combinator that takes a number of promises and returns a new promise that resolves when all of the input promises have resolved successfully.
inter_activity_latch.h: A latch that can be used to synchronize between different activities.inter_activity_mutex.h: A mutex that can be used to protect shared data between different activities.latch.h: A simple latch that can be used for synchronization within a single activity.promise_mutex.h: A mutex that can be used to protect shared data within a single activity.
arena_promise.h:ArenaPromise<T>is a promise that is allocated from an arena, which was be useful for performance-critical code. It is deprecated and should not be used in new codecontext.h:GetContext<T>()allows retrieving aTfrom the current activity's context.detail/: Implementation details for the promise library.event_engine_wakeup_scheduler.h: A wakeup scheduler that uses theEventEngineto schedule wakeups.exec_ctx_wakeup_scheduler.h: A wakeup scheduler that uses theExecCtxto schedule wakeups.inter_activity_pipe.h: A pipe that can be used for communication between different activities.interceptor_list.h: A list of interceptors that can be used to add functionality to a promise.mpsc.h/mpsc.cc: A multi-producer, single-consumer queue that can be used for communication between promises.observable.h:Observableis a promise that can be observed by multiple other promises.pipe.h:Pipeprovides a mechanism for communicating between two promises within the same activity.sleep.h/sleep.cc:Sleepis a promise that resolves after a given duration.status_flag.h: A simple flag that can be used to indicate the status of an asynchronous operation.wait_for_callback.h:WaitForCallbackis a promise that waits for a callback to be called.wait_set.h/wait_set.cc: A set of promises that can be waited on together.
- The promise library is a key component of gRPC's asynchronous programming model.
- It is used extensively throughout the gRPC core to implement non-blocking I/O and other asynchronous operations.
- The library provides a rich set of combinators for composing promises together to create complex asynchronous workflows.
- Pay close attention to the distinction between inter-activity and intra-activity synchronization primitives. Using the wrong one can lead to deadlocks.
- The use of
ArenaPromiseshould be considered in performance-sensitive code to avoid heap allocations. - The
detail/directory contains internal implementation details and should not be used directly by consumers of the library.
- The test files for this library are located in
test/core/promise/.