fiber
Loading...
Searching...
No Matches
fiber::Promise< T > Class Template Reference

Promise and Future pairs are used to synchronise values between asynchronous tasks. More...

#include <Future.hpp>

Public Member Functions

 Promise ()=default
 
 Promise (const Promise &)=delete
 
Promiseoperator= (Promise &&oldPromise)
 Thread and interrupt save move save move that re-registers stack pointers in between the future and the promise.
 
 Promise (Promise &&oldPromise)
 
bool is_connected_to (const Future< T > &future) const
 Checks if the passed future is the one connected to this promise.
 
 ~Promise () noexcept
 Signals a broken promise to the future if no value has been assigned to the promise before destruction.
 
void set_value (const T &value)
 Sets a value to the future that this promise is based on.
 
Promiseoperator= (const T &value)
 equivalent to set_value()
 
void set_value (T &&value)
 Sets a value to the future that this promise is based on.
 
Promiseoperator= (T &&value)
 equivalent to set_value()
 

Friends

class Future< T >
 
FuturePromisePair< T > make_future_promise ()
 creates a linked future promise pair
 

Detailed Description

template<class T>
class fiber::Promise< T >

Promise and Future pairs are used to synchronise values between asynchronous tasks.

If you are on a single core bare-metal embedded system (aka. all context switching is interrupt driven) you can set the FIBER_MULTI_CORE definiteion flag for optimizations - smaller binary and faster execution. It will then remove all the locks and logic that is needed for multi-core thread safety.

Example: one wants to copy data asynchronously (for example with a DMA controller) while doing some computations in the mean time.

// promise for CRC interrupts
static Promise<uint32_t> crc_promise;
// async function that accepts a promise for example when called from a future holder
fiber::Future<uint32_t> async_crc(const void* data, size_t dataLength){
// create a future promise pair use:
auto [crc_future, crc_promise] = fiber::make_future_promise<uint32_t>();
// start the CRC calculation
CRC_DMA_start(data, dataLength);
// return before the CRC computation finished
return crc_future;
}
// Once the CRC computation finished the interrupt will be called
void crc_complete_interrupt(){
// keep the promise by assigning value to it
crc_promise.set_value(CRC->result_register);
}
int main(){
// ...
Future<uint32_t> crc_future = async_crc(message, length);
// let the crc hardware do its thing and do some other math in the mean time
// ...
// ... some math ...
// ...
// now we need the crc. The .get() method will either wait for the value from the intterupt befor returning it.
send_message(message, length, crc_future.get());
}
Future and Promise pairs are used to synchronise values between asynchronous tasks.
Definition Future.hpp:62
void set_value(const T &value)
Sets a value to the future that this promise is based on.
Definition Future.hpp:560
FuturePromisePair< T > make_future_promise()
creates a linked future promise pair
Definition Future.hpp:751
int main()
Definition test_main.cpp:23

Constructor & Destructor Documentation

◆ Promise() [1/3]

template<class T>
fiber::Promise< T >::Promise ( )
default

◆ Promise() [2/3]

template<class T>
fiber::Promise< T >::Promise ( const Promise< T > & )
delete

◆ Promise() [3/3]

template<class T>
fiber::Promise< T >::Promise ( Promise< T > && oldPromise)
inline

◆ ~Promise()

template<class T>
fiber::Promise< T >::~Promise ( )
inlinenoexcept

Signals a broken promise to the future if no value has been assigned to the promise before destruction.

Member Function Documentation

◆ is_connected_to()

template<class T>
bool fiber::Promise< T >::is_connected_to ( const Future< T > & future) const
inline

Checks if the passed future is the one connected to this promise.

Returns
true if the passed future is the same that this promise is connected to.

◆ operator=() [1/3]

template<class T>
Promise & fiber::Promise< T >::operator= ( const T & value)
inline

equivalent to set_value()

◆ operator=() [2/3]

template<class T>
Promise & fiber::Promise< T >::operator= ( Promise< T > && oldPromise)
inline

Thread and interrupt save move save move that re-registers stack pointers in between the future and the promise.

◆ operator=() [3/3]

template<class T>
Promise & fiber::Promise< T >::operator= ( T && value)
inline

equivalent to set_value()

◆ set_value() [1/2]

template<class T>
void fiber::Promise< T >::set_value ( const T & value)
inline

Sets a value to the future that this promise is based on.

Parameters
valueThe value that should be set to the Future and "keep the promise".
Exceptions
anstd::exception on double writes

◆ set_value() [2/2]

template<class T>
void fiber::Promise< T >::set_value ( T && value)
inline

Sets a value to the future that this promise is based on.

Parameters
valueThe value that should be set to the Future and "keep the promise"
Exceptions
anfiber::Exception on double writes

Friends And Related Symbol Documentation

◆ Future< T >

template<class T>
friend class Future< T >
friend

◆ make_future_promise

template<class T>
FuturePromisePair< T > make_future_promise ( )
friend

creates a linked future promise pair

Example:

auto [future, promise] = make_future_promise<int>();
friend FuturePromisePair< T > make_future_promise()
creates a linked future promise pair
Definition Future.hpp:751
Template Parameters
TThe type that is being promised by the promise and awaited by the future
Returns
A linked FuturePromisePair Future promise pair

The documentation for this class was generated from the following file: