• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

ThreadWeaver

  • ThreadWeaver
  • WeaverImpl
Signals | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ThreadWeaver::WeaverImpl Class Reference

#include <WeaverImpl.h>

Inheritance diagram for ThreadWeaver::WeaverImpl:
Inheritance graph
[legend]

Signals

void asyncThreadSuspended (ThreadWeaver::Thread *)
 
void threadBusy (ThreadWeaver::Thread *, ThreadWeaver::Job *j)
 
void threadExited (ThreadWeaver::Thread *)
 
void threadStarted (ThreadWeaver::Thread *)
 
void threadSuspended (ThreadWeaver::Thread *)
 
- Signals inherited from ThreadWeaver::WeaverInterface
void finished ()
 
void jobDone (ThreadWeaver::Job *)
 
void stateChanged (ThreadWeaver::State *)
 
void suspended ()
 

Public Member Functions

 WeaverImpl (QObject *parent=0)
 
virtual ~WeaverImpl ()
 
int activeThreadCount ()
 
virtual Job * applyForWork (Thread *thread, Job *previous)
 
void assignJobs ()
 
void blockThreadUntilJobsAreBeingAssigned (Thread *th)
 
int currentNumberOfThreads () const
 
void decActiveThreadCount ()
 
virtual bool dequeue (Job *)
 
virtual void dequeue ()
 
void dumpJobs ()
 
virtual void enqueue (Job *)
 
virtual void finish ()
 
void incActiveThreadCount ()
 
bool isEmpty () const
 
bool isIdle () const
 
int maximumNumberOfThreads () const
 
int queueLength () const
 
void registerObserver (WeaverObserver *)
 
void requestAbort ()
 
virtual void resume ()
 
void setMaximumNumberOfThreads (int cap)
 
void setState (StateId)
 
const State & state () const
 
virtual void suspend ()
 
Job * takeFirstAvailableJob (Job *previous)
 
void waitForAvailableJob (Thread *th)
 
- Public Member Functions inherited from ThreadWeaver::WeaverInterface
 WeaverInterface (QObject *parent=0)
 
virtual ~WeaverInterface ()
 

Protected Member Functions

void adjustActiveThreadCount (int diff)
 
void adjustInventory (int noOfNewJobs)
 
virtual Thread * createThread ()
 

Protected Attributes

int m_active
 
QList< Job * > m_assignments
 
QList< Thread * > m_inventory
 
int m_inventoryMax
 
QWaitCondition m_jobAvailable
 
QWaitCondition m_jobFinished
 

Detailed Description

A WeaverImpl is the manager of worker threads (Thread objects) to which it assigns jobs from its queue.

It extends the API of WeaverInterface to provide additional methods needed by the Thread objects.

Definition at line 51 of file WeaverImpl.h.

Constructor & Destructor Documentation

WeaverImpl::WeaverImpl ( QObject *  parent = 0)
explicit

Construct a WeaverImpl object.

Definition at line 52 of file WeaverImpl.cpp.

WeaverImpl::~WeaverImpl ( )
virtual

Destruct a WeaverImpl object.

Definition at line 78 of file WeaverImpl.cpp.

Member Function Documentation

int WeaverImpl::activeThreadCount ( )

Returns the number of active threads.

Threads are active if they process a job.

Definition at line 327 of file WeaverImpl.cpp.

void WeaverImpl::adjustActiveThreadCount ( int  diff)
protected

Adjust active thread count.

This is a helper function for incActiveThreadCount and decActiveThreadCount.

Definition at line 313 of file WeaverImpl.cpp.

void WeaverImpl::adjustInventory ( int  noOfNewJobs)
protected

Adjust the inventory size.

This method creates threads on demand. Threads in the inventory are not created upon construction of the WeaverImpl object, but when jobs are queued. This avoids costly delays on the application startup time. Threads are created when the inventory size is under inventoryMin and new jobs are queued.

Definition at line 205 of file WeaverImpl.cpp.

Job * WeaverImpl::applyForWork ( Thread *  thread,
Job *  previous 
)
virtual

Assign a job to the calling thread.

This is supposed to be called from the Thread objects in the inventory. Do not call this method from your code.

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 354 of file WeaverImpl.cpp.

void WeaverImpl::assignJobs ( )

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 288 of file WeaverImpl.cpp.

void ThreadWeaver::WeaverImpl::asyncThreadSuspended ( ThreadWeaver::Thread *  )
signal
void WeaverImpl::blockThreadUntilJobsAreBeingAssigned ( Thread *  th)

Blocks the calling thread until some actor calls assignJobs.

Definition at line 364 of file WeaverImpl.cpp.

Thread * WeaverImpl::createThread ( )
protectedvirtual

Factory method to create the threads.

Overload in adapted Weaver implementations.

Definition at line 233 of file WeaverImpl.cpp.

int WeaverImpl::currentNumberOfThreads ( ) const
virtual

Returns the current number of threads in the inventory.

Implements ThreadWeaver::WeaverInterface.

Definition at line 162 of file WeaverImpl.cpp.

void WeaverImpl::decActiveThreadCount ( )

Decrement the count of active threads.

Definition at line 305 of file WeaverImpl.cpp.

bool WeaverImpl::dequeue ( Job *  )
virtual

Remove a job from the queue.

If the job was queued but not started so far, it is simply 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.

Implements ThreadWeaver::WeaverInterface.

Definition at line 238 of file WeaverImpl.cpp.

void WeaverImpl::dequeue ( )
virtual

Remove all queued jobs.

Please note that this will not kill the threads, therefore all jobs that are being processed will be continued.

Implements ThreadWeaver::WeaverInterface.

Definition at line 265 of file WeaverImpl.cpp.

void WeaverImpl::dumpJobs ( )

Dump the current jobs to the console.

Not part of the API.

Definition at line 414 of file WeaverImpl.cpp.

void WeaverImpl::enqueue ( Job *  )
virtual

Add a job to be executed.

It depends on the state if execution of the job will be attempted immediately. In suspended state, jobs can be added to the queue, but the threads remain suspended. In WorkongHard state, an idle thread may immediately execute the job, or it might be queued if all threads are busy.

Implements ThreadWeaver::WeaverInterface.

Definition at line 182 of file WeaverImpl.cpp.

void WeaverImpl::finish ( )
virtual

Finish all queued operations, then return.

This method is used in imperative (not event driven) programs that cannot react on events to have the controlling (main) thread wait wait for the jobs to finish. The call will block the calling thread and return when all queued jobs have been processed.

Warning: This will suspend your thread! Warning: If one of your jobs enters an infinite loop, this will never return!

Implements ThreadWeaver::WeaverInterface.

Definition at line 386 of file WeaverImpl.cpp.

void WeaverImpl::incActiveThreadCount ( )

Increment the count of active threads.

Definition at line 300 of file WeaverImpl.cpp.

bool WeaverImpl::isEmpty ( ) const
virtual

Is the queue empty? The queue is empty if no more jobs are queued.

Implements ThreadWeaver::WeaverInterface.

Definition at line 293 of file WeaverImpl.cpp.

bool WeaverImpl::isIdle ( ) const
virtual

Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads.

Implements ThreadWeaver::WeaverInterface.

Definition at line 380 of file WeaverImpl.cpp.

int WeaverImpl::maximumNumberOfThreads ( ) const
virtual

Get the maximum number of threads this Weaver may start.

Implements ThreadWeaver::WeaverInterface.

Definition at line 156 of file WeaverImpl.cpp.

int WeaverImpl::queueLength ( ) const
virtual

Returns the number of pending jobs.

This will return the number of queued jobs. Jobs that are currently being executed are not part of the queue. All jobs in the queue are waiting to be executed.

Implements ThreadWeaver::WeaverInterface.

Definition at line 374 of file WeaverImpl.cpp.

void WeaverImpl::registerObserver ( WeaverObserver *  )
virtual

Register an observer.

Observers provides signals on different weaver events that are otherwise only available through objects of different classes (threads, jobs). Usually, access to the signals of those objects is not provided through the weaver API. Use an observer to reveice notice, for example, on thread activity.

