Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qsemaphore_unix.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qsemaphore_unix.cpp,v 1.2 2003/07/10 02:40:12 llornkcor Exp $
00003 **
00004 ** QSemaphore class for Unix
00005 **
00006 ** Created : 20010725
00007 **
00008 ** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #if defined(QT_THREAD_SUPPORT)
00039 
00040 #include "qsemaphore.h"
00041 #include "qmutex.h"
00042 #include "qwaitcondition.h"
00043 
00044 
00087 class QSemaphorePrivate {
00088 public:
00089     QSemaphorePrivate(int);
00090 
00091     QMutex mutex;
00092     QWaitCondition cond;
00093 
00094     int value, max;
00095 };
00096 
00097 
00098 QSemaphorePrivate::QSemaphorePrivate(int m)
00099     : mutex(FALSE), value(0), max(m)
00100 {
00101 }
00102 
00103 
00108 QSemaphore::QSemaphore(int maxcount)
00109 {
00110     d = new QSemaphorePrivate(maxcount);
00111 }
00112 
00113 
00120 QSemaphore::~QSemaphore()
00121 {
00122     delete d;
00123 }
00124 
00125 
00133 int QSemaphore::operator++(int)
00134 {
00135     int ret;
00136 
00137     d->mutex.lock();
00138 
00139     while (d->value >= d->max)
00140         d->cond.wait(&(d->mutex));
00141 
00142     ++(d->value);
00143     if (d->value > d->max) d->value = d->max;
00144     ret = d->value;
00145 
00146     d->mutex.unlock();
00147 
00148     return ret;
00149 }
00150 
00151 
00158 int QSemaphore::operator--(int)
00159 {
00160     int ret;
00161 
00162     d->mutex.lock();
00163 
00164     --(d->value);
00165     if (d->value < 0) d->value = 0;
00166     ret = d->value;
00167 
00168     d->cond.wakeAll();
00169     d->mutex.unlock();
00170 
00171     return ret;
00172 }
00173 
00174 
00180 int QSemaphore::operator+=(int n)
00181 {
00182     int ret;
00183 
00184     d->mutex.lock();
00185 
00186     if ( n < 0 || n > d->max ) {
00187 #ifdef QT_CHECK_RANGE
00188         qWarning( "QSemaphore::operator+=: paramter %d out of range", n );
00189 #endif // QT_CHECK_RANGE
00190         n = n < 0 ? 0 : d->max;
00191     }
00192 
00193     while (d->value + n > d->max)
00194         d->cond.wait(&(d->mutex));
00195 
00196     d->value += n;
00197     ret = d->value;
00198 
00199     d->mutex.unlock();
00200 
00201     return ret;
00202 }
00203 
00204 
00208 int QSemaphore::operator-=(int n)
00209 {
00210     int ret;
00211 
00212     d->mutex.lock();
00213 
00214     if ( n < 0 || n > d->value ) {
00215 #ifdef QT_CHECK_RANGE
00216         qWarning( "QSemaphore::operator-=: paramter %d out of range", n );
00217 #endif // QT_CHECK_RANGE
00218         n = n < 0 ? 0 : d->value;
00219     }
00220 
00221     d->value -= n;
00222     ret = d->value;
00223 
00224     d->cond.wakeOne();
00225     d->mutex.unlock();
00226 
00227     return ret;
00228 }
00229 
00230 
00235 int QSemaphore::available() const {
00236     int ret;
00237 
00238     d->mutex.lock();
00239     ret = d->max - d->value;
00240     d->mutex.unlock();
00241 
00242     return ret;
00243 }
00244 
00245 
00249 int QSemaphore::total() const {
00250     int ret;
00251 
00252     d->mutex.lock();
00253     ret = d->max;
00254     d->mutex.unlock();
00255 
00256     return ret;
00257 }
00258 
00259 
00266 bool QSemaphore::tryAccess(int n)
00267 {
00268     if (! d->mutex.tryLock())
00269         return FALSE;
00270 
00271     if (d->value + n > d->max) {
00272         d->mutex.unlock();
00273         return FALSE;
00274     }
00275 
00276     d->value += n;
00277 
00278 #ifdef QT_CHECK_RANGE
00279     if (d->value > d->max) {
00280         qWarning("QSemaphore::operator+=: attempt to allocate more resources than available");
00281         d->value = d->max;
00282     }
00283 #endif
00284 
00285     d->mutex.unlock();
00286 
00287     return TRUE;
00288 }
00289 
00290 #endif // QT_THREAD_SUPPORT

Generated on Sat Nov 5 16:18:32 2005 for OPIE by  doxygen 1.4.2