Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::base::WorkerPool Class Reference

Manages one or more threads that run in a loop, performing some work with each iteration (if any work is available). More...

#include "workerpool.h"

Inheritance diagram for ion::base::WorkerPool:
Collaboration diagram for ion::base::WorkerPool:

Classes

class  Worker
 Interface to enable pluggable worker behavior. More...
 

Public Member Functions

 WorkerPool (Worker *worker)
 Constructor/destructor. More...
 
 ~WorkerPool () override
 
const std::string & GetName () const
 Gets a descriptive name for the pool from the worker. More...
 
void Suspend ()
 Suspends all threads until Resume() is called. More...
 
void Resume ()
 Resumes all threads. More...
 
bool IsSuspended () const
 Return true if pool's threads are suspended. More...
 
void ResizeThreadPool (size_t thread_count)
 Changes the number of theads in the pool. More...
 
port::SemaphoreGetWorkSemaphore ()
 Returns the semaphore that is used to signal that a unit of work is available to process. More...
 
const AllocatorPtrGetAllocator () const
 Returns the Allocator that was used for the instance. More...
 
const AllocatorPtrGetNonNullAllocator () const
 Return our allocator, or the default allocator if the instance was declared on the stack. More...
 
const AllocatorPtrGetAllocatorForLifetime (AllocationLifetime lifetime) const
 Convenience function that returns the Allocator to use to allocate an object with a specific lifetime. More...
 
void * operator new (size_t size)
 The standard no-parameter new operator uses the default Allocator. More...
 
void * operator new (size_t size, AllocationLifetime lifetime)
 This overloaded version of the new operator uses the AllocationManager's default Allocator for the specified lifetime. More...
 
void * operator new (size_t size, const AllocatorPtr &allocator)
 This overloaded version of the new operator takes the Allocator to use directly as a parameter. More...
 
void * operator new (size_t size, const AllocatorPtr &allocator, void *ptr)
 Special operator new for using placement new with Allocatables. More...
 
void * operator new (size_t size, void *ptr)
 The placement new operator is defined conventionally. More...
 
void operator delete (void *ptr)
 Define the delete operator to use specialized functions dealing with an Allocator. More...
 
void operator delete (void *ptr, AllocationLifetime lifetime)
 Windows requires these (or it issues C4291 warnings). More...
 
void operator delete (void *ptr, const AllocatorPtr &allocator)
 
void operator delete (void *ptr, void *ptr2)
 The placement delete operator does nothing, as usual. More...
 

Protected Member Functions

virtual void ThreadEntryPoint ()
 ThreadEntryPoint() is run on each created thread. More...
 

Detailed Description

Manages one or more threads that run in a loop, performing some work with each iteration (if any work is available).

Newly-constructed WorkerPools are "suspended"... Resume() must be called in order for work to commence. This allows the user of this code to guaranteed that the "worker" object has been fully-constructed by the time that Resume() is called. For example, an anticipated use-case is to have an class which:

  • encapsulates a WorkerPool
  • implements Worker
  • passes itself via "this" to the WorkerPool's constructor. If the DoWork() method is implemented in a subclass, undefined behavior will result if a thread calls DoWork() before all constructors finish. Similarly, Suspend() must be called before the subclass destructor is invoked.

Definition at line 46 of file workerpool.h.

Constructor & Destructor Documentation

ion::base::WorkerPool::WorkerPool ( WorkerPool::Worker worker)
explicit

Constructor/destructor.

Definition at line 25 of file workerpool.cc.

References ThreadEntryPoint().

ion::base::WorkerPool::~WorkerPool ( )
override

Member Function Documentation

const AllocatorPtr& ion::base::Allocatable::GetAllocator ( ) const
inlineinherited

Returns the Allocator that was used for the instance.

This will be NULL if the instance was declared on the stack or created with normal placement new.

Definition at line 68 of file allocatable.h.

References allocator_.

Referenced by ion::base::DataContainer::CreateAndCopy(), ion::text::DynamicFontImage::FindContainingImageDataIndex(), and ion::text::DynamicFontImage::FindImageDataIndex().

const std::string& ion::base::WorkerPool::GetName ( ) const
inline

Gets a descriptive name for the pool from the worker.

Definition at line 79 of file workerpool.h.

References ion::base::WorkerPool::Worker::GetName().

const AllocatorPtr& ion::base::Allocatable::GetNonNullAllocator ( ) const
inlineinherited

Return our allocator, or the default allocator if the instance was declared on the stack.

Definition at line 72 of file allocatable.h.

References allocator_, and ion::base::AllocationManager::GetNonNullAllocator().

port::Semaphore* ion::base::WorkerPool::GetWorkSemaphore ( )
inline

Returns the semaphore that is used to signal that a unit of work is available to process.

Definition at line 94 of file workerpool.h.

bool ion::base::WorkerPool::IsSuspended ( ) const

Return true if pool's threads are suspended.

void ion::base::Allocatable::operator delete ( void *  ptr)
inlineinherited

Define the delete operator to use specialized functions dealing with an Allocator.

Definition at line 109 of file allocatable.h.

void ion::base::Allocatable::operator delete ( void *  ptr,
AllocationLifetime  lifetime 
)
inlineinherited

Windows requires these (or it issues C4291 warnings).

Definition at line 112 of file allocatable.h.

void ion::base::Allocatable::operator delete ( void *  ptr,
const AllocatorPtr allocator 
)
inlineinherited

Definition at line 113 of file allocatable.h.

void ion::base::Allocatable::operator delete ( void *  ptr,
void *  ptr2 
)
inlineinherited

The placement delete operator does nothing, as usual.

Definition at line 118 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size)
inlineinherited

The standard no-parameter new operator uses the default Allocator.

Definition at line 84 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
AllocationLifetime  lifetime 
)
inlineinherited

This overloaded version of the new operator uses the AllocationManager's default Allocator for the specified lifetime.

Definition at line 88 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
const AllocatorPtr allocator 
)
inlineinherited

This overloaded version of the new operator takes the Allocator to use directly as a parameter.

If the Allocator pointer is NULL, this uses the default Allocator.

Definition at line 95 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
const AllocatorPtr allocator,
void *  ptr 
)
inlineinherited

Special operator new for using placement new with Allocatables.

Definition at line 100 of file allocatable.h.

void* ion::base::Allocatable::operator new ( size_t  size,
void *  ptr 
)
inlineinherited

The placement new operator is defined conventionally.

Definition at line 105 of file allocatable.h.

void ion::base::WorkerPool::ResizeThreadPool ( size_t  thread_count)

Changes the number of theads in the pool.

void ion::base::WorkerPool::Resume ( )

Resumes all threads.

void ion::base::WorkerPool::Suspend ( )

Suspends all threads until Resume() is called.

Wait for each thread to finish what they're doing and acknowledge the suspend-request.

virtual void ion::base::WorkerPool::ThreadEntryPoint ( )
protectedvirtual

ThreadEntryPoint() is run on each created thread.

Subclasses may override this method, but they must call the superclass implementation.

IMPORTANT: subclasses which override ThreadEntryPoint() MUST call ResizeThreadPool(0) in their destructor to ensure that the vtable isn't changed while there exist active invocations of ThreadEntryPoint().

Referenced by WorkerPool().


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