Lunchbox  1.4.0
Public Member Functions | Friends
lunchbox::RequestHandler Class Reference

A thread-safe request handler. More...

#include <requestHandler.h>

+ Inheritance diagram for lunchbox::RequestHandler:
+ Collaboration diagram for lunchbox::RequestHandler:

List of all members.

Public Member Functions

 RequestHandler ()
 Construct a new request handler.
 ~RequestHandler ()
 Destruct the request handler.
uint32_t registerRequest (void *data=0)
 Register a new request.
void unregisterRequest (const uint32_t requestID)
 Unregister a request.
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.
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.
bool waitRequest (const uint32_t requestID, bool &result, const uint32_t timeout=LB_TIMEOUT_INDEFINITE)
 Wait for a request with a bool result.
bool waitRequest (const uint32_t requestID, uint128_t &result, const uint32_t timeout=LB_TIMEOUT_INDEFINITE)
 Wait for a request with an uint128_t result.
bool waitRequest (const uint32_t requestID)
 Wait for a request without a result.
bool isRequestServed (const uint32_t requestID) const
 Poll for the completion of a request.
void * getRequestData (const uint32_t requestID)
 Retrieve the user-specific data for a request.
void serveRequest (const uint32_t requestID, void *result=0)
 Serve a request with a void* result.
void serveRequest (const uint32_t requestID, uint32_t result)
 Serve a request with an uint32_t result.
void serveRequest (const uint32_t requestID, bool result)
 Serve a request with a bool result.
void serveRequest (const uint32_t requestID, const uint128_t &result)
 Serve a request with an uint128_t result.
bool hasPendingRequests () const

Friends

std::ostream & operator<< (std::ostream &, const RequestHandler &)

Detailed Description

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.

A note on thread-safety: Unless threadSafe is set in the constructor, the functions 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.

Definition at line 44 of file requestHandler.h.


Constructor & Destructor Documentation

Construct a new request handler.

Version:
1.0

Destruct the request handler.


Member Function Documentation

void* lunchbox::RequestHandler::getRequestData ( const uint32_t  requestID)

Retrieve the user-specific data for a request.

Parameters:
requestIDthe request identifier.
Returns:
the user-specific data for the request.
Version:
1.0
Returns:
true if this request handler has pending requests.
Version:
1.0
bool lunchbox::RequestHandler::isRequestServed ( const uint32_t  requestID) const

Poll for the completion of a request.

Does not unregister the request.

Parameters:
requestIDthe request identifier.
Returns:
true if the request has been served, false if it is pending.
Version:
1.0
uint32_t lunchbox::RequestHandler::registerRequest ( void *  data = 0)

Register a new request.

Parameters:
dataa pointer to user-specific data for the request, can be 0.
Returns:
the request identifier.
Version:
1.0
void lunchbox::RequestHandler::serveRequest ( const uint32_t  requestID,
void *  result = 0 
)

Serve a request with a void* result.

Parameters:
requestIDthe request identifier.
resultthe result of the request.
Version:
1.0
void lunchbox::RequestHandler::serveRequest ( const uint32_t  requestID,
uint32_t  result 
)

Serve a request with an uint32_t result.

Version:
1.0
void lunchbox::RequestHandler::serveRequest ( const uint32_t  requestID,
bool  result 
)

Serve a request with a bool result.

Version:
1.0
void lunchbox::RequestHandler::serveRequest ( const uint32_t  requestID,
const uint128_t result 
)

Serve a request with an uint128_t result.

Version:
1.0
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.

Parameters:
requestIDthe request identifier.
Version:
1.0
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.

Parameters:
requestIDthe request identifier.
resultthe result code of the operation.
timeoutthe timeout in milliseconds to wait for the request, or LB_TIMEOUT_INDEFINITE to wait indefinitely.
Returns:
true if the request was served, false if not.
Version:
1.0
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.

Version:
1.0
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.

Version:
1.0
bool lunchbox::RequestHandler::waitRequest ( const uint32_t  requestID,
uint128_t result,
const uint32_t  timeout = LB_TIMEOUT_INDEFINITE 
)

Wait for a request with an uint128_t result.

Version:
1.0
bool lunchbox::RequestHandler::waitRequest ( const uint32_t  requestID)

Wait for a request without a result.

Version:
1.0

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