ThreadWeaver
ThreadWeaver.cpp
Go to the documentation of this file.00001 /* -*- C++ -*- 00002 00003 This file implements the Weaver 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: ThreadWeaver.cpp 30 2005-08-16 16:16:04Z mirko $ 00027 */ 00028 00029 #include "ThreadWeaver.h" 00030 00031 #include <QtCore/QMutex> 00032 00033 #include "WeaverImpl.h" 00034 #include "WeaverObserver.h" 00035 00036 using namespace ThreadWeaver; 00037 00038 class Weaver::Private 00039 { 00040 public: 00041 Private () 00042 : weaverinterface ( 0) 00043 {} 00044 00045 WeaverInterface* weaverinterface; 00046 }; 00047 00048 Weaver::Weaver ( QObject* parent ) 00049 : WeaverInterface( parent ) 00050 , d (new Private) 00051 { 00052 d->weaverinterface = makeWeaverImpl(); 00053 connect ( d->weaverinterface, SIGNAL ( finished() ), SIGNAL ( finished() ) ); 00054 connect ( d->weaverinterface, SIGNAL ( suspended() ), SIGNAL ( suspended() ) ); 00055 connect ( d->weaverinterface, SIGNAL ( jobDone( ThreadWeaver::Job* ) ), 00056 SIGNAL ( jobDone ( ThreadWeaver::Job* ) ) ); 00057 } 00058 00059 Weaver::~Weaver() 00060 { 00061 delete d->weaverinterface; 00062 delete d; 00063 } 00064 00065 WeaverInterface* Weaver::makeWeaverImpl() 00066 { 00067 return new WeaverImpl ( this ); 00068 } 00069 00070 const State& Weaver::state() const 00071 { 00072 return d->weaverinterface->state(); 00073 } 00074 00075 void Weaver::registerObserver ( WeaverObserver *ext ) 00076 { 00077 d->weaverinterface->registerObserver ( ext ); 00078 } 00079 00080 Weaver* Weaver::instance() 00081 { 00086 static Weaver* s_instance; 00087 00088 if ( s_instance == 0 ) 00089 { // we try to avoid the expensive mutex-lock operation if possible: 00090 static QMutex mutex; 00091 QMutexLocker l(&mutex); 00092 if ( s_instance == 0 ) 00093 { 00094 s_instance = new Weaver(); 00095 } 00096 } 00097 return s_instance; 00098 } 00099 00100 void Weaver::enqueue (Job* j) 00101 { 00102 d->weaverinterface->enqueue ( j ); 00103 } 00104 00105 bool Weaver::dequeue (Job* j) 00106 { 00107 return d->weaverinterface->dequeue ( j ); 00108 } 00109 00110 void Weaver::dequeue () 00111 { 00112 return d->weaverinterface->dequeue(); 00113 } 00114 00115 void Weaver::finish () 00116 { 00117 return d->weaverinterface->finish (); 00118 } 00119 00120 void Weaver::suspend () 00121 { 00122 return d->weaverinterface->suspend(); 00123 } 00124 00125 void Weaver::resume () 00126 { 00127 return d->weaverinterface->resume(); 00128 } 00129 00130 bool Weaver::isEmpty() const 00131 { 00132 return d->weaverinterface->isEmpty(); 00133 } 00134 00135 bool Weaver::isIdle() const 00136 { 00137 return d->weaverinterface->isIdle(); 00138 } 00139 00140 int Weaver::queueLength() const 00141 { 00142 return d->weaverinterface->queueLength(); 00143 } 00144 00145 void Weaver::setMaximumNumberOfThreads( int cap ) 00146 { 00147 d->weaverinterface->setMaximumNumberOfThreads( cap ); 00148 } 00149 00150 int Weaver::currentNumberOfThreads() const 00151 { 00152 return d->weaverinterface->currentNumberOfThreads(); 00153 } 00154 00155 int Weaver::maximumNumberOfThreads() const 00156 { 00157 return d->weaverinterface->maximumNumberOfThreads(); 00158 } 00159 00160 void Weaver::requestAbort() 00161 { 00162 d->weaverinterface->requestAbort(); 00163 } 00164 00165 #include "ThreadWeaver.moc"