ThreadWeaver
WeaverImpl.h
Go to the documentation of this file.00001 /* -*- C++ -*- 00002 00003 This file implements the public interfaces of the WeaverImpl class. 00004 00005 $ Author: Mirko Boehm $ 00006 $ Copyright: (C) 2005, 2006 Mirko Boehm $ 00007 $ Contact: mirko@kde.org 00008 http://www.kde.org 00009 http://www.hackerbuero.org $ 00010 00011 This library is free software; you can redistribute it and/or 00012 modify it under the terms of the GNU Library General Public 00013 License as published by the Free Software Foundation; either 00014 version 2 of the License, or (at your option) any later version. 00015 00016 This library is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 Library General Public License for more details. 00020 00021 You should have received a copy of the GNU Library General Public License 00022 along with this library; see the file COPYING.LIB. If not, write to 00023 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00024 Boston, MA 02110-1301, USA. 00025 00026 $Id: WeaverImpl.h 32 2005-08-17 08:38:01Z mirko $ 00027 */ 00028 #ifndef WeaverImpl_H 00029 #define WeaverImpl_H 00030 00031 #include <QtCore/QObject> 00032 #include <QtCore/QWaitCondition> 00033 00034 #ifndef THREADWEAVER_PRIVATE_API 00035 #define THREADWEAVER_PRIVATE_API 00036 #endif 00037 00038 #include "State.h" 00039 #include "WeaverInterface.h" 00040 00041 namespace ThreadWeaver { 00042 00043 class Job; 00044 class Thread; 00045 class WeaverObserver; 00046 00051 class WeaverImpl : public WeaverInterface 00052 { 00053 Q_OBJECT 00054 public: 00056 explicit WeaverImpl (QObject* parent=0 ); 00058 virtual ~WeaverImpl (); 00059 const State& state() const; 00060 00061 void setMaximumNumberOfThreads( int cap ); 00062 int maximumNumberOfThreads() const; 00063 int currentNumberOfThreads () const; 00064 00065 00067 void setState( StateId ); 00068 void registerObserver ( WeaverObserver* ); 00069 virtual void enqueue (Job*); 00070 virtual bool dequeue (Job*); 00071 virtual void dequeue (); 00072 virtual void finish(); 00073 virtual void suspend( ); 00074 virtual void resume(); 00075 bool isEmpty () const; 00076 bool isIdle () const; 00077 int queueLength () const; 00089 virtual Job* applyForWork (Thread *thread, Job *previous); 00091 void blockThreadUntilJobsAreBeingAssigned(Thread *th); 00093 void waitForAvailableJob(Thread *th); 00095 void incActiveThreadCount(); 00097 void decActiveThreadCount(); 00101 int activeThreadCount(); 00107 Job* takeFirstAvailableJob(); 00111 void assignJobs(); 00112 void requestAbort(); 00113 00115 void dumpJobs(); 00116 00117 Q_SIGNALS: 00119 void threadStarted ( ThreadWeaver::Thread* ); 00121 void threadExited ( ThreadWeaver::Thread* ); 00123 void threadSuspended ( ThreadWeaver::Thread* ); 00125 void threadBusy ( ThreadWeaver::Thread*, ThreadWeaver::Job* j); 00126 00127 // FIXME (0.7) this seems to be unnecessary 00128 // some more private Q_SIGNALS: There are situations where other threads 00129 // call functions of (this). In this case, there may be confusion 00130 // about whether to handle th signals synchroneously or not. The 00131 // following signals are asynchroneoulsy connected to their siblings. 00132 void asyncThreadSuspended( ThreadWeaver::Thread* ); 00133 00134 protected: 00137 void adjustActiveThreadCount ( int diff ); 00141 virtual Thread* createThread(); 00150 // @TODO: add code to raise inventory size over inventoryMin 00151 // @TODO: add code to quit unnecessary threads 00152 void adjustInventory ( int noOfNewJobs ); 00156 // void lock (); 00157 // /** Unlock. See lock(). */ 00158 // void unlock (); 00160 QList<Thread*> m_inventory; 00162 QList<Job*> m_assignments; 00165 int m_active; 00167 int m_inventoryMax; 00169 QWaitCondition m_jobAvailable; 00171 QWaitCondition m_jobFinished; 00172 00173 private: 00175 QMutex *m_mutex; 00176 00178 QMutex* m_finishMutex; 00179 00181 QMutex* m_jobAvailableMutex; 00182 00183 // @TODO: make state objects static 00187 State* m_state; 00189 State *m_states[NoOfStates]; 00190 }; 00191 00192 } // namespace ThreadWeaver 00193 00194 #endif // WeaverImpl_H