LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Private Member Functions | Private Attributes
LogCabin::RPC::ThreadDispatchService Class Reference

This class is an adaptor to enable multi-threaded services. More...

#include <ThreadDispatchService.h>

Inheritance diagram for LogCabin::RPC::ThreadDispatchService:
LogCabin::RPC::Service

List of all members.

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 &)
ThreadDispatchServiceoperator= (const ThreadDispatchService &)

Private Attributes

std::shared_ptr< ServicethreadSafeService
 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< ServerRPCrpcQueue
 The queue of work that worker threads pull from.

Detailed Description

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.


Constructor & Destructor Documentation

LogCabin::RPC::ThreadDispatchService::ThreadDispatchService ( std::shared_ptr< Service threadSafeService,
uint32_t  minThreads,
uint32_t  maxThreads 
)

Constructor.

Parameters:
threadSafeServiceThe underlying service that will handle RPCs inside of worker threads spawned by this class.
minThreadsThe number of threads with which to start the thread pool. These will be created in the constructor.
maxThreadsThe 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.


Member Function Documentation

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.

The main loop executed in workers.

Definition at line 84 of file ThreadDispatchService.cc.

ThreadDispatchService& LogCabin::RPC::ThreadDispatchService::operator= ( const ThreadDispatchService ) [private]

Member Data Documentation

The service that will handle RPCs inside of worker thread spawned by this class.

Definition at line 77 of file ThreadDispatchService.h.

The maximum number of threads this class is allowed to use for its thread pool.

Definition at line 83 of file ThreadDispatchService.h.

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.

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.

A flag to tell workers that they should exit.

Definition at line 112 of file ThreadDispatchService.h.

The queue of work that worker threads pull from.

Definition at line 117 of file ThreadDispatchService.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines