KPIM::ThreadWeaver::Weaver Class Reference
A weaver is the manager of worker threads (Thread objects) to which it assigns jobs from it's queue. More...
#include <weaver.h>
Inheritance diagram for KPIM::ThreadWeaver::Weaver:

Signals | |
void | finished () |
void | suspended () |
void | jobDone (Job *) |
void | threadCreated (Thread *) |
void | threadDestroyed (Thread *) |
void | threadBusy (Thread *) |
void | threadSuspended (Thread *) |
Public Methods | |
Weaver (QObject *parent=0, const char *name=0, int inventoryMin=4, int inventoryMax=32) | |
virtual | ~Weaver () |
virtual void | enqueue (Job *) |
void | enqueue (QPtrList< Job > jobs) |
virtual bool | dequeue (Job *) |
virtual void | dequeue () |
virtual void | finish () |
virtual void | suspend (bool state) |
bool | isEmpty () const |
bool | isIdle () const |
int | queueLength () |
virtual Job * | applyForWork (Thread *thread, Job *previous) |
void | lock () |
void | unlock () |
void | post (Event::Action, Thread *=0, Job *=0) |
int | threads () const |
Protected Methods | |
void | assignJobs () |
bool | event (QEvent *) |
Protected Attributes | |
QPtrList< Thread > | m_inventory |
QPtrList< Job > | m_assignments |
int | m_active |
int | m_inventoryMin |
int | m_inventoryMax |
QWaitCondition | m_jobAvailable |
QWaitCondition | m_jobFinished |
bool | m_shuttingDown |
bool | m_running |
bool | m_suspend |
Detailed Description
A weaver is the manager of worker threads (Thread objects) to which it assigns jobs from it's queue.
Definition at line 294 of file weaver.h.
Member Function Documentation
|
Add a job to be executed.
Definition at line 327 of file weaver.cpp. References QPtrList< Job >::append(), assignJobs(), lock(), m_assignments, m_running, and unlock(). |
|
Enqueue all jobs in the given list. This is an atomic operation, no jobs will start before all jobs in the list are enqueued. If you need a couple of jobs done and want to receive the finished () signal afterwards, use this method to queue them. Otherwise, when enqueueing your jobs individually, there is a chance that you receive more than one finished signal. Definition at line 339 of file weaver.cpp. References QPtrList< Job >::append(), assignJobs(), QPtrList::first(), lock(), m_assignments, QPtrList::next(), and unlock(). |
|
Remove a job from the queue. If the job qas queued but not started so far, it is simple removed from the queue. For now, it is unsupported to dequeue a job once its execution has started. For that case, you will have to provide a method to interrupt your job's execution (and receive the done signal). Returns true if the job has been dequeued, false if the job has already been started or is not found in the queue. Definition at line 353 of file weaver.cpp. References m_assignments, and QPtrList< Job >::remove(). |
|
Remove all queued jobs. Please note that this will not kill the threads, therefore all jobs that are being processed will be continued. Definition at line 359 of file weaver.cpp. References QPtrList< Job >::clear(), and m_assignments. |
|
Finish all queued operations, then return. This method is used in imperative programs that cannot react on events to have the controlling (main) thread wait wait for the jobs to finish. Warning: This will suspend your thread! Warning: If your jobs enter for example an infinite loop, this will never return! Definition at line 537 of file weaver.cpp. References isIdle(), m_jobFinished, and QWaitCondition::wait(). |
|
Suspend job execution if state = true, otherwise resume job execution if it was suspended. When suspending, all threads are allowed to finish the currently assigned job but will not receive a new assignment. When all threads are done processing the assigned job, the signal suspended will() be emitted. If you call suspend (true) and there are no jobs left to be done, you will immidiately receive the suspended() signal. Definition at line 365 of file weaver.cpp. References assignJobs(), isEmpty(), lock(), m_active, m_suspend, post(), and unlock(). |
|
Is the queue empty?
Definition at line 451 of file weaver.cpp. References QPtrList< Job >::count(), and m_assignments. Referenced by applyForWork(), isIdle(), and suspend(). |
|
Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads (m_active is zero).
Definition at line 531 of file weaver.cpp. References isEmpty(), and m_active. Referenced by finish(). |
|
Returns the number of pending jobs.
Definition at line 525 of file weaver.cpp. References QPtrList< Job >::count(), and m_assignments. Referenced by applyForWork(). |
|
Assign a job to the calling thread. This is supposed to be called from the Thread objects in the inventory. Returns 0 if the weaver is shutting down, telling the calling thread to finish and exit. If no jobs are available and shut down is not in progress, the calling thread is suspended until either condition is met. In previous, threads give the job they have completed. If this is the first job, previous is zero. Definition at line 457 of file weaver.cpp. References QPtrList< Job >::count(), QPtrList< Job >::getFirst(), isEmpty(), lock(), m_active, m_assignments, m_jobAvailable, m_jobFinished, m_running, m_shuttingDown, m_suspend, post(), queueLength(), QPtrList< Job >::removeFirst(), suspended(), unlock(), QWaitCondition::wait(), and QWaitCondition::wakeOne(). Referenced by KPIM::ThreadWeaver::Thread::run(). |
|
Lock the mutex for this weaver. The threads in the inventory need to lock the weaver's mutex to synchronize the job management. Definition at line 306 of file weaver.cpp. References QMutex::lock(), and QMutex::locked(). Referenced by applyForWork(), enqueue(), and suspend(). |
|
Unlock. See lock(). Definition at line 313 of file weaver.cpp. References QMutex::locked(), and QMutex::unlock(). Referenced by applyForWork(), enqueue(), and suspend(). |
|
Post an event that is handled by this object, but in the main (GUI) thread. Different threads may use this method to communicate with the main thread. thread and job mark the objects associated with this event. Definition at line 445 of file weaver.cpp. References QApplication::postEvent(). Referenced by applyForWork(), KPIM::ThreadWeaver::Thread::post(), and suspend(). |
|
Returns the current number of threads in the inventory.
Definition at line 321 of file weaver.cpp. References QPtrList< Thread >::count(), and m_inventory. |
|
This signal is emitted when the Weaver has finished ALL currently queued jobs. If a number of jobs is enqueued sequentially, this signal might be emitted a couple of times (what happens is that all already queued jobs have been processed while you still add new ones). This is not a bug, but the intended behaviour. Referenced by event(). |
|
Thread queueing has been suspended. When suspend is called with state = true, all threads are allowed to finish their job. When the last thread finished, this signal is emitted. Referenced by applyForWork(), and event(). |
|
This signal is emitted when a job is done. It is up to the programmer if this signal or the done signal of the job is more handy. Referenced by event(). |
|
Schedule enqueued jobs to be executed by idle threads. This will try to distribute as many jobs as possible to all idle threads. Definition at line 387 of file weaver.cpp. References m_jobAvailable, and QWaitCondition::wakeAll(). |
|
Check incoming events for user defined ones. The threads use user defined events to communicate with the Weaver. Reimplemented from QObject. Definition at line 392 of file weaver.cpp. References KPIM::ThreadWeaver::Event::action(), QObject::event(), event(), finished(), KPIM::ThreadWeaver::Event::job(), jobDone(), m_shuttingDown, KPIM::ThreadWeaver::Job::processEvent(), suspended(), KPIM::ThreadWeaver::Event::thread(), and QEvent::type(). Referenced by event(). |
Member Data Documentation
|
The thread inventory.
Definition at line 413 of file weaver.h. Referenced by threads(). |
|
The job queue.
Definition at line 415 of file weaver.h. Referenced by applyForWork(), dequeue(), enqueue(), isEmpty(), and queueLength(). |
|
The number of jobs that are assigned to the worker threads, but not finished.
Definition at line 418 of file weaver.h. Referenced by applyForWork(), isIdle(), and suspend(). |
|
Stored setting.
|
|
Stored setting .
|
|
Wait condition all idle or done threads wait for.
Definition at line 424 of file weaver.h. Referenced by applyForWork(), and assignJobs(). |
|
Wait for a job to finish.
Definition at line 426 of file weaver.h. Referenced by applyForWork(), and finish(). |
|
Indicates if the weaver is shutting down and exiting it's threads.
Definition at line 429 of file weaver.h. Referenced by applyForWork(), and event(). |
|
m_running is set to true when a job is enqueued and set to false when the job finishes that was the last in the queue. E.g., this will flip from false to true to false when you continuously enqueue one single job. Definition at line 434 of file weaver.h. Referenced by applyForWork(), and enqueue(). |
|
If m_suspend is true, no new jobs will be assigned to threads. Jobs may be queued, but will not be processed until suspend (false) is called. Definition at line 439 of file weaver.h. Referenced by applyForWork(), and suspend(). |
The documentation for this class was generated from the following files: