LogCabin
|
This class is an adaptor to enable multi-threaded services. More...
#include <ThreadDispatchService.h>
Public Member Functions | |
ThreadDispatchService (std::shared_ptr< Service > threadSafeService, uint32_t minThreads, uint32_t maxThreads) | |
Constructor. | |
~ThreadDispatchService () | |
Destructor. | |
void | handleRPC (ServerRPC serverRPC) |
This method is overridden by a subclass and invoked by the Server class when a new RPC arrives. | |
std::string | getName () const |
Return a short name for this service which can be used in things like log messages. | |
Private Member Functions | |
void | workerMain () |
The main loop executed in workers. | |
ThreadDispatchService (const ThreadDispatchService &) | |
ThreadDispatchService & | operator= (const ThreadDispatchService &) |
Private Attributes | |
std::shared_ptr< Service > | threadSafeService |
The service that will handle RPCs inside of worker thread spawned by this class. | |
const uint32_t | maxThreads |
The maximum number of threads this class is allowed to use for its thread pool. | |
std::mutex | mutex |
This mutex protects all of the members of this class defined below this point. | |
std::vector< std::thread > | threads |
The thread pool of workers that process RPCs. | |
uint32_t | numFreeWorkers |
The number of workers that are waiting for work (on the condition variable). | |
Core::ConditionVariable | conditionVariable |
Notifies workers that there are available RPCs to process or exit has been set. | |
bool | exit |
A flag to tell workers that they should exit. | |
std::queue< ServerRPC > | rpcQueue |
The queue of work that worker threads pull from. |
This class is an adaptor to enable multi-threaded services.
This Service is intended to plug into a Server and run directly on the Event::Loop thread. You provide it with another Service on the constructor, and the job of this class is to manage a thread pool on which to call your Service's handleRPC() method.
Definition at line 39 of file ThreadDispatchService.h.
LogCabin::RPC::ThreadDispatchService::ThreadDispatchService | ( | std::shared_ptr< Service > | threadSafeService, |
uint32_t | minThreads, | ||
uint32_t | maxThreads | ||
) |
Constructor.
threadSafeService | The underlying service that will handle RPCs inside of worker threads spawned by this class. |
minThreads | The number of threads with which to start the thread pool. These will be created in the constructor. |
maxThreads | The maximum number of threads this class is allowed to use for its thread pool. The thread pool dynamically grows as needed up until this limit. This should be set to at least 'minThreads' and more than 0. |
Definition at line 25 of file ThreadDispatchService.cc.
Destructor.
This will attempt to join all threads and will close sessions on RPCs that have not been serviced.
Definition at line 44 of file ThreadDispatchService.cc.
LogCabin::RPC::ThreadDispatchService::ThreadDispatchService | ( | const ThreadDispatchService & | ) | [private] |
void LogCabin::RPC::ThreadDispatchService::handleRPC | ( | ServerRPC | serverRPC | ) | [virtual] |
This method is overridden by a subclass and invoked by the Server class when a new RPC arrives.
It should call ServerRPC::reply() or another method on the RPC to handle the request. The Server class calls this on a thread pool.
Implements LogCabin::RPC::Service.
Definition at line 67 of file ThreadDispatchService.cc.
std::string LogCabin::RPC::ThreadDispatchService::getName | ( | ) | const [virtual] |
Return a short name for this service which can be used in things like log messages.
Implements LogCabin::RPC::Service.
Definition at line 78 of file ThreadDispatchService.cc.
void LogCabin::RPC::ThreadDispatchService::workerMain | ( | ) | [private] |
The main loop executed in workers.
Definition at line 84 of file ThreadDispatchService.cc.
ThreadDispatchService& LogCabin::RPC::ThreadDispatchService::operator= | ( | const ThreadDispatchService & | ) | [private] |
std::shared_ptr<Service> LogCabin::RPC::ThreadDispatchService::threadSafeService [private] |
The service that will handle RPCs inside of worker thread spawned by this class.
Definition at line 77 of file ThreadDispatchService.h.
const uint32_t LogCabin::RPC::ThreadDispatchService::maxThreads [private] |
The maximum number of threads this class is allowed to use for its thread pool.
Definition at line 83 of file ThreadDispatchService.h.
std::mutex LogCabin::RPC::ThreadDispatchService::mutex [private] |
This mutex protects all of the members of this class defined below this point.
Definition at line 89 of file ThreadDispatchService.h.
std::vector<std::thread> LogCabin::RPC::ThreadDispatchService::threads [private] |
The thread pool of workers that process RPCs.
Definition at line 94 of file ThreadDispatchService.h.
uint32_t LogCabin::RPC::ThreadDispatchService::numFreeWorkers [private] |
The number of workers that are waiting for work (on the condition variable).
This is used to dynamically launch new workers when necessary.
Definition at line 101 of file ThreadDispatchService.h.
Notifies workers that there are available RPCs to process or exit has been set.
To wait on this, one needs to hold mutex.
Definition at line 107 of file ThreadDispatchService.h.
bool LogCabin::RPC::ThreadDispatchService::exit [private] |
A flag to tell workers that they should exit.
Definition at line 112 of file ThreadDispatchService.h.
std::queue<ServerRPC> LogCabin::RPC::ThreadDispatchService::rpcQueue [private] |
The queue of work that worker threads pull from.
Definition at line 117 of file ThreadDispatchService.h.