NCBI C++ ToolKit
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CThreadPool_Impl Class Reference

Search Toolkit Book for CThreadPool_Impl

Real implementation of all ThreadPool functions. More...

+ Inheritance diagram for CThreadPool_Impl:
+ Collaboration diagram for CThreadPool_Impl:

Classes

struct  SExclusiveTaskInfo
 Type for storing information about exclusive task launching. More...
 

Public Types

typedef CThreadPool::TExclusiveFlags TExclusiveFlags
 
- Public Types inherited from CObject
enum  EAllocFillMode { eAllocFillNone = 1 , eAllocFillZero , eAllocFillPattern }
 Control filling of newly allocated memory. More...
 
typedef CObjectCounterLocker TLockerType
 Default locker type for CRef. More...
 
typedef atomic< Uint8TCounter
 Counter type is CAtomiCounter. More...
 
typedef Uint8 TCount
 Alias for value type of counter. More...
 

Public Member Functions

 CThreadPool_Impl (CThreadPool *pool_intf, unsigned int queue_size, unsigned int max_threads, unsigned int min_threads, CThread::TRunMode threads_mode=CThread::fRunDefault)
 Constructor with default controller. More...
 
 CThreadPool_Impl (CThreadPool *pool_intf, unsigned int queue_size, CThreadPool_Controller *controller, CThread::TRunMode threads_mode=CThread::fRunDefault)
 Constructor with explicitly given controller. More...
 
CThreadPoolGetPoolInterface (void) const
 Get pointer to ThreadPool interface object. More...
 
void SetDestroyTimeout (const CTimeSpan &timeout)
 Set destroy timeout for the pool. More...
 
const CTimeSpanGetDestroyTimeout (void) const
 Get destroy timeout for the pool. More...
 
void DestroyReference (void)
 Destroy reference to this object Method is called when CThreadPool object is destroyed which means that implementation can be destroyed too if there is no references to it left. More...
 
CMutexGetMainPoolMutex (void)
 Get main pool mutex. More...
 
void AddTask (CThreadPool_Task *task, const CTimeSpan *timeout)
 Add task to the pool. More...
 
void CancelTask (CThreadPool_Task *task)
 Request to cancel the task. More...
 
void CancelTasks (TExclusiveFlags tasks_group)
 Cancel the selected groups of tasks in the pool. More...
 
void RequestExclusiveExecution (CThreadPool_Task *task, TExclusiveFlags flags)
 Add the task for exclusive execution in the pool. More...
 
void LaunchThreads (unsigned int count)
 Launch new threads in pool. More...
 
void FinishThreads (unsigned int count)
 Finish threads in pool Stop first all idle threads then stop busy threads without cancelation of currently executing tasks. More...
 
unsigned int GetThreadsCount (void) const
 Get number of threads running in the pool. More...
 
bool SetThreadIdle (CThreadPool_ThreadImpl *thread, bool is_idle)
 Mark thread as idle or non-idle. More...
 
void ThreadStopped (CThreadPool_ThreadImpl *thread)
 Callback from working thread when it finished its Main() method. More...
 
void ThreadStateChanged (void)
 Callback when some thread changed its idleness or finished (including service thread) More...
 
CRef< CThreadPool_TaskTryGetNextTask (void)
 Get next task from queue if there is one If the queue is empty then return NULL. More...
 
void TaskStarting (void)
 Callback from thread when it is starting to execute task. More...
 
void TaskFinished (void)
 Callback from thread when it has finished to execute task. More...
 
unsigned int GetQueuedTasksCount (void) const
 Get the number of tasks currently waiting in queue. More...
 
unsigned int GetExecutingTasksCount (void) const
 Get the number of currently executing tasks. More...
 
SExclusiveTaskInfo TryGetExclusiveTask (void)
 Get the next exclusive task to execute. More...
 
void RequestSuspend (TExclusiveFlags flags)
 Request suspension of the pool. More...
 
void ResumeWork (void)
 Resume the pool operation after exclusive task execution. More...
 
bool IsSuspended (void) const
 Check if the pool is suspended for exclusive execution. More...
 
bool CanDoExclusiveTask (void) const
 Check if it is already allowed to execute exclusive task. More...
 
void Abort (const CTimeSpan *timeout)
 Abort the pool operation. More...
 
bool IsAborted (void) const
 Check if the pool is already aborted. More...
 
void FlushThreads (CThreadPool::EFlushType flush_type)
 Finish all current threads and replace them with new ones. More...
 
void CallController (CThreadPool_Controller::EEvent event)
 Call the CThreadPool_Controller::HandleEvent() method of the pool controller with the given event type. More...
 
void CallControllerOther (void)
 Schedule running of CThreadPool_Controller::HandleEvent() with eOther event type. More...
 
CTimeSpan GetSafeSleepTime (void) const
 Call the CThreadPool_Controller::GetSafeSleepTime() method of the pool controller. More...
 
void SetInterfaceStarted (void)
 Mark that initialization of the interface was finished. More...
 
- Public Member Functions inherited from CObject
 CObject (void)
 Constructor. More...
 
 CObject (const CObject &src)
 Copy constructor. More...
 
virtual ~CObject (void)
 Destructor. More...
 
CObjectoperator= (const CObject &src) THROWS_NONE
 Assignment operator. More...
 
bool CanBeDeleted (void) const THROWS_NONE
 Check if object can be deleted. More...
 
bool IsAllocatedInPool (void) const THROWS_NONE
 Check if object is allocated in memory pool (not system heap) More...
 
bool Referenced (void) const THROWS_NONE
 Check if object is referenced. More...
 
bool ReferencedOnlyOnce (void) const THROWS_NONE
 Check if object is referenced only once. More...
 
void AddReference (void) const
 Add reference to object. More...
 
void RemoveReference (void) const
 Remove reference to object. More...
 
void ReleaseReference (void) const
 Remove reference without deleting object. More...
 
virtual void DoNotDeleteThisObject (void)
 Mark this object as not allocated in heap – do not delete this object. More...
 
virtual void DoDeleteThisObject (void)
 Mark this object as allocated in heap – object can be deleted. More...
 
void * operator new (size_t size)
 Define new operator for memory allocation. More...
 
void * operator new[] (size_t size)
 Define new[] operator for 'array' memory allocation. More...
 
void operator delete (void *ptr)
 Define delete operator for memory deallocation. More...
 
void operator delete[] (void *ptr)
 Define delete[] operator for memory deallocation. More...
 
void * operator new (size_t size, void *place)
 Define new operator. More...
 
void operator delete (void *ptr, void *place)
 Define delete operator. More...
 
void * operator new (size_t size, CObjectMemoryPool *place)
 Define new operator using memory pool. More...
 
void operator delete (void *ptr, CObjectMemoryPool *place)
 Define delete operator. More...
 
virtual void DebugDump (CDebugDumpContext ddc, unsigned int depth) const
 Define method for dumping debug information. More...
 
- Public Member Functions inherited from CDebugDumpable
 CDebugDumpable (void)
 
virtual ~CDebugDumpable (void)
 
void DebugDumpText (ostream &out, const string &bundle, unsigned int depth) const
 
void DebugDumpFormat (CDebugDumpFormatter &ddf, const string &bundle, unsigned int depth) const
 
void DumpToConsole (void) const
 

Static Public Member Functions

static CThreadPool_Impls_GetImplPointer (CThreadPool *pool)
 Convert pointer to CThreadPool object into pointer to CThreadPool_Impl object. More...
 
static void sx_SetTaskStatus (CThreadPool_Task *task, CThreadPool_Task::EStatus status)
 Call x_SetTaskStatus() for the given task. More...
 
static void sx_RequestToCancel (CThreadPool_Task *task)
 Call x_RequestToCancel() for the given task. More...
 
- Static Public Member Functions inherited from CObject
static NCBI_XNCBI_EXPORT void ThrowNullPointerException (void)
 Define method to throw null pointer exception. More...
 
static NCBI_XNCBI_EXPORT void ThrowNullPointerException (const type_info &type)
 
static EAllocFillMode GetAllocFillMode (void)
 
static void SetAllocFillMode (EAllocFillMode mode)
 
static void SetAllocFillMode (const string &value)
 Set mode from configuration parameter value. More...
 
- Static Public Member Functions inherited from CDebugDumpable
static void EnableDebugDump (bool on)
 

Private Types

typedef CSyncQueue< CRef< CThreadPool_Task >, CSyncQueue_multiset< CRef< CThreadPool_Task >, SThreadPool_TaskCompare > > TQueue
 Type of queue used for storing tasks. More...
 
typedef CSyncQueue< SExclusiveTaskInfoTExclusiveQueue
 Type of queue used for storing information about exclusive tasks. More...
 
typedef set< CThreadPool_ThreadImpl * > TThreadsList
 Type of list of all poolled threads. More...
 
typedef bool(CThreadPool_Impl::* TWaitPredicate) (void) const
 Type of some simple predicate. More...
 

Private Member Functions

 CThreadPool_Impl (const CThreadPool_Impl &)
 Prohibit copying and assigning. More...
 
CThreadPool_Imploperator= (const CThreadPool_Impl &)
 
unsigned int x_GetQueueSize (unsigned int queue_size)
 Transform size of queue given in constructor to the size passed to CSyncQueue constructor. More...
 
void x_Init (CThreadPool *pool_intf, CThreadPool_Controller *controller, CThread::TRunMode threads_mode)
 Initialization of all class member variables that can be initialized outside of constructor. More...
 
 ~CThreadPool_Impl (void)
 Destructor. Will be called from CRef. More...
 
void x_RemoveTaskFromQueue (const CThreadPool_Task *task)
 Delete task from the queue If task does not exist in queue then does nothing. More...
 
void x_CancelQueuedTasks (void)
 Cancel all tasks waiting in the queue. More...
 
void x_CancelExecutingTasks (void)
 Cancel all currently executing tasks. More...
 
bool x_NoNewTaskAllowed (void) const
 Check if addeding new tasks to the pool is prohibited. More...
 
bool x_CanAddImmediateTask (void) const
 Check if new task can be added to the pool when queueiing is disabled. More...
 
bool x_HasNoThreads (void) const
 Check if all threads in pool finished their work. More...
 
bool x_WaitForPredicate (TWaitPredicate wait_func, CThreadPool_Guard *pool_guard, CSemaphore *wait_sema, const CTimeSpan *timeout, const CStopWatch *timer)
 Wait for some predicate to be true. More...
 

Private Attributes

CThreadPoolm_Interface
 ThreadPool interface object attached to this implementation. More...
 
CRef< CThreadPool_Implm_SelfRef
 Reference to this pool to prevent its destroying earlier than we allow it to. More...
 
CTimeSpan m_DestroyTimeout
 Timeout to wait for all threads to finish before the ThreadPool interface object will be able to destroy. More...
 
TQueue m_Queue
 Queue for storing tasks. More...
 
CMutex m_MainPoolMutex
 Mutex for guarding all changes in the pool, its threads and controller. More...
 
CSemaphore m_RoomWait
 Semaphore for waiting for available threads to process task when queuing is disabled. More...
 
CRef< CThreadPool_Controllerm_Controller
 Controller managing count of threads in pool. More...
 
TThreadsList m_IdleThreads
 List of all idle threads. More...
 
TThreadsList m_WorkingThreads
 List of all threads currently executing some tasks. More...
 
CThread::TRunMode m_ThreadsMode
 Running mode of all threads. More...
 
CAtomicCounter m_ThreadsCount
 Total number of threads Introduced for more adequate and fast reflecting to threads starting and stopping events. More...
 
CAtomicCounter m_ExecutingTasks
 Number of tasks executing now Introduced for more adequate and fast reflecting to task executing start and finish events. More...
 
CAtomicCounter m_TotalTasks
 Total number of tasks acquired by pool Includes queued tasks and executing (but not exclusive!) tasks. More...
 
bool m_IsQueueAllowed
 Flag about working with special case: FALSE - queue_size == 0, TRUE - queue_size > 0. More...
 
atomic< boolm_Aborted
 If pool is already aborted or not. More...
 
CSemaphore m_AbortWait
 Semaphore for waiting for threads finishing in Abort() method. More...
 
atomic< boolm_Suspended
 If pool is suspended for exclusive task execution or not. More...
 
volatile TExclusiveFlags m_SuspendFlags
 Requested requirements for the exclusive execution environment. More...
 
volatile bool m_FlushRequested
 Flag indicating if flush of threads requested after adding exclusive task but before it is started its execution. More...
 
CRef< CThreadPool_ServiceThreadm_ServiceThread
 Thread for execution of exclusive tasks and passing of events to the controller. More...
 
TExclusiveQueue m_ExclusiveQueue
 Queue for information about exclusive tasks. More...
 

Additional Inherited Members

- Static Public Attributes inherited from CObject
static const TCount eCounterBitsCanBeDeleted = 1 << 0
 Define possible object states. More...
 
static const TCount eCounterBitsInPlainHeap = 1 << 1
 Heap signature was found. More...
 
static const TCount eCounterBitsPlaceMask
 Mask for 'in heap' state flags. More...
 
static const int eCounterStep = 1 << 2
 Skip over the "in heap" bits. More...
 
static const TCount eCounterValid = TCount(1) << (sizeof(TCount) * 8 - 2)
 Minimal value for valid objects (reference counter is zero) Must be a single bit value. More...
 
static const TCount eCounterStateMask
 Valid object, and object in heap. More...
 
- Protected Member Functions inherited from CObject
virtual void DeleteThis (void)
 Virtual method "deleting" this object. More...
 

Detailed Description

Real implementation of all ThreadPool functions.

Definition at line 58 of file thread_pool.cpp.

Member Typedef Documentation

◆ TExclusiveFlags

Definition at line 61 of file thread_pool.cpp.

◆ TExclusiveQueue

Type of queue used for storing information about exclusive tasks.

Definition at line 258 of file thread_pool.cpp.

◆ TQueue

Type of queue used for storing tasks.

Definition at line 256 of file thread_pool.cpp.

◆ TThreadsList

Type of list of all poolled threads.

Definition at line 260 of file thread_pool.cpp.

◆ TWaitPredicate

typedef bool(CThreadPool_Impl::* CThreadPool_Impl::TWaitPredicate) (void) const
private

Type of some simple predicate.

See also
x_WaitForPredicate

Definition at line 299 of file thread_pool.cpp.

Constructor & Destructor Documentation

◆ CThreadPool_Impl() [1/3]

CThreadPool_Impl::CThreadPool_Impl ( CThreadPool pool_intf,
unsigned int  queue_size,
unsigned int  max_threads,
unsigned int  min_threads,
CThread::TRunMode  threads_mode = CThread::fRunDefault 
)
inline

Constructor with default controller.

Parameters
pool_intfThreadPool interface object attached to this implementation
See also
CThreadPool::CThreadPool()

Definition at line 1419 of file thread_pool.cpp.

References x_Init().

◆ CThreadPool_Impl() [2/3]

CThreadPool_Impl::CThreadPool_Impl ( CThreadPool pool_intf,
unsigned int  queue_size,
CThreadPool_Controller controller,
CThread::TRunMode  threads_mode = CThread::fRunDefault 
)
inline

Constructor with explicitly given controller.

Parameters
pool_intfThreadPool interface object attached to this implementation
See also
CThreadPool::CThreadPool()

Definition at line 1434 of file thread_pool.cpp.

References x_Init().

◆ CThreadPool_Impl() [3/3]

CThreadPool_Impl::CThreadPool_Impl ( const CThreadPool_Impl )
private

Prohibit copying and assigning.

◆ ~CThreadPool_Impl()

CThreadPool_Impl::~CThreadPool_Impl ( void  )
private

Destructor. Will be called from CRef.

Definition at line 1468 of file thread_pool.cpp.

Member Function Documentation

◆ Abort()

void CThreadPool_Impl::Abort ( const CTimeSpan timeout)
inline

◆ AddTask()

void CThreadPool_Impl::AddTask ( CThreadPool_Task task,
const CTimeSpan timeout 
)
inline

◆ CallController()

void CThreadPool_Impl::CallController ( CThreadPool_Controller::EEvent  event)
inline

Call the CThreadPool_Controller::HandleEvent() method of the pool controller with the given event type.

If ThreadPool is already aborted and controller is reset then do nothing.

Definition at line 758 of file thread_pool.cpp.

References CThreadPool_Controller::eSuspend, CRef< C, Locker >::GetNCPointerOrNull(), CThreadPool_Controller::HandleEvent(), IsSuspended(), m_Aborted, and m_Controller.

Referenced by RequestSuspend(), ResumeWork(), and CThreadPool_ServiceThread::x_Idle().

◆ CallControllerOther()

void CThreadPool_Impl::CallControllerOther ( void  )
inline

◆ CancelTask()

void CThreadPool_Impl::CancelTask ( CThreadPool_Task task)

◆ CancelTasks()

void CThreadPool_Impl::CancelTasks ( TExclusiveFlags  tasks_group)
inline

◆ CanDoExclusiveTask()

bool CThreadPool_Impl::CanDoExclusiveTask ( void  ) const
inline

Check if it is already allowed to execute exclusive task.

Definition at line 878 of file thread_pool.cpp.

References CThreadPool::fFlushThreads, GetThreadsCount(), m_SuspendFlags, m_WorkingThreads, and set< Key, Compare >::size().

Referenced by CThreadPool_ServiceThread::Main().

◆ DestroyReference()

void CThreadPool_Impl::DestroyReference ( void  )
inline

Destroy reference to this object Method is called when CThreadPool object is destroyed which means that implementation can be destroyed too if there is no references to it left.

Definition at line 1472 of file thread_pool.cpp.

References Abort(), m_DestroyTimeout, m_Interface, m_SelfRef, m_ServiceThread, and NULL.

Referenced by CThreadPool::~CThreadPool().

◆ FinishThreads()

void CThreadPool_Impl::FinishThreads ( unsigned int  count)

Finish threads in pool Stop first all idle threads then stop busy threads without cancelation of currently executing tasks.

Parameters
countNumber of threads to finish

Definition at line 1518 of file thread_pool.cpp.

References count, m_IdleThreads, m_WorkingThreads, and REVERSE_ITERATE.

Referenced by Abort(), CThreadPool_Controller::EnsureLimits(), FlushThreads(), RequestSuspend(), and CThreadPool_Controller::SetThreadsCount().

◆ FlushThreads()

void CThreadPool_Impl::FlushThreads ( CThreadPool::EFlushType  flush_type)
inline

◆ GetDestroyTimeout()

const CTimeSpan & CThreadPool_Impl::GetDestroyTimeout ( void  ) const
inline

Get destroy timeout for the pool.

See also
CThreadPool::GetDestroyTimeout()

Definition at line 1493 of file thread_pool.cpp.

References m_DestroyTimeout.

Referenced by CThreadPool::GetDestroyTimeout().

◆ GetExecutingTasksCount()

unsigned int CThreadPool_Impl::GetExecutingTasksCount ( void  ) const
inline

Get the number of currently executing tasks.

Definition at line 738 of file thread_pool.cpp.

References CAtomicCounter::Get(), and m_ExecutingTasks.

Referenced by CThreadPool::GetExecutingTasksCount().

◆ GetMainPoolMutex()

CMutex & CThreadPool_Impl::GetMainPoolMutex ( void  )
inline

Get main pool mutex.

See also
CThreadPool::GetMainPoolMutex()

Definition at line 657 of file thread_pool.cpp.

References m_MainPoolMutex.

Referenced by CThreadPool::GetMainPoolMutex(), and CThreadPool_Guard::Guard().

◆ GetPoolInterface()

CThreadPool * CThreadPool_Impl::GetPoolInterface ( void  ) const
inline

Get pointer to ThreadPool interface object.

Definition at line 702 of file thread_pool.cpp.

References m_Interface.

Referenced by CThreadPool_Task::GetPool(), CThreadPool_Controller::GetPool(), and CThreadPool_ThreadImpl::GetPool().

◆ GetQueuedTasksCount()

unsigned int CThreadPool_Impl::GetQueuedTasksCount ( void  ) const
inline

Get the number of tasks currently waiting in queue.

Definition at line 732 of file thread_pool.cpp.

References CSyncQueue< Type, Container, Traits >::GetSize(), and m_Queue.

Referenced by Abort(), AddTask(), and CThreadPool::GetQueuedTasksCount().

◆ GetSafeSleepTime()

CTimeSpan CThreadPool_Impl::GetSafeSleepTime ( void  ) const
inline

Call the CThreadPool_Controller::GetSafeSleepTime() method of the pool controller.

If ThreadPool is already aborted and controller is reset then return time period of 1 second.

Definition at line 744 of file thread_pool.cpp.

References CRef< C, Locker >::GetNCPointerOrNull(), CThreadPool_Controller::GetSafeSleepTime(), m_Aborted, and m_Controller.

Referenced by CThreadPool_ServiceThread::x_Idle().

◆ GetThreadsCount()

unsigned int CThreadPool_Impl::GetThreadsCount ( void  ) const
inline

◆ IsAborted()

bool CThreadPool_Impl::IsAborted ( void  ) const
inline

Check if the pool is already aborted.

Definition at line 714 of file thread_pool.cpp.

References m_Aborted.

Referenced by CThreadPool_Controller::HandleEvent(), CThreadPool::IsAborted(), and CThreadPool_ServiceThread::Main().

◆ IsSuspended()

bool CThreadPool_Impl::IsSuspended ( void  ) const
inline

◆ LaunchThreads()

void CThreadPool_Impl::LaunchThreads ( unsigned int  count)

◆ operator=()

CThreadPool_Impl& CThreadPool_Impl::operator= ( const CThreadPool_Impl )
private

◆ RequestExclusiveExecution()

void CThreadPool_Impl::RequestExclusiveExecution ( CThreadPool_Task task,
TExclusiveFlags  flags 
)

◆ RequestSuspend()

void CThreadPool_Impl::RequestSuspend ( TExclusiveFlags  flags)
inline

◆ ResumeWork()

void CThreadPool_Impl::ResumeWork ( void  )
inline

Resume the pool operation after exclusive task execution.

Definition at line 909 of file thread_pool.cpp.

References CallController(), CThreadPool_Controller::eResume, ITERATE, m_IdleThreads, and m_Suspended.

Referenced by CThreadPool_ServiceThread::Main().

◆ s_GetImplPointer()

CThreadPool_Impl * CThreadPool_Impl::s_GetImplPointer ( CThreadPool pool)
inlinestatic

Convert pointer to CThreadPool object into pointer to CThreadPool_Impl object.

Can be done only here to avoid excessive friendship to CThreadPool class.

Definition at line 1397 of file thread_pool.cpp.

References CThreadPool::m_Impl.

Referenced by CThreadPool_Thread::CThreadPool_Thread(), and CThreadPool_Controller::GetMainPoolMutex().

◆ SetDestroyTimeout()

void CThreadPool_Impl::SetDestroyTimeout ( const CTimeSpan timeout)
inline

Set destroy timeout for the pool.

See also
CThreadPool::SetDestroyTimeout()

Definition at line 1487 of file thread_pool.cpp.

References m_DestroyTimeout.

Referenced by CThreadPool::SetDestroyTimeout().

◆ SetInterfaceStarted()

void CThreadPool_Impl::SetInterfaceStarted ( void  )
inline

Mark that initialization of the interface was finished.

Definition at line 708 of file thread_pool.cpp.

References CThread::fRunDetached, m_ServiceThread, and CThread::Run().

Referenced by CThreadPool::CThreadPool().

◆ SetThreadIdle()

bool CThreadPool_Impl::SetThreadIdle ( CThreadPool_ThreadImpl thread,
bool  is_idle 
)

◆ sx_RequestToCancel()

void CThreadPool_Impl::sx_RequestToCancel ( CThreadPool_Task task)
inlinestatic

Call x_RequestToCancel() for the given task.

Method introduced to avoid excessive friendship to CThreadPool_Task class.

See also
CThreadPool_Task::x_RequestToCancel()

Definition at line 696 of file thread_pool.cpp.

References CThreadPool_Task::x_RequestToCancel().

Referenced by CThreadPool_ThreadImpl::CancelCurrentTask(), CThreadPool_ThreadImpl::Main(), CThreadPool_ServiceThread::Main(), and CThreadPool_ServiceThread::RequestToFinish().

◆ sx_SetTaskStatus()

void CThreadPool_Impl::sx_SetTaskStatus ( CThreadPool_Task task,
CThreadPool_Task::EStatus  status 
)
inlinestatic

Call x_SetTaskStatus() for the given task.

Method introduced to avoid excessive friendship to CThreadPool_Task class.

See also
CThreadPool_Task::x_SetTaskStatus()

Definition at line 689 of file thread_pool.cpp.

References CThreadPool_Task::x_SetStatus().

Referenced by CThreadPool_ThreadImpl::Main(), CThreadPool_ServiceThread::Main(), and CThreadPool_ThreadImpl::x_TaskFinished().

◆ TaskFinished()

void CThreadPool_Impl::TaskFinished ( void  )
inline

Callback from thread when it has finished to execute task.

Definition at line 787 of file thread_pool.cpp.

References CAtomicCounter::Add(), CallControllerOther(), m_ExecutingTasks, m_IsQueueAllowed, m_RoomWait, m_TotalTasks, and CSemaphore::Post().

Referenced by CThreadPool_ThreadImpl::x_TaskFinished().

◆ TaskStarting()

void CThreadPool_Impl::TaskStarting ( void  )
inline

Callback from thread when it is starting to execute task.

Definition at line 778 of file thread_pool.cpp.

References CAtomicCounter::Add(), and m_ExecutingTasks.

Referenced by CThreadPool_ThreadImpl::Main().

◆ ThreadStateChanged()

void CThreadPool_Impl::ThreadStateChanged ( void  )
inline

◆ ThreadStopped()

void CThreadPool_Impl::ThreadStopped ( CThreadPool_ThreadImpl thread)
inline

Callback from working thread when it finished its Main() method.

Definition at line 827 of file thread_pool.cpp.

References CAtomicCounter::Add(), CallControllerOther(), set< Key, Compare >::erase(), m_IdleThreads, m_ThreadsCount, m_WorkingThreads, and ThreadStateChanged().

Referenced by CThreadPool_ThreadImpl::OnExit().

◆ TryGetExclusiveTask()

CThreadPool_Impl::SExclusiveTaskInfo CThreadPool_Impl::TryGetExclusiveTask ( void  )
inline

◆ TryGetNextTask()

CRef< CThreadPool_Task > CThreadPool_Impl::TryGetNextTask ( void  )
inline

Get next task from queue if there is one If the queue is empty then return NULL.

Definition at line 842 of file thread_pool.cpp.

References CSyncQueue< Type, Container, Traits >::GetSize(), IsSuspended(), m_Queue, and CSyncQueue< Type, Container, Traits >::Pop().

Referenced by CThreadPool_ThreadImpl::Main().

◆ x_CanAddImmediateTask()

bool CThreadPool_Impl::x_CanAddImmediateTask ( void  ) const
private

Check if new task can be added to the pool when queueiing is disabled.

Definition at line 1600 of file thread_pool.cpp.

References CAtomicCounter::Get(), CThreadPool_Controller::GetMaxThreads(), int, IsSuspended(), m_Controller, m_TotalTasks, and x_NoNewTaskAllowed().

Referenced by AddTask().

◆ x_CancelExecutingTasks()

void CThreadPool_Impl::x_CancelExecutingTasks ( void  )
private

Cancel all currently executing tasks.

Definition at line 1849 of file thread_pool.cpp.

References ITERATE, m_IdleThreads, and m_WorkingThreads.

Referenced by Abort(), CancelTasks(), and RequestSuspend().

◆ x_CancelQueuedTasks()

void CThreadPool_Impl::x_CancelQueuedTasks ( void  )
private

◆ x_GetQueueSize()

unsigned int CThreadPool_Impl::x_GetQueueSize ( unsigned int  queue_size)
inlineprivate

Transform size of queue given in constructor to the size passed to CSyncQueue constructor.

Method can be called only from constructor because it initializes value of m_IsQueueAllowed member variable.

Definition at line 1403 of file thread_pool.cpp.

References m_IsQueueAllowed.

◆ x_HasNoThreads()

bool CThreadPool_Impl::x_HasNoThreads ( void  ) const
private

◆ x_Init()

void CThreadPool_Impl::x_Init ( CThreadPool pool_intf,
CThreadPool_Controller controller,
CThread::TRunMode  threads_mode 
)
private

Initialization of all class member variables that can be initialized outside of constructor.

Parameters
pool_intfThreadPool interface object attached to this implementation
controllerController for the pool

Definition at line 1446 of file thread_pool.cpp.

References CThread::fRunAllowST, CThread::fRunDetached, m_Aborted, m_Controller, m_DestroyTimeout, m_ExecutingTasks, m_FlushRequested, m_Interface, m_SelfRef, m_ServiceThread, m_Suspended, m_ThreadsCount, m_ThreadsMode, m_TotalTasks, CAtomicCounter::Set(), and CThreadPool_Controller::x_AttachToPool().

Referenced by CThreadPool_Impl().

◆ x_NoNewTaskAllowed()

bool CThreadPool_Impl::x_NoNewTaskAllowed ( void  ) const
inlineprivate

Check if addeding new tasks to the pool is prohibited.

Definition at line 1592 of file thread_pool.cpp.

References CThreadPool::fDoNotAllowNewTasks, IsSuspended(), m_Aborted, and m_SuspendFlags.

Referenced by AddTask(), and x_CanAddImmediateTask().

◆ x_RemoveTaskFromQueue()

void CThreadPool_Impl::x_RemoveTaskFromQueue ( const CThreadPool_Task task)
inlineprivate

Delete task from the queue If task does not exist in queue then does nothing.

Definition at line 1759 of file thread_pool.cpp.

References CSyncQueue_AccessGuard< Type, Container, Traits >::Begin(), CSyncQueue_AccessGuard< Type, Container, Traits >::End(), CSyncQueue_AccessGuard< Type, Container, Traits >::Erase(), and m_Queue.

Referenced by CancelTask().

◆ x_WaitForPredicate()

bool CThreadPool_Impl::x_WaitForPredicate ( TWaitPredicate  wait_func,
CThreadPool_Guard pool_guard,
CSemaphore wait_sema,
const CTimeSpan timeout,
const CStopWatch timer 
)
private

Wait for some predicate to be true.

Parameters
wait_funcPredicate to wait for
pool_guardGuardian that locks main pool mutex at the time of method call and that have to be unlocked for the time of waiting
wait_semaSemaphore which will be posted when predicate become true
timeoutMaximum amount of time to wait
timerTimer for mesuring elapsed time. Method assumes that timer is started at the moment from which timeout should be calculated.

Definition at line 1623 of file thread_pool.cpp.

References done, CStopWatch::Elapsed(), eNegative, CTimeSpan::GetAsDouble(), CTimeSpan::GetSign(), CThreadPool_Guard::Guard(), CThreadPool_Guard::Release(), CSemaphore::TryWait(), and CSemaphore::Wait().

Referenced by Abort(), and AddTask().

Member Data Documentation

◆ m_Aborted

atomic<bool> CThreadPool_Impl::m_Aborted
private

◆ m_AbortWait

CSemaphore CThreadPool_Impl::m_AbortWait
private

Semaphore for waiting for threads finishing in Abort() method.

See also
Abort()

Definition at line 374 of file thread_pool.cpp.

Referenced by Abort(), and ThreadStateChanged().

◆ m_Controller

CRef<CThreadPool_Controller> CThreadPool_Impl::m_Controller
private

Controller managing count of threads in pool.

Definition at line 347 of file thread_pool.cpp.

Referenced by Abort(), CallController(), GetSafeSleepTime(), x_CanAddImmediateTask(), and x_Init().

◆ m_DestroyTimeout

CTimeSpan CThreadPool_Impl::m_DestroyTimeout
private

Timeout to wait for all threads to finish before the ThreadPool interface object will be able to destroy.

Definition at line 338 of file thread_pool.cpp.

Referenced by DestroyReference(), GetDestroyTimeout(), SetDestroyTimeout(), and x_Init().

◆ m_ExclusiveQueue

TExclusiveQueue CThreadPool_Impl::m_ExclusiveQueue
private

Queue for information about exclusive tasks.

Definition at line 392 of file thread_pool.cpp.

Referenced by Abort(), FlushThreads(), RequestExclusiveExecution(), and TryGetExclusiveTask().

◆ m_ExecutingTasks

CAtomicCounter CThreadPool_Impl::m_ExecutingTasks
private

Number of tasks executing now Introduced for more adequate and fast reflecting to task executing start and finish events.

Definition at line 361 of file thread_pool.cpp.

Referenced by GetExecutingTasksCount(), TaskFinished(), TaskStarting(), and x_Init().

◆ m_FlushRequested

volatile bool CThreadPool_Impl::m_FlushRequested
private

Flag indicating if flush of threads requested after adding exclusive task but before it is started its execution.

Definition at line 387 of file thread_pool.cpp.

Referenced by FlushThreads(), TryGetExclusiveTask(), and x_Init().

◆ m_IdleThreads

TThreadsList CThreadPool_Impl::m_IdleThreads
private

◆ m_Interface

CThreadPool* CThreadPool_Impl::m_Interface
private

ThreadPool interface object attached to this implementation.

Definition at line 332 of file thread_pool.cpp.

Referenced by CancelTask(), DestroyReference(), GetPoolInterface(), LaunchThreads(), and x_Init().

◆ m_IsQueueAllowed

bool CThreadPool_Impl::m_IsQueueAllowed
private

Flag about working with special case: FALSE - queue_size == 0, TRUE - queue_size > 0.

Definition at line 368 of file thread_pool.cpp.

Referenced by AddTask(), TaskFinished(), and x_GetQueueSize().

◆ m_MainPoolMutex

CMutex CThreadPool_Impl::m_MainPoolMutex
private

Mutex for guarding all changes in the pool, its threads and controller.

Definition at line 342 of file thread_pool.cpp.

Referenced by GetMainPoolMutex().

◆ m_Queue

TQueue CThreadPool_Impl::m_Queue
private

Queue for storing tasks.

Definition at line 340 of file thread_pool.cpp.

Referenced by AddTask(), GetQueuedTasksCount(), SetThreadIdle(), TryGetNextTask(), x_CancelQueuedTasks(), and x_RemoveTaskFromQueue().

◆ m_RoomWait

CSemaphore CThreadPool_Impl::m_RoomWait
private

Semaphore for waiting for available threads to process task when queuing is disabled.

Definition at line 345 of file thread_pool.cpp.

Referenced by AddTask(), and TaskFinished().

◆ m_SelfRef

CRef<CThreadPool_Impl> CThreadPool_Impl::m_SelfRef
private

Reference to this pool to prevent its destroying earlier than we allow it to.

Definition at line 335 of file thread_pool.cpp.

Referenced by DestroyReference(), and x_Init().

◆ m_ServiceThread

CRef<CThreadPool_ServiceThread> CThreadPool_Impl::m_ServiceThread
private

Thread for execution of exclusive tasks and passing of events to the controller.

Definition at line 390 of file thread_pool.cpp.

Referenced by Abort(), CallControllerOther(), DestroyReference(), RequestExclusiveExecution(), SetInterfaceStarted(), ThreadStateChanged(), x_HasNoThreads(), and x_Init().

◆ m_Suspended

atomic<bool> CThreadPool_Impl::m_Suspended
private

If pool is suspended for exclusive task execution or not.

Thread Checker can complain that access to this variable everywhere is not guarded by some mutex. But it's okay because special care is taken to make any race a matter of timing - suspend will happen properly in any case. Also everything is written with the assumption that there's no other threads (besides this very thread pool) that could call any methods here.

Definition at line 382 of file thread_pool.cpp.

Referenced by IsSuspended(), RequestSuspend(), ResumeWork(), and x_Init().

◆ m_SuspendFlags

volatile TExclusiveFlags CThreadPool_Impl::m_SuspendFlags
private

Requested requirements for the exclusive execution environment.

Definition at line 384 of file thread_pool.cpp.

Referenced by AddTask(), CanDoExclusiveTask(), RequestSuspend(), SetThreadIdle(), ThreadStateChanged(), and x_NoNewTaskAllowed().

◆ m_ThreadsCount

CAtomicCounter CThreadPool_Impl::m_ThreadsCount
private

Total number of threads Introduced for more adequate and fast reflecting to threads starting and stopping events.

Definition at line 357 of file thread_pool.cpp.

Referenced by GetThreadsCount(), LaunchThreads(), ThreadStopped(), and x_Init().

◆ m_ThreadsMode

CThread::TRunMode CThreadPool_Impl::m_ThreadsMode
private

Running mode of all threads.

Definition at line 353 of file thread_pool.cpp.

Referenced by LaunchThreads(), and x_Init().

◆ m_TotalTasks

CAtomicCounter CThreadPool_Impl::m_TotalTasks
private

Total number of tasks acquired by pool Includes queued tasks and executing (but not exclusive!) tasks.

Introduced for maintaining atomicity of this number changing

Definition at line 365 of file thread_pool.cpp.

Referenced by AddTask(), TaskFinished(), TryGetExclusiveTask(), x_CanAddImmediateTask(), and x_Init().

◆ m_WorkingThreads

TThreadsList CThreadPool_Impl::m_WorkingThreads
private

List of all threads currently executing some tasks.

Definition at line 351 of file thread_pool.cpp.

Referenced by CanDoExclusiveTask(), FinishThreads(), SetThreadIdle(), ThreadStateChanged(), ThreadStopped(), x_CancelExecutingTasks(), and x_HasNoThreads().


The documentation for this class was generated from the following file:
Modified on Fri Sep 20 14:58:29 2024 by modify_doxy.py rev. 669887