Lunchbox
1.14.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
|
A thread-safe request handler. More...
#include <requestHandler.h>
Public Member Functions | |
RequestHandler () | |
Construct a new request handler. More... | |
~RequestHandler () | |
Destruct the request handler. More... | |
template<class T > | |
Request< T > | registerRequest (void *data=0) |
Register a request. More... | |
uint32_t | registerRequest (void *data=0) |
Register a request. More... | |
void | unregisterRequest (const uint32_t requestID) |
Unregister a request. More... | |
bool | waitRequest (const uint32_t requestID, void *&result, const uint32_t timeout=LB_TIMEOUT_INDEFINITE) |
Wait a given time for the completion of a request. More... | |
bool | waitRequest (const uint32_t requestID, uint32_t &result, const uint32_t timeout=LB_TIMEOUT_INDEFINITE) |
Wait for a request with an uint32_t result. More... | |
bool | waitRequest (const uint32_t requestID, bool &result, const uint32_t timeout=LB_TIMEOUT_INDEFINITE) |
Wait for a request with a bool result. More... | |
bool | waitRequest (const uint32_t requestID, servus::uint128_t &result, const uint32_t timeout=LB_TIMEOUT_INDEFINITE) |
Wait for a request with an uint128_t result. More... | |
bool | waitRequest (const uint32_t requestID) |
Wait for a request without a result. More... | |
void * | getRequestData (const uint32_t requestID) |
Retrieve the user-specific data for a request. More... | |
void | serveRequest (const uint32_t requestID, void *result=0) |
Serve a request with a void* result. More... | |
void | serveRequest (const uint32_t requestID, uint32_t result) |
Serve a request with an uint32_t result. More... | |
void | serveRequest (const uint32_t requestID, bool result) |
Serve a request with a bool result. More... | |
void | serveRequest (const uint32_t requestID, const servus::uint128_t &result) |
Serve a request with an uint128_t result. More... | |
bool | hasPendingRequests () const |
bool | isRequestReady (const uint32_t) const |
Friends | |
std::ostream & | operator<< (std::ostream &, const RequestHandler &) |
A thread-safe request handler.
Different execution threads can synchronize using a request handler. One thread registers a request, and later waits for the request to be served. Another thread can serve the request, providing a result value.
Thread-safety: The methods registerRequest(), unregisterRequest() and waitRequest() are supposed to be called from one 'waiting' thread, and the functions serveRequest() and deleteRequest() are supposed to be called only from one 'serving' thread.
Example:
Definition at line 44 of file requestHandler.h.
lunchbox::RequestHandler::RequestHandler | ( | ) |
Construct a new request handler.
lunchbox::RequestHandler::~RequestHandler | ( | ) |
Destruct the request handler.
void* lunchbox::RequestHandler::getRequestData | ( | const uint32_t | requestID | ) |
Retrieve the user-specific data for a request.
requestID | the request identifier. |
bool lunchbox::RequestHandler::hasPendingRequests | ( | ) | const |
|
inline |
Register a request.
data | a pointer to user-specific data for the request, can be 0. |
Definition at line 164 of file requestHandler.h.
|
inline |
Register a request.
data | a pointer to user-specific data for the request, can be 0. |
Definition at line 70 of file requestHandler.h.
References LB_TIMEOUT_INDEFINITE, and lunchbox::operator<<().
void lunchbox::RequestHandler::serveRequest | ( | const uint32_t | requestID, |
void * | result = 0 |
||
) |
Serve a request with a void* result.
requestID | the request identifier. |
result | the result of the request. |
void lunchbox::RequestHandler::serveRequest | ( | const uint32_t | requestID, |
uint32_t | result | ||
) |
Serve a request with an uint32_t result.
void lunchbox::RequestHandler::serveRequest | ( | const uint32_t | requestID, |
bool | result | ||
) |
Serve a request with a bool result.
void lunchbox::RequestHandler::serveRequest | ( | const uint32_t | requestID, |
const servus::uint128_t & | result | ||
) |
Serve a request with an uint128_t result.
void lunchbox::RequestHandler::unregisterRequest | ( | const uint32_t | requestID | ) |
Unregister a request.
Note that waitRequest automatically unregisters the request when it was successful. This method is only used when a waitRequest has timed out and the request will no longer be used.
requestID | the request identifier. |
bool lunchbox::RequestHandler::waitRequest | ( | const uint32_t | requestID, |
void *& | result, | ||
const uint32_t | timeout = LB_TIMEOUT_INDEFINITE |
||
) |
Wait a given time for the completion of a request.
The request is unregistered upon successful completion, i.e, the when the method returns true.
requestID | the request identifier. |
result | the result code of the operation. |
timeout | the timeout in milliseconds to wait for the request, or LB_TIMEOUT_INDEFINITE to wait indefinitely. |
bool lunchbox::RequestHandler::waitRequest | ( | const uint32_t | requestID, |
uint32_t & | result, | ||
const uint32_t | timeout = LB_TIMEOUT_INDEFINITE |
||
) |
Wait for a request with an uint32_t result.
bool lunchbox::RequestHandler::waitRequest | ( | const uint32_t | requestID, |
bool & | result, | ||
const uint32_t | timeout = LB_TIMEOUT_INDEFINITE |
||
) |
Wait for a request with a bool result.
bool lunchbox::RequestHandler::waitRequest | ( | const uint32_t | requestID, |
servus::uint128_t & | result, | ||
const uint32_t | timeout = LB_TIMEOUT_INDEFINITE |
||
) |
Wait for a request with an uint128_t result.
bool lunchbox::RequestHandler::waitRequest | ( | const uint32_t | requestID | ) |
Wait for a request without a result.