Diffstat (limited to 'qmake/tools/qsemaphore_unix.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | qmake/tools/qsemaphore_unix.cpp | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/qmake/tools/qsemaphore_unix.cpp b/qmake/tools/qsemaphore_unix.cpp index fcf28da..4516049 100644 --- a/qmake/tools/qsemaphore_unix.cpp +++ b/qmake/tools/qsemaphore_unix.cpp @@ -138,134 +138,132 @@ int QSemaphore::operator++(int) while (d->value >= d->max) d->cond.wait(&(d->mutex)); ++(d->value); if (d->value > d->max) d->value = d->max; ret = d->value; d->mutex.unlock(); return ret; } /*! Postfix -- operator. Release access of the semaphore. This wakes all threads waiting for access to the semaphore. */ int QSemaphore::operator--(int) { int ret; d->mutex.lock(); --(d->value); if (d->value < 0) d->value = 0; ret = d->value; d->cond.wakeAll(); d->mutex.unlock(); return ret; } /*! Try to get access to the semaphore. If \l available() \< \a n, this call will block until it can get all the accesses it wants, i.e. until available() \>= \a n. */ int QSemaphore::operator+=(int n) { int ret; d->mutex.lock(); + if ( n < 0 || n > d->max ) { +#ifdef QT_CHECK_RANGE + qWarning( "QSemaphore::operator+=: paramter %d out of range", n ); +#endif // QT_CHECK_RANGE + n = n < 0 ? 0 : d->max; + } + while (d->value + n > d->max) d->cond.wait(&(d->mutex)); d->value += n; - -#ifdef QT_CHECK_RANGE - if (d->value > d->max) { - qWarning("QSemaphore::operator+=: attempt to allocate more resources than available"); - d->value = d->max; - } -#endif - ret = d->value; d->mutex.unlock(); return ret; } /*! Release \a n accesses to the semaphore. */ int QSemaphore::operator-=(int n) { int ret; d->mutex.lock(); - d->value -= n; - + if ( n < 0 || n > d->value ) { #ifdef QT_CHECK_RANGE - if (d->value < 0) { - qWarning("QSemaphore::operator-=: attempt to deallocate more resources than taken"); - d->value = 0; + qWarning( "QSemaphore::operator-=: paramter %d out of range", n ); +#endif // QT_CHECK_RANGE + n = n < 0 ? 0 : d->value; } -#endif + d->value -= n; ret = d->value; d->cond.wakeOne(); d->mutex.unlock(); return ret; } /*! Returns the number of accesses currently available to the semaphore. */ int QSemaphore::available() const { int ret; d->mutex.lock(); ret = d->max - d->value; d->mutex.unlock(); return ret; } /*! Returns the total number of accesses to the semaphore. */ int QSemaphore::total() const { int ret; d->mutex.lock(); ret = d->max; d->mutex.unlock(); return ret; } /*! Try to get access to the semaphore. If \l available() \< \a n, this function will return FALSE immediately. If \l available() \>= \a n, this function will take \a n accesses and return TRUE. This function does \e not block. */ bool QSemaphore::tryAccess(int n) { if (! d->mutex.tryLock()) return FALSE; |