To unregister, simply delete the observer.

Implements ThreadWeaver::WeaverInterface.

Definition at line 168 of file WeaverImpl.cpp.

void WeaverImpl::requestAbort ( )
virtual

Request aborts of the currently executed jobs.

It is important to understand that aborts are requested, but cannot be guaranteed, as not all Job classes support it. It is up to the application to decide if and how job aborts are necessary.

Implements ThreadWeaver::WeaverInterface.

Definition at line 406 of file WeaverImpl.cpp.

void WeaverImpl::resume ( )
virtual

Resume job queueing.

See also
suspend

Implements ThreadWeaver::WeaverInterface.

Definition at line 283 of file WeaverImpl.cpp.

void WeaverImpl::setMaximumNumberOfThreads ( int  cap)
virtual

Set the maximum number of threads this Weaver object may start.

Implements ThreadWeaver::WeaverInterface.

Definition at line 149 of file WeaverImpl.cpp.

void WeaverImpl::setState ( StateId  id)

Set the object state.

Definition at line 124 of file WeaverImpl.cpp.

const State & WeaverImpl::state ( ) const
virtual

Return the state of the weaver object.

Implements ThreadWeaver::WeaverInterface.

Definition at line 143 of file WeaverImpl.cpp.

void WeaverImpl::suspend ( )
virtual

Suspend job execution.

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() and there are no jobs left to be done, you will immediately receive the suspended() signal.

Implements ThreadWeaver::WeaverInterface.

Definition at line 278 of file WeaverImpl.cpp.

Job * WeaverImpl::takeFirstAvailableJob ( Job *  previous)

Take the first available job out of the queue and return it.

The job will be removed from the queue (therefore, take). Only jobs that have no unresolved dependencies are considered available. If only jobs that depened on other, unfinished jobs are in the queue, this method returns a nil pointer.

Definition at line 333 of file WeaverImpl.cpp.

void ThreadWeaver::WeaverImpl::threadBusy ( ThreadWeaver::Thread *  ,
ThreadWeaver::Job *  j 
)
signal

The thread is busy executing job j.

void ThreadWeaver::WeaverImpl::threadExited ( ThreadWeaver::Thread *  )
signal

A thread has exited.

void ThreadWeaver::WeaverImpl::threadStarted ( ThreadWeaver::Thread *  )
signal

A Thread has been created.

void ThreadWeaver::WeaverImpl::threadSuspended ( ThreadWeaver::Thread *  )
signal

A thread has been suspended.

void WeaverImpl::waitForAvailableJob ( Thread *  th)

Wait for a job to become available.

Definition at line 359 of file WeaverImpl.cpp.

Member Data Documentation

int ThreadWeaver::WeaverImpl::m_active
protected

The number of jobs that are assigned to the worker threads, but not finished.

Definition at line 165 of file WeaverImpl.h.

QList<Job*> ThreadWeaver::WeaverImpl::m_assignments
protected

The job queue.

Definition at line 162 of file WeaverImpl.h.

QList<Thread*> ThreadWeaver::WeaverImpl::m_inventory
protected

Lock the mutex for this weaver.

The threads in the inventory need to lock the weaver's mutex to synchronize the job management. Unlock. See lock(). The thread inventory.

Definition at line 160 of file WeaverImpl.h.

int ThreadWeaver::WeaverImpl::m_inventoryMax
protected

Stored setting .

Definition at line 167 of file WeaverImpl.h.

QWaitCondition ThreadWeaver::WeaverImpl::m_jobAvailable
protected

Wait condition all idle or done threads wait for.

Definition at line 169 of file WeaverImpl.h.

QWaitCondition ThreadWeaver::WeaverImpl::m_jobFinished
protected

Wait for a job to finish.

Definition at line 171 of file WeaverImpl.h.


The documentation for this class was generated from the following files:
  • WeaverImpl.h
  • WeaverImpl.cpp
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ThreadWeaver

Skip menu "ThreadWeaver"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal