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

ThreadWeaver

  • threadweaver
  • Weaver
WeaverImpl.h
Go to the documentation of this file.
1/* -*- C++ -*-
2
3This file implements the public interfaces of the WeaverImpl class.
4
5$ Author: Mirko Boehm $
6$ Copyright: (C) 2005-2013 Mirko Boehm $
7$ Contact: mirko@kde.org
8http://www.kde.org
9http://creative-destruction.me $
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Library General Public License for more details.
20
21 You should have received a copy of the GNU Library General Public License
22 along with this library; see the file COPYING.LIB. If not, write to
23 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA.
25
26$Id: WeaverImpl.h 32 2005-08-17 08:38:01Z mirko $
27*/
28#ifndef WeaverImpl_H
29#define WeaverImpl_H
30
31#include <QtCore/QObject>
32#include <QtCore/QWaitCondition>
33
34#ifndef THREADWEAVER_PRIVATE_API
35#define THREADWEAVER_PRIVATE_API
36#endif
37
38#include "State.h"
39#include "WeaverInterface.h"
40
41namespace ThreadWeaver {
42
43 class Job;
44 class Thread;
45 class WeaverObserver;
46
51 class WeaverImpl : public WeaverInterface
52 {
53 Q_OBJECT
54 public:
56 explicit WeaverImpl (QObject* parent=0 );
58 virtual ~WeaverImpl ();
59 const State& state() const;
60
61 void setMaximumNumberOfThreads( int cap );
62 int maximumNumberOfThreads() const;
63 int currentNumberOfThreads () const;
64
65
67 void setState( StateId );
68 void registerObserver ( WeaverObserver* );
69 virtual void enqueue (Job*);
70 virtual bool dequeue (Job*);
71 virtual void dequeue ();
72 virtual void finish();
73 virtual void suspend( );
74 virtual void resume();
75 bool isEmpty () const;
76 bool isIdle () const;
77 int queueLength () const;
89 virtual Job* applyForWork (Thread *thread, Job *previous);
91 void blockThreadUntilJobsAreBeingAssigned(Thread *th);
93 void waitForAvailableJob(Thread *th);
95 void incActiveThreadCount();
97 void decActiveThreadCount();
101 int activeThreadCount();
107 Job* takeFirstAvailableJob(Job* previous);
111 void assignJobs();
112 void requestAbort();
113
115 void dumpJobs();
116
117 Q_SIGNALS:
119 void threadStarted ( ThreadWeaver::Thread* );
121 void threadExited ( ThreadWeaver::Thread* );
123 void threadSuspended ( ThreadWeaver::Thread* );
125 void threadBusy ( ThreadWeaver::Thread*, ThreadWeaver::Job* j);
126
127 // FIXME (0.7) this seems to be unnecessary
128 // some more private Q_SIGNALS: There are situations where other threads
129 // call functions of (this). In this case, there may be confusion
130 // about whether to handle the signals synchronously or not. The
131 // following signals are asynchroneoulsy connected to their siblings.
132 void asyncThreadSuspended( ThreadWeaver::Thread* );
133
134 protected:
137 void adjustActiveThreadCount ( int diff );
141 virtual Thread* createThread();
150 // @TODO: add code to raise inventory size over inventoryMin
151 // @TODO: add code to quit unnecessary threads
152 void adjustInventory ( int noOfNewJobs );
156// void lock ();
157// /** Unlock. See lock(). */
158// void unlock ();
160 QList<Thread*> m_inventory;
162 QList<Job*> m_assignments;
165 int m_active;
167 int m_inventoryMax;
169 QWaitCondition m_jobAvailable;
171 QWaitCondition m_jobFinished;
172
173 private:
175 QMutex *m_mutex;
176
178 QMutex* m_finishMutex;
179
181 QMutex* m_jobAvailableMutex;
182
183 // @TODO: make state objects static
187 State* m_state;
189 State *m_states[NoOfStates];
190 };
191
192} // namespace ThreadWeaver
193
194#endif // WeaverImpl_H
State.h
WeaverInterface.h
QList
Definition: DependencyPolicy.h:32
QObject
ThreadWeaver::Job
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition: Job.h:66
ThreadWeaver::State
We use a State pattern to handle the system state in ThreadWeaver.
Definition: State.h:74
ThreadWeaver::Thread
The class Thread is used to represent the worker threads in the weaver's inventory.
Definition: Thread.h:47
ThreadWeaver::WeaverImpl
A WeaverImpl is the manager of worker threads (Thread objects) to which it assigns jobs from its queu...
Definition: WeaverImpl.h:52
ThreadWeaver::WeaverImpl::blockThreadUntilJobsAreBeingAssigned
void blockThreadUntilJobsAreBeingAssigned(Thread *th)
Blocks the calling thread until some actor calls assignJobs.
Definition: WeaverImpl.cpp:364
ThreadWeaver::WeaverImpl::setMaximumNumberOfThreads
void setMaximumNumberOfThreads(int cap)
Set the maximum number of threads this Weaver object may start.
Definition: WeaverImpl.cpp:149
ThreadWeaver::WeaverImpl::dumpJobs
void dumpJobs()
Dump the current jobs to the console.
Definition: WeaverImpl.cpp:414
ThreadWeaver::WeaverImpl::threadStarted
void threadStarted(ThreadWeaver::Thread *)
A Thread has been created.
ThreadWeaver::WeaverImpl::isEmpty
bool isEmpty() const
Is the queue empty? The queue is empty if no more jobs are queued.
Definition: WeaverImpl.cpp:293
ThreadWeaver::WeaverImpl::resume
virtual void resume()
Resume job queueing.
Definition: WeaverImpl.cpp:283
ThreadWeaver::WeaverImpl::m_inventory
QList< Thread * > m_inventory
Lock the mutex for this weaver.
Definition: WeaverImpl.h:160
ThreadWeaver::WeaverImpl::m_jobFinished
QWaitCondition m_jobFinished
Wait for a job to finish.
Definition: WeaverImpl.h:171
ThreadWeaver::WeaverImpl::finish
virtual void finish()
Finish all queued operations, then return.
Definition: WeaverImpl.cpp:386
ThreadWeaver::WeaverImpl::registerObserver
void registerObserver(WeaverObserver *)
Register an observer.
Definition: WeaverImpl.cpp:168
ThreadWeaver::WeaverImpl::waitForAvailableJob
void waitForAvailableJob(Thread *th)
Wait for a job to become available.
Definition: WeaverImpl.cpp:359
ThreadWeaver::WeaverImpl::maximumNumberOfThreads
int maximumNumberOfThreads() const
Get the maximum number of threads this Weaver may start.
Definition: WeaverImpl.cpp:156
ThreadWeaver::WeaverImpl::m_inventoryMax
int m_inventoryMax
Stored setting .
Definition: WeaverImpl.h:167
ThreadWeaver::WeaverImpl::~WeaverImpl
virtual ~WeaverImpl()
Destruct a WeaverImpl object.
Definition: WeaverImpl.cpp:78
ThreadWeaver::WeaverImpl::takeFirstAvailableJob
Job * takeFirstAvailableJob(Job *previous)
Take the first available job out of the queue and return it.
Definition: WeaverImpl.cpp:333
ThreadWeaver::WeaverImpl::incActiveThreadCount
void incActiveThreadCount()
Increment the count of active threads.
Definition: WeaverImpl.cpp:300
ThreadWeaver::WeaverImpl::setState
void setState(StateId)
Set the object state.
Definition: WeaverImpl.cpp:124
ThreadWeaver::WeaverImpl::asyncThreadSuspended
void asyncThreadSuspended(ThreadWeaver::Thread *)
ThreadWeaver::WeaverImpl::applyForWork
virtual Job * applyForWork(Thread *thread, Job *previous)
Assign a job to the calling thread.
Definition: WeaverImpl.cpp:354
ThreadWeaver::WeaverImpl::threadExited
void threadExited(ThreadWeaver::Thread *)
A thread has exited.
ThreadWeaver::WeaverImpl::m_assignments
QList< Job * > m_assignments
The job queue.
Definition: WeaverImpl.h:162
ThreadWeaver::WeaverImpl::adjustActiveThreadCount
void adjustActiveThreadCount(int diff)
Adjust active thread count.
Definition: WeaverImpl.cpp:313
ThreadWeaver::WeaverImpl::currentNumberOfThreads
int currentNumberOfThreads() const
Returns the current number of threads in the inventory.
Definition: WeaverImpl.cpp:162
ThreadWeaver::WeaverImpl::queueLength
int queueLength() const
Returns the number of pending jobs.
Definition: WeaverImpl.cpp:374
ThreadWeaver::WeaverImpl::threadBusy
void threadBusy(ThreadWeaver::Thread *, ThreadWeaver::Job *j)
The thread is busy executing job j.
ThreadWeaver::WeaverImpl::m_active
int m_active
The number of jobs that are assigned to the worker threads, but not finished.
Definition: WeaverImpl.h:165
ThreadWeaver::WeaverImpl::state
const State & state() const
Return the state of the weaver object.
Definition: WeaverImpl.cpp:143
ThreadWeaver::WeaverImpl::activeThreadCount
int activeThreadCount()
Returns the number of active threads.
Definition: WeaverImpl.cpp:327
ThreadWeaver::WeaverImpl::adjustInventory
void adjustInventory(int noOfNewJobs)
Adjust the inventory size.
Definition: WeaverImpl.cpp:205
ThreadWeaver::WeaverImpl::requestAbort
void requestAbort()
Request aborts of the currently executed jobs.
Definition: WeaverImpl.cpp:406
ThreadWeaver::WeaverImpl::enqueue
virtual void enqueue(Job *)
Add a job to be executed.
Definition: WeaverImpl.cpp:182
ThreadWeaver::WeaverImpl::decActiveThreadCount
void decActiveThreadCount()
Decrement the count of active threads.
Definition: WeaverImpl.cpp:305
ThreadWeaver::WeaverImpl::threadSuspended
void threadSuspended(ThreadWeaver::Thread *)
A thread has been suspended.
ThreadWeaver::WeaverImpl::suspend
virtual void suspend()
Suspend job execution.
Definition: WeaverImpl.cpp:278
ThreadWeaver::WeaverImpl::createThread
virtual Thread * createThread()
Factory method to create the threads.
Definition: WeaverImpl.cpp:233
ThreadWeaver::WeaverImpl::isIdle
bool isIdle() const
Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads...
Definition: WeaverImpl.cpp:380
ThreadWeaver::WeaverImpl::assignJobs
void assignJobs()
Schedule enqueued jobs to be executed by idle threads.
Definition: WeaverImpl.cpp:288
ThreadWeaver::WeaverImpl::dequeue
virtual void dequeue()
Remove all queued jobs.
Definition: WeaverImpl.cpp:265
ThreadWeaver::WeaverImpl::m_jobAvailable
QWaitCondition m_jobAvailable
Wait condition all idle or done threads wait for.
Definition: WeaverImpl.h:169
ThreadWeaver::WeaverInterface
WeaverInterface provides a common interface for weaver implementations.
Definition: WeaverInterface.h:61
ThreadWeaver::WeaverObserver
Observers provides signals on some Weaver events that are otherwise only available through objects of...
Definition: WeaverObserver.h:59
ThreadWeaver
Definition: DebuggingAids.h:51
ThreadWeaver::StateId
StateId
All weaver objects maintain a state of operation which can be queried by the application.
Definition: State.h:48
ThreadWeaver::NoOfStates
@ NoOfStates
Not a state, but a sentinel for the number of defined states.
Definition: State.h:68
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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