00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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