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 | |||
@@ -122,166 +122,164 @@ QSemaphore::~QSemaphore() | |||
122 | delete d; | 122 | delete d; |
123 | } | 123 | } |
124 | 124 | ||
125 | 125 | ||
126 | /*! | 126 | /*! |
127 | Postfix ++ operator. | 127 | Postfix ++ operator. |
128 | 128 | ||
129 | Try to get access to the semaphore. If \l available() == 0, this | 129 | Try to get access to the semaphore. If \l available() == 0, this |
130 | call will block until it can get access, i.e. until available() \> | 130 | call will block until it can get access, i.e. until available() \> |
131 | 0. | 131 | 0. |
132 | */ | 132 | */ |
133 | int QSemaphore::operator++(int) | 133 | int QSemaphore::operator++(int) |
134 | { | 134 | { |
135 | int ret; | 135 | int ret; |
136 | 136 | ||
137 | d->mutex.lock(); | 137 | d->mutex.lock(); |
138 | 138 | ||
139 | while (d->value >= d->max) | 139 | while (d->value >= d->max) |
140 | d->cond.wait(&(d->mutex)); | 140 | d->cond.wait(&(d->mutex)); |
141 | 141 | ||
142 | ++(d->value); | 142 | ++(d->value); |
143 | if (d->value > d->max) d->value = d->max; | 143 | if (d->value > d->max) d->value = d->max; |
144 | ret = d->value; | 144 | ret = d->value; |
145 | 145 | ||
146 | d->mutex.unlock(); | 146 | d->mutex.unlock(); |
147 | 147 | ||
148 | return ret; | 148 | return ret; |
149 | } | 149 | } |
150 | 150 | ||
151 | 151 | ||
152 | /*! | 152 | /*! |
153 | Postfix -- operator. | 153 | Postfix -- operator. |
154 | 154 | ||
155 | Release access of the semaphore. This wakes all threads waiting | 155 | Release access of the semaphore. This wakes all threads waiting |
156 | for access to the semaphore. | 156 | for access to the semaphore. |
157 | */ | 157 | */ |
158 | int QSemaphore::operator--(int) | 158 | int QSemaphore::operator--(int) |
159 | { | 159 | { |
160 | int ret; | 160 | int ret; |
161 | 161 | ||
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 | */ |
180 | int QSemaphore::operator+=(int n) | 180 | int 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 | */ |
209 | int QSemaphore::operator-=(int n) | 208 | int 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 | */ |
237 | int QSemaphore::available() const { | 235 | int 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 | ||
248 | /*! | 246 | /*! |
249 | Returns the total number of accesses to the semaphore. | 247 | Returns the total number of accesses to the semaphore. |
250 | */ | 248 | */ |
251 | int QSemaphore::total() const { | 249 | int QSemaphore::total() const { |
252 | int ret; | 250 | int ret; |
253 | 251 | ||
254 | d->mutex.lock(); | 252 | d->mutex.lock(); |
255 | ret = d->max; | 253 | ret = d->max; |
256 | d->mutex.unlock(); | 254 | d->mutex.unlock(); |
257 | 255 | ||
258 | return ret; | 256 | return ret; |
259 | } | 257 | } |
260 | 258 | ||
261 | 259 | ||
262 | /*! | 260 | /*! |
263 | Try to get access to the semaphore. If \l available() \< \a n, this | 261 | Try to get access to the semaphore. If \l available() \< \a n, this |
264 | function will return FALSE immediately. If \l available() \>= \a n, | 262 | function will return FALSE immediately. If \l available() \>= \a n, |
265 | this function will take \a n accesses and return TRUE. This | 263 | this function will take \a n accesses and return TRUE. This |
266 | function does \e not block. | 264 | function does \e not block. |
267 | */ | 265 | */ |
268 | bool QSemaphore::tryAccess(int n) | 266 | bool QSemaphore::tryAccess(int n) |
269 | { | 267 | { |
270 | if (! d->mutex.tryLock()) | 268 | if (! d->mutex.tryLock()) |
271 | return FALSE; | 269 | return FALSE; |
272 | 270 | ||
273 | if (d->value + n > d->max) { | 271 | if (d->value + n > d->max) { |
274 | d->mutex.unlock(); | 272 | d->mutex.unlock(); |
275 | return FALSE; | 273 | return FALSE; |
276 | } | 274 | } |
277 | 275 | ||
278 | d->value += n; | 276 | d->value += n; |
279 | 277 | ||
280 | #ifdef QT_CHECK_RANGE | 278 | #ifdef QT_CHECK_RANGE |
281 | if (d->value > d->max) { | 279 | if (d->value > d->max) { |
282 | qWarning("QSemaphore::operator+=: attempt to allocate more resources than available"); | 280 | qWarning("QSemaphore::operator+=: attempt to allocate more resources than available"); |
283 | d->value = d->max; | 281 | d->value = d->max; |
284 | } | 282 | } |
285 | #endif | 283 | #endif |
286 | 284 | ||
287 | d->mutex.unlock(); | 285 | d->mutex.unlock(); |