hmbdc
simplify-high-performance-messaging-programming
|
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 | can_handle |
struct | createEntry |
struct | deliverAll |
struct | MCGen |
struct | MCGen< std::tuple< Messages...> > |
struct | setupConduit |
struct | setupConduit< MsgConduits, DeliverPred, std::tuple< M, Messages...> > |
struct | setupConduit< MsgConduits, void *, std::tuple< M, Messages...> > |
struct | Transport |
struct | TransportEntry |
Public Member Functions | |
template<typename U = Requests> | |
BlockingContext (typename std::enable_if< std::tuple_size< U >::value==0, void >::type *=nullptr) | |
trivial ctor | |
template<typename Client , typename DeliverPred = deliverAll> | |
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), DeliverPred &&pred=DeliverPred()) |
start a client within the Context. The client is powered by a single OS thread. More... | |
template<typename LoadSharingClientPtrIt , typename DeliverPred = deliverAll> | |
void | start (LoadSharingClientPtrIt begin, LoadSharingClientPtrIt end, size_t capacity=1024, size_t maxItemSize=max_size_in_tuple< typename std::decay< decltype(**LoadSharingClientPtrIt())>::type::Interests >::value, uint64_t cpuAffinity=0, time::Duration maxBlockingTime=time::Duration::seconds(1), DeliverPred &&pred=DeliverPred()) |
start a group of clients within the Context that collectively processing messages in a load sharing manner. Each 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<MessageC Message> | |
std::enable_if< can_handle < Message >::match, void > ::type | send (Message &&m) |
send a message to the BlockingContext to dispatch More... | |
template<MessageC Message, typename... Args> | |
std::enable_if< can_handle < Message >::match, void > ::type | sendInPlace (Args &&...args) |
send a message by directly constructing the message in receiving message queue More... | |
template<MessageC Message> | |
std::enable_if< can_handle < Message >::match, bool > ::type | trySend (Message &&m, time::Duration timeout=time::Duration::seconds(0)) |
best effort to send a message to via the BlockingContext More... | |
template<MessageC Message, typename... Args> | |
std::enable_if< can_handle < Message >::match, bool > ::type | trySendInPlace (Args &&...args) |
best effort to send a message by directly constructing the message in receiving message queue More... | |
template<MessageForwardIterC ForwardIt> | |
std::enable_if< can_handle < decltype(*(ForwardIt()))> ::match, void >::type | send (ForwardIt begin, size_t n) |
send a range of messages via the BlockingContext More... | |
template<MessageC M, typename ReplyHandler > | |
bool | requestReply (REQUEST< M > &&request, ReplyHandler &replyHandler, time::Duration timeout=time::Duration::seconds(0xffffffff)) |
part of synchronous request reply interface. The caller is blocked until the replies to this request are heard and processed. Just liek regular Messages REQUEST is sent out to Clients that are interested in this Context More... | |
template<ReplyC ReplyT> | |
void | sendReply (ReplyT &&reply) |
part of synchronous request reply interface. Send a constructed REPLY to the Context, it will only reach to the origianl sender Client. ALL REPLY need to sent out using this interface to avoid undefined behavior. More... | |
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.
MessageTuples | std tuple capturing the Messages that the Context is supposed to deliver. Messages that not listed here are silently dropped to ensure loose coupling between senders and receivers |
|
inline |
wait until all threads of the Context exit
blocking call
|
inline |
part of synchronous request reply interface. The caller is blocked until the replies to this request are heard and processed. Just liek regular Messages REQUEST is sent out to Clients that are interested in this Context
MessageC | Message the type wrapped in REQUEST<> template |
typename | ReplyHandler type for how to handle the replies. example of a handler expect two steps replies for requestReply call: * struct ClientAlsoHandlesReplies
* : Client<ClientAlsoHandlesReplies, REPLY<Reply0>, REPLY<Reply1>, OtherMsg> {
* void handleReplyCb(Reply0 const&){
* // got step 1 reply
* }
* void handleReplyCb(Reply1 const&){
* // got step 2 reply
* // throw to unblock the requestReply call
* }
*
|
request | the REQUEST sent out |
replyHandler | the callback logic to handle replies |
timeout | timeout value |
|
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
Message | type |
m | message |
|
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
begin | a forward iterator point at the start of the range |
n | length of the range |
|
inline |
send a message by directly constructing the message in receiving message queue
since the message only exists after being deleivered, the DeliverPred is not called to decide if it should be delivered
args | ctor args |
Message | type |
typename | ... Args args |
|
inline |
|
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
Client | actual Client type |
DeliverPred | a 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 thread |
Usage example: //the following Pred verifies srcId matches for Response, and let all other Messages types //deliver
c | Client |
capacity | the maximum messages this Client can buffer |
maxItemSize | the max size of a message - when the Client is interetsed in JustBytes, this value MUST be big enough to hold the max size message to avoid truncation |
cpuAffinity | cpu affinity mask for this Client's thread |
maxBlockingTime | it 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 | see DeliverPred documentation |
|
inline |
start a group of clients within the Context that collectively processing messages in a load sharing manner. Each 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
LoadSharingClientPtrIt | iterator to a Client pointer |
DeliverPred | a condition functor deciding if a message should be delivered to these Clients, which provides filtering before the Message type filtering. It improves performance in the way it could potentially reduce the unblocking times of threads. An exception is for Client that are interested in JustBytes (basically every message), this filter does not apply - no filtering for these Clients |
begin | an iterator, **begin should produce a Client& for the first Client |
end | an end iterator, [begin, end) is the range for Clients |
capacity | the maximum messages this Client can buffer |
maxItemSize | the max size of a message - when the Client is interetsed in JustBytes, this value MUST be big enough to hold the max size message to avoid truncation |
cpuAffinity | cpu affinity mask for this Client's thread |
maxBlockingTime | it 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 | see DeliverPred documentation |
|
inline |
stop the message dispatching - asynchronously
asynchronously means not garanteed message dispatching stops immidiately after this non-blocking call
|
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
m | message |
timeout | return false if cannot deliver in the specified time |
|
inline |
best effort to send a message by directly constructing the message in receiving message queue
since the message only exists after being deleivered, the DeliverPred is not called to decide if it should be delivered this method is not recommended if more than one recipients are accepting this message since it is hard to ensure each message is delivered once and only once. this call does not block - return false when delivery doesn't reach ALL intended recipients This method is threadsafe, which means you can call it anywhere in the code