hmbdc
simplify-high-performance-messaging-programming
Classes | Public Member Functions | List of all members
hmbdc::app::BlockingContext< MessageTuples > Class Template Reference

A BlockingContext is like a media object that facilitates the communications for the Clients that it is holding. Each Client is powered by a single OS thread. a Client needs to be started once and only once to a single BlockingContext before any messages sending happens - typically in the initialization stage in main(), undefined behavior otherwise. More...

#include <BlockingContext.hpp>

Classes

struct  MCGen
 
struct  MCGen< std::tuple< Messages... > >
 
struct  setupConduitFor
 
struct  setupConduitFor< MsgConduits, Client, DeliverPred, std::tuple< M, Messages... > >
 
struct  setupConduitFor< MsgConduits, Client, void *, std::tuple< M, Messages... > >
 
struct  Transport
 
struct  TransportEntry
 

Public Member Functions

 BlockingContext ()
 trivial ctor
 
template<typename Client >
void start (Client &c, size_t capacity=1024, size_t maxItemSize=max_size_in_tuple< typename Client::Interests >::value, uint64_t cpuAffinity=0, time::Duration maxBlockingTime=time::Duration::seconds(1))
 start a client within the Context. The client is powered by a single OS thread More...
 
template<typename Client , typename DeliverPred >
void start (Client &c, size_t capacity, size_t maxItemSize, uint64_t cpuAffinity, time::Duration maxBlockingTime, DeliverPred &&pred)
 start a client within the Context. The client is powered by a single OS thread More...
 
void stop ()
 stop the message dispatching - asynchronously More...
 
void join ()
 wait until all threads of the Context exit More...
 
template<typename Message >
void send (Message &&m)
 send a message to the BlockingContext to dispatch More...
 
template<typename Message >
bool trySend (Message &&m)
 best effort to send a message to via the BlockingContext More...
 
template<typename Message , typename... Args>
bool trySendInPlace (Args &&...args)
 it does the same thing as trySend More...
 
template<typename ForwardIt >
void send (ForwardIt begin, size_t n)
 send a range of messages via the BlockingContext More...
 

Detailed Description

template<typename... MessageTuples>
class hmbdc::app::BlockingContext< MessageTuples >

A BlockingContext is like a media object that facilitates the communications for the Clients that it is holding. Each Client is powered by a single OS thread. a Client needs to be started once and only once to a single BlockingContext before any messages sending happens - typically in the initialization stage in main(), undefined behavior otherwise.

a Client running in such a BlockingContext utilizing OS's blocking mechanism and takes less CPU time. The Client's responding time scales better when the number of Clients greatly exceeds the availlable CPUs in the system and the effective message rate for a Client tends to be low.

Template Parameters
MessageTuplesstd tuple capturing the Messages that the Context is supposed to deliver

Member Function Documentation

template<typename... MessageTuples>
void hmbdc::app::BlockingContext< MessageTuples >::join ( )
inline

wait until all threads of the Context exit

blocking call

template<typename... MessageTuples>
template<typename Message >
void hmbdc::app::BlockingContext< MessageTuples >::send ( Message &&  m)
inline

send a message to the BlockingContext to dispatch

only the Clients that handles the Message will get it This function is threadsafe, which means you can call it anywhere in the code

Template Parameters
Messagetype
Parameters
mmessage
template<typename... MessageTuples>
template<typename ForwardIt >
void hmbdc::app::BlockingContext< MessageTuples >::send ( ForwardIt  begin,
size_t  n 
)
inline

send a range of messages via the BlockingContext

only the Clients that handles the Message will get it of course This function is threadsafe, which means you can call it anywhere in the code

Parameters
begina forward iterator point at the start of the range
nlength of the range
template<typename... MessageTuples>
template<typename Client >
void hmbdc::app::BlockingContext< MessageTuples >::start ( Client c,
size_t  capacity = 1024,
size_t  maxItemSize = max_size_in_tuple<typename Client::Interests>::value,
uint64_t  cpuAffinity = 0,
time::Duration  maxBlockingTime = time::Duration::seconds(1) 
)
inline

start a client within the Context. The client is powered by a single OS thread

it is ok if a Client is blocking, if its own buffer is not full, it doesn't affect other Client's capabilities of receiving Messages

Usage example:

// the following starts 1 Client with buffer length of 2048 messages and pinning it on 3rd CPU
ctx.start(client2, 2048, 0x4ul);
Template Parameters
Clientactual Client type
Parameters
cClient
capacitythe maximum messages this Client can buffer
maxItemSizethe max size of a message
cpuAffinitycpu affinity mask for this Client's thread
maxBlockingTimeit is recommended to limit the duration a Client blocks due to no messages to handle, so it can respond to things like Context is stopped, or generate heartbeats if applicable.
template<typename... MessageTuples>
template<typename Client , typename DeliverPred >
void hmbdc::app::BlockingContext< MessageTuples >::start ( Client c,
size_t  capacity,
size_t  maxItemSize,
uint64_t  cpuAffinity,
time::Duration  maxBlockingTime,
DeliverPred &&  pred 
)
inline

start a client within the Context. The client is powered by a single OS thread

it is ok if a Client is blocking, if its own buffer is not full, it doesn't affect other Client's capabilities of receiving Messages

Template Parameters
Clientactual Client type
DeliverPreda condition functor deciding if a message should be delivered to a Client, which provides filtering before the Message type filtering. It improves performance in the way it could potentially reduce the unblocking times of a Client.

Usage example: //the following Pred verifies srcId matches for Response, and let all other Messages types //deliver

struct Pred {
Pred(uint16_t srcId)
: srcId(srcId){}
bool operator()(Response const& resp) {
return resp.srcId == srcId;
}
template <typename M>
bool operator()(M const&) {return true;}
uint16_t srcId;
};
Parameters
cClient
capacitythe maximum messages this Client can buffer
maxItemSizethe max size of a message
cpuAffinitycpu affinity mask for this Client's thread
maxBlockingTimeit is recommended to limit the duration a Client blocks due to no messages to handle, so it can respond to things like Context is stopped, or generate heartbeats if applicable.
pred
template<typename... MessageTuples>
void hmbdc::app::BlockingContext< MessageTuples >::stop ( )
inline

stop the message dispatching - asynchronously

asynchronously means not garanteed message dispatching stops immidiately after this non-blocking call

template<typename... MessageTuples>
template<typename Message >
bool hmbdc::app::BlockingContext< MessageTuples >::trySend ( Message &&  m)
inline

best effort to send a message to via the BlockingContext

this method is not recommended if more than one recipients are accepting this message since there is no garantee each one will receive it once and only once. this call does not block - return false when deliver doesn't reach all intended recipients This method is threadsafe, which means you can call it anywhere in the code

Parameters
mmessage
Returns
true if send successfully to one or no one is interested
template<typename... MessageTuples>
template<typename Message , typename... Args>
bool hmbdc::app::BlockingContext< MessageTuples >::trySendInPlace ( Args &&...  args)
inline

it does the same thing as trySend

it is provided for interface compliance

Parameters
argsctor args
Template Parameters
Messagetype
typename... Args args
Returns
true if send successfully

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