Mercury Currency Engine
Classes | Functions
await.hpp File Reference
#include <deque>
#include <memory>
#include "function_utility.hpp"
#include "scheduler.hpp"
#include "threadpool.hpp"
#include "unbuffered_channel.hpp"
Include dependency graph for await.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mce::detail::await_coroutine
 
struct  mce::detail::await_worker
 
struct  mce::detail::await_worker::worker_task
 
struct  mce::detail::await_threadpool
 specialized threadpool implementation for handling blocking tasks More...
 

Functions

template<typename Callable , typename... As>
detail::function_return_type< Callable, As... > mce::detail::await_ (std::false_type, Callable &&cb, As &&... args)
 
template<typename Callable , typename... As>
int mce::detail::await_ (std::true_type, Callable &&cb, As &&... args)
 
template<typename Callable , typename... As>
detail::convert_void_return< Callable, As... > mce::await (Callable &&cb, As &&... args)
 Execute Callable potentially on a different thread and block current context until operation completes. More...
 
bool mce::is_await ()
 
size_t mce::await_count ()
 

Detailed Description

trivial execution of blocking operations (such as in/out operations)

Function Documentation

◆ await()

template<typename Callable , typename... As>
detail::convert_void_return<Callable,As...> mce::await ( Callable &&  cb,
As &&...  args 
)
inline

Execute Callable potentially on a different thread and block current context until operation completes.

await() accepts a Callable, and any number of arguments, and returns the result of calling the given Callable with the given arguments. await() is a special function which enables blocking Callable calls made from a coroutine to be executed safely so that it does not block other coroutines from running.

Anything that does an OS level block (non-mce sleeping, std::mutex::lock(), std::condition_variable::wait(), file operations, etc...) can be executed within this call without blocking other scheduler coroutines.

Because the caller of mce::await() is blocked during this operations runtime, it is safe to access values on the caller's stack by reference.

If mce::await() is called outside of a coroutine running on a mce::scheduler OR mce::await() was called inside another call to mce::await(), the argument Callable is executed immediately on the current thread instead of on another thread. Otherwise, the calling coroutine will be blocked an the operation passed to mce::await() will be executed on another, dedicated thread inside another mce::scheduler.

Calls (or implicit calls) to this_scheduler(), or this_threadpool() made within a call to mce::await() will return the values they would have if they had been called outside of mce::await(). That is, code running in mce::await() will be able to schedule operations as if it was running in its original execution environment. IE, calls to mce::concurrent() or mce::parallel() will function "normally".

Returns
the result of the Callable or 0 (if Callable returns void)

◆ await_count()

size_t mce::await_count ( )
inline
Returns
the count of active await worker threads

◆ is_await()

bool mce::is_await ( )
inline
Returns
true if executing on an await() managed thread, else false