ThreadWeaver
ResourceRestrictionPolicy.cpp
Go to the documentation of this file.00001 /* -*- C++ -*- 00002 00003 This file implements the ResourceRestrictionPolicy class. 00004 00005 $ Author: Mirko Boehm $ 00006 $ Copyright: (C) 2004, 2005 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: Job.h 32 2005-08-17 08:38:01Z mirko $ 00027 */ 00028 00029 #include "ResourceRestrictionPolicy.h" 00030 00031 #include <QtCore/QList> 00032 #include <QtCore/QMutex> 00033 00034 #include "DebuggingAids.h" 00035 00036 using namespace ThreadWeaver; 00037 00038 class ResourceRestrictionPolicy::Private 00039 { 00040 public: 00041 Private ( int theCap ) 00042 : cap ( theCap) 00043 {} 00044 00045 int cap; 00046 QList<Job*> customers; 00047 QMutex mutex; 00048 }; 00049 00050 ResourceRestrictionPolicy::ResourceRestrictionPolicy ( int cap) 00051 : QueuePolicy () 00052 , d (new Private (cap)) 00053 { 00054 } 00055 00056 ResourceRestrictionPolicy::~ResourceRestrictionPolicy() 00057 { 00058 delete d; 00059 } 00060 00061 void ResourceRestrictionPolicy::setCap (int cap) 00062 { 00063 QMutexLocker l ( & d->mutex ); 00064 d->cap = cap; 00065 } 00066 00067 int ResourceRestrictionPolicy::cap() const 00068 { 00069 QMutexLocker l ( & d->mutex ); 00070 return d->cap; 00071 } 00072 00073 bool ResourceRestrictionPolicy::canRun( Job* job ) 00074 { 00075 QMutexLocker l ( & d->mutex ); 00076 if ( d->customers.size() < d->cap ) 00077 { 00078 d->customers.append( job ); 00079 return true; 00080 } else { 00081 return false; 00082 } 00083 } 00084 00085 void ResourceRestrictionPolicy::free ( Job* job ) 00086 { 00087 QMutexLocker l ( & d->mutex ); 00088 int position = d->customers.indexOf (job); 00089 00090 if (position != -1) 00091 { 00092 debug ( 4, "ResourceRestrictionPolicy::free: job %p done.\n", (void*)job ); 00093 d->customers.removeAt (position); 00094 } 00095 } 00096 00097 void ResourceRestrictionPolicy::release ( Job* job ) 00098 { 00099 free (job); 00100 } 00101 00102 void ResourceRestrictionPolicy::destructed ( Job* job ) 00103 { 00104 free (job); 00105 }