NCBI C++ ToolKit
|
Search Toolkit Book for CThreadPool_Impl
Real implementation of all ThreadPool functions. More...
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< Uint8 > | TCounter |
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... | |
CThreadPool * | GetPoolInterface (void) const |
Get pointer to ThreadPool interface object. More... | |
void | SetDestroyTimeout (const CTimeSpan &timeout) |
Set destroy timeout for the pool. More... | |
const CTimeSpan & | GetDestroyTimeout (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... | |
CMutex & | GetMainPoolMutex (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_Task > | TryGetNextTask (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... | |
CObject & | operator= (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_Impl * | s_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< SExclusiveTaskInfo > | TExclusiveQueue |
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_Impl & | operator= (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 | |
CThreadPool * | m_Interface |
ThreadPool interface object attached to this implementation. More... | |
CRef< CThreadPool_Impl > | m_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_Controller > | m_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< bool > | m_Aborted |
If pool is already aborted or not. More... | |
CSemaphore | m_AbortWait |
Semaphore for waiting for threads finishing in Abort() method. More... | |
atomic< bool > | m_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_ServiceThread > | m_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... | |
Real implementation of all ThreadPool functions.
Definition at line 58 of file thread_pool.cpp.
Definition at line 61 of file thread_pool.cpp.
|
private |
Type of queue used for storing information about exclusive tasks.
Definition at line 258 of file thread_pool.cpp.
|
private |
Type of queue used for storing tasks.
Definition at line 256 of file thread_pool.cpp.
|
private |
Type of list of all poolled threads.
Definition at line 260 of file thread_pool.cpp.
Type of some simple predicate.
Definition at line 299 of file thread_pool.cpp.
|
inline |
Constructor with default controller.
pool_intf | ThreadPool interface object attached to this implementation |
Definition at line 1419 of file thread_pool.cpp.
References x_Init().
|
inline |
Constructor with explicitly given controller.
pool_intf | ThreadPool interface object attached to this implementation |
Definition at line 1434 of file thread_pool.cpp.
References x_Init().
|
private |
Prohibit copying and assigning.
|
private |
Destructor. Will be called from CRef.
Definition at line 1468 of file thread_pool.cpp.
Abort the pool operation.
Definition at line 1916 of file thread_pool.cpp.
References CTimeSpan::AsSmartString(), CSyncQueue_AccessGuard< Type, Container, Traits >::Begin(), CSyncQueue< Type, Container, Traits >::Clear(), Critical(), CSyncQueue_AccessGuard< Type, Container, Traits >::End(), ERR_POST_X, CStopWatch::eStart, FinishThreads(), GetQueuedTasksCount(), CSyncQueue< Type, Container, Traits >::GetSize(), GetThreadsCount(), m_Aborted, m_AbortWait, m_Controller, m_ExclusiveQueue, m_ServiceThread, CRef< C, Locker >::NotNull(), CSemaphore::Post(), CThreadPool_ServiceThread::RequestToFinish(), Warning(), x_CancelExecutingTasks(), x_CancelQueuedTasks(), CThreadPool_Controller::x_DetachFromPool(), x_HasNoThreads(), and x_WaitForPredicate().
Referenced by CThreadPool::Abort(), and DestroyReference().
|
inline |
Add task to the pool.
Definition at line 1669 of file thread_pool.cpp.
References _ASSERT, CAtomicCounter::Add(), CallControllerOther(), check_flags(), count, CThreadPool_Task::eIdle, CStopWatch::Elapsed(), CThreadPool_Task::eQueued, CStopWatch::eStart, CThreadPool::fCancelQueuedTasks, CThreadPool::fDoNotAllowNewTasks, CTimeSpan::GetAsDouble(), GetQueuedTasksCount(), CSyncQueue< Type, Container, Traits >::GetSize(), GetThreadsCount(), CThreadPool_Guard::Guard(), int, IsSuspended(), ITERATE, LaunchThreads(), m_Aborted, m_IdleThreads, m_IsQueueAllowed, m_Queue, m_RoomWait, m_SuspendFlags, m_TotalTasks, NCBI_THROW, CSyncQueue< Type, Container, Traits >::Push(), Ref(), ThrowAddProhibited(), x_CanAddImmediateTask(), x_CancelQueuedTasks(), x_NoNewTaskAllowed(), CThreadPool_Task::x_ResetOwner(), CThreadPool_Task::x_SetOwner(), CThreadPool_Task::x_SetStatus(), and x_WaitForPredicate().
Referenced by CThreadPool::AddTask().
|
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().
|
inline |
Schedule running of CThreadPool_Controller::HandleEvent() with eOther event type.
Definition at line 769 of file thread_pool.cpp.
References m_ServiceThread, and CThreadPool_ServiceThread::NeedCallController().
Referenced by AddTask(), CancelTask(), CancelTasks(), LaunchThreads(), TaskFinished(), and ThreadStopped().
void CThreadPool_Impl::CancelTask | ( | CThreadPool_Task * | task | ) |
Request to cancel the task.
Definition at line 1799 of file thread_pool.cpp.
References _ASSERT, CallControllerOther(), CThreadPool_Task::eIdle, CThreadPool_Task::GetPool(), CThreadPool_Task::GetStatus(), CThreadPool_Task::IsFinished(), m_Interface, NCBI_THROW, x_RemoveTaskFromQueue(), and CThreadPool_Task::x_RequestToCancel().
Referenced by CThreadPool::CancelTask(), and CThreadPool_Task::RequestToCancel().
|
inline |
Cancel the selected groups of tasks in the pool.
Definition at line 1831 of file thread_pool.cpp.
References _ASSERT, CallControllerOther(), CThreadPool::fCancelExecutingTasks, CThreadPool::fCancelQueuedTasks, x_CancelExecutingTasks(), and x_CancelQueuedTasks().
Referenced by CThreadPool::CancelTasks().
|
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().
|
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().
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.
count | Number 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().
|
inline |
Finish all current threads and replace them with new ones.
Definition at line 1880 of file thread_pool.cpp.
References CThreadPool::eStartImmediately, CThreadPool::eWaitToFinish, CThreadPool::fFlushThreads, FinishThreads(), CSyncQueue< Type, Container, Traits >::GetSize(), GetThreadsCount(), IsSuspended(), m_Aborted, m_ExclusiveQueue, m_FlushRequested, NCBI_THROW, and RequestExclusiveExecution().
Referenced by CThreadPool::FlushThreads().
Get destroy timeout for the pool.
Definition at line 1493 of file thread_pool.cpp.
References m_DestroyTimeout.
Referenced by CThreadPool::GetDestroyTimeout().
|
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().
|
inline |
Get main pool mutex.
Definition at line 657 of file thread_pool.cpp.
References m_MainPoolMutex.
Referenced by CThreadPool::GetMainPoolMutex(), and CThreadPool_Guard::Guard().
|
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().
|
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().
|
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().
|
inline |
Get number of threads running in the pool.
Definition at line 726 of file thread_pool.cpp.
References CAtomicCounter::Get(), and m_ThreadsCount.
Referenced by Abort(), AddTask(), CanDoExclusiveTask(), CThreadPool_Controller::EnsureLimits(), FlushThreads(), CThreadPool::GetThreadsCount(), CThreadPool_Controller::SetThreadsCount(), and ThreadStateChanged().
|
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().
|
inline |
Check if the pool is suspended for exclusive execution.
Definition at line 720 of file thread_pool.cpp.
References m_Suspended.
Referenced by AddTask(), CallController(), FlushThreads(), CThreadPool_Controller::HandleEvent(), SetThreadIdle(), ThreadStateChanged(), TryGetNextTask(), x_CanAddImmediateTask(), and x_NoNewTaskAllowed().
void CThreadPool_Impl::LaunchThreads | ( | unsigned int | count | ) |
Launch new threads in pool.
count | Number of threads to launch |
Definition at line 1499 of file thread_pool.cpp.
References CAtomicCounter::Add(), CallControllerOther(), count, CThreadPool::CreateThread(), i, set< Key, Compare >::insert(), m_IdleThreads, m_Interface, m_ThreadsCount, m_ThreadsMode, and CThreadPool_ThreadImpl::s_GetImplPointer().
Referenced by AddTask(), CThreadPool_Controller::EnsureLimits(), and CThreadPool_Controller::SetThreadsCount().
|
private |
void CThreadPool_Impl::RequestExclusiveExecution | ( | CThreadPool_Task * | task, |
TExclusiveFlags | flags | ||
) |
Add the task for exclusive execution in the pool.
Definition at line 1774 of file thread_pool.cpp.
References _ASSERT, CThreadPool_Task::eQueued, flags, m_Aborted, m_ExclusiveQueue, m_ServiceThread, NCBI_THROW, CSyncQueue< Type, Container, Traits >::Push(), Ref(), CThreadPool_ServiceThread::WakeUp(), CThreadPool_Task::x_SetOwner(), and CThreadPool_Task::x_SetStatus().
Referenced by FlushThreads(), and CThreadPool::RequestExclusiveExecution().
|
inline |
Request suspension of the pool.
flags | Parameters for necessary exclusive execution environment |
Definition at line 890 of file thread_pool.cpp.
References CallController(), CThreadPool_Controller::eSuspend, CThreadPool::fCancelExecutingTasks, CThreadPool::fCancelQueuedTasks, CThreadPool::fFlushThreads, FinishThreads(), flags, m_IdleThreads, m_Suspended, m_SuspendFlags, set< Key, Compare >::size(), x_CancelExecutingTasks(), and x_CancelQueuedTasks().
Referenced by CThreadPool_ServiceThread::Main().
|
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().
|
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().
Set destroy timeout for the pool.
Definition at line 1487 of file thread_pool.cpp.
References m_DestroyTimeout.
Referenced by CThreadPool::SetDestroyTimeout().
|
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().
bool CThreadPool_Impl::SetThreadIdle | ( | CThreadPool_ThreadImpl * | thread, |
bool | is_idle | ||
) |
Mark thread as idle or non-idle.
thread | Thread to mark |
is_idle | If thread should be marked as idle or not |
Definition at line 1555 of file thread_pool.cpp.
References set< Key, Compare >::end(), set< Key, Compare >::erase(), CThreadPool::fFlushThreads, set< Key, Compare >::find(), CSyncQueue< Type, Container, Traits >::GetSize(), set< Key, Compare >::insert(), IsSuspended(), m_IdleThreads, m_Queue, m_SuspendFlags, m_WorkingThreads, CThreadPool_ThreadImpl::RequestToFinish(), ThreadStateChanged(), and CThreadPool_ThreadImpl::WakeUp().
Referenced by CThreadPool_ThreadImpl::x_SetIdleState().
|
inlinestatic |
Call x_RequestToCancel() for the given task.
Method introduced to avoid excessive friendship to CThreadPool_Task class.
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().
|
inlinestatic |
Call x_SetTaskStatus() for the given task.
Method introduced to avoid excessive friendship to CThreadPool_Task class.
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().
|
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().
|
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().
|
inline |
Callback when some thread changed its idleness or finished (including service thread)
Definition at line 798 of file thread_pool.cpp.
References CThreadPool::fFlushThreads, GetThreadsCount(), IsSuspended(), m_Aborted, m_AbortWait, m_ServiceThread, m_SuspendFlags, m_WorkingThreads, CSemaphore::Post(), set< Key, Compare >::size(), CThreadPool_ServiceThread::WakeUp(), and x_HasNoThreads().
Referenced by CThreadPool_ServiceThread::Main(), SetThreadIdle(), and ThreadStopped().
|
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().
|
inline |
Get the next exclusive task to execute.
Definition at line 857 of file thread_pool.cpp.
References CSyncQueue_AccessGuard< Type, Container, Traits >::Begin(), CThreadPool::fExecuteQueuedTasks, CThreadPool::fFlushThreads, CAtomicCounter::Get(), CSyncQueue< Type, Container, Traits >::GetSize(), info, m_ExclusiveQueue, m_FlushRequested, m_TotalTasks, and CSyncQueue< Type, Container, Traits >::Pop().
Referenced by CThreadPool_ServiceThread::Main().
|
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().
|
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().
|
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().
|
private |
Cancel all tasks waiting in the queue.
Definition at line 1866 of file thread_pool.cpp.
References CSyncQueue_AccessGuard< Type, Container, Traits >::Begin(), CSyncQueue< Type, Container, Traits >::Clear(), CSyncQueue_AccessGuard< Type, Container, Traits >::End(), and m_Queue.
Referenced by Abort(), AddTask(), CancelTasks(), and RequestSuspend().
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.
|
private |
Check if all threads in pool finished their work.
Definition at line 1615 of file thread_pool.cpp.
References CRef< C, Locker >::GetNCPointerOrNull(), CThreadPool_ServiceThread::IsFinished(), m_IdleThreads, m_ServiceThread, m_WorkingThreads, and set< Key, Compare >::size().
Referenced by Abort(), and ThreadStateChanged().
|
private |
Initialization of all class member variables that can be initialized outside of constructor.
pool_intf | ThreadPool interface object attached to this implementation |
controller | Controller 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().
|
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().
|
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().
|
private |
Wait for some predicate to be true.
wait_func | Predicate to wait for |
pool_guard | Guardian that locks main pool mutex at the time of method call and that have to be unlocked for the time of waiting |
wait_sema | Semaphore which will be posted when predicate become true |
timeout | Maximum amount of time to wait |
timer | Timer 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().
|
private |
If pool is already aborted or not.
Definition at line 370 of file thread_pool.cpp.
Referenced by Abort(), AddTask(), CallController(), FlushThreads(), GetSafeSleepTime(), IsAborted(), RequestExclusiveExecution(), ThreadStateChanged(), x_Init(), and x_NoNewTaskAllowed().
|
private |
Semaphore for waiting for threads finishing in Abort() method.
Definition at line 374 of file thread_pool.cpp.
Referenced by Abort(), and ThreadStateChanged().
|
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().
|
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().
|
private |
Queue for information about exclusive tasks.
Definition at line 392 of file thread_pool.cpp.
Referenced by Abort(), FlushThreads(), RequestExclusiveExecution(), and TryGetExclusiveTask().
|
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().
|
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().
|
private |
List of all idle threads.
Definition at line 349 of file thread_pool.cpp.
Referenced by AddTask(), FinishThreads(), LaunchThreads(), RequestSuspend(), ResumeWork(), SetThreadIdle(), ThreadStopped(), x_CancelExecutingTasks(), and x_HasNoThreads().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
private |
Running mode of all threads.
Definition at line 353 of file thread_pool.cpp.
Referenced by LaunchThreads(), and x_Init().
|
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().
|
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().