summaryrefslogtreecommitdiff
path: root/qmake/tools/qsemaphore_unix.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qsemaphore_unix.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qsemaphore_unix.cpp26
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
@@ -162,86 +162,84 @@ int QSemaphore::operator--(int)
162 d->mutex.lock(); 162 d->mutex.lock();
163 163
164 --(d->value); 164 --(d->value);
165 if (d->value < 0) d->value = 0; 165 if (d->value < 0) d->value = 0;
166 ret = d->value; 166 ret = d->value;
167 167
168 d->cond.wakeAll(); 168 d->cond.wakeAll();
169 d->mutex.unlock(); 169 d->mutex.unlock();
170 170
171 return ret; 171 return ret;
172} 172}
173 173
174 174
175/*! 175/*!
176 Try to get access to the semaphore. If \l available() \< \a n, this 176 Try to get access to the semaphore. If \l available() \< \a n, this
177 call will block until it can get all the accesses it wants, i.e. 177 call will block until it can get all the accesses it wants, i.e.
178 until available() \>= \a n. 178 until available() \>= \a n.
179*/ 179*/
180int QSemaphore::operator+=(int n) 180int QSemaphore::operator+=(int n)
181{ 181{
182 int ret; 182 int ret;
183 183
184 d->mutex.lock(); 184 d->mutex.lock();
185 185
186 if ( n < 0 || n > d->max ) {
187#ifdef QT_CHECK_RANGE
188 qWarning( "QSemaphore::operator+=: paramter %d out of range", n );
189#endif // QT_CHECK_RANGE
190 n = n < 0 ? 0 : d->max;
191 }
192
186 while (d->value + n > d->max) 193 while (d->value + n > d->max)
187 d->cond.wait(&(d->mutex)); 194 d->cond.wait(&(d->mutex));
188 195
189 d->value += n; 196 d->value += n;
190
191#ifdef QT_CHECK_RANGE
192 if (d->value > d->max) {
193 qWarning("QSemaphore::operator+=: attempt to allocate more resources than available");
194 d->value = d->max;
195 }
196#endif
197
198 ret = d->value; 197 ret = d->value;
199 198
200 d->mutex.unlock(); 199 d->mutex.unlock();
201 200
202 return ret; 201 return ret;
203} 202}
204 203
205 204
206/*! 205/*!
207 Release \a n accesses to the semaphore. 206 Release \a n accesses to the semaphore.
208*/ 207*/
209int QSemaphore::operator-=(int n) 208int QSemaphore::operator-=(int n)
210{ 209{
211 int ret; 210 int ret;
212 211
213 d->mutex.lock(); 212 d->mutex.lock();
214 213
215 d->value -= n; 214 if ( n < 0 || n > d->value ) {
216
217#ifdef QT_CHECK_RANGE 215#ifdef QT_CHECK_RANGE
218 if (d->value < 0) { 216 qWarning( "QSemaphore::operator-=: paramter %d out of range", n );
219 qWarning("QSemaphore::operator-=: attempt to deallocate more resources than taken"); 217#endif // QT_CHECK_RANGE
220 d->value = 0; 218 n = n < 0 ? 0 : d->value;
221 } 219 }
222#endif
223 220
221 d->value -= n;
224 ret = d->value; 222 ret = d->value;
225 223
226 d->cond.wakeOne(); 224 d->cond.wakeOne();
227 d->mutex.unlock(); 225 d->mutex.unlock();
228 226
229 return ret; 227 return ret;
230} 228}
231 229
232 230
233/*! 231/*!
234 Returns the number of accesses currently available to the 232 Returns the number of accesses currently available to the
235 semaphore. 233 semaphore.
236*/ 234*/
237int QSemaphore::available() const { 235int QSemaphore::available() const {
238 int ret; 236 int ret;
239 237
240 d->mutex.lock(); 238 d->mutex.lock();
241 ret = d->max - d->value; 239 ret = d->max - d->value;
242 d->mutex.unlock(); 240 d->mutex.unlock();
243 241
244 return ret; 242 return ret;
245} 243}
246 244
247 245