summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qsemaphore.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/thread/qsemaphore.cpp')
-rw-r--r--src/corelib/thread/qsemaphore.cpp39
1 files changed, 6 insertions, 33 deletions
diff --git a/src/corelib/thread/qsemaphore.cpp b/src/corelib/thread/qsemaphore.cpp
index 74e0746f43..aa04fb10ff 100644
--- a/src/corelib/thread/qsemaphore.cpp
+++ b/src/corelib/thread/qsemaphore.cpp
@@ -39,8 +39,6 @@
****************************************************************************/
#include "qsemaphore.h"
-
-#ifndef QT_NO_THREAD
#include "qmutex.h"
#include "qfutex_p.h"
#include "qwaitcondition.h"
@@ -487,11 +485,9 @@ bool QSemaphore::tryAcquire(int n, int timeout)
QDeadlineTimer timer(timeout);
QMutexLocker locker(&d->mutex);
- qint64 remainingTime = timer.remainingTime();
- while (n > d->avail && remainingTime != 0) {
- if (!d->cond.wait(locker.mutex(), remainingTime))
+ while (n > d->avail && !timer.hasExpired()) {
+ if (!d->cond.wait(locker.mutex(), timer))
return false;
- remainingTime = timer.remainingTime();
}
if (n > d->avail)
return false;
@@ -519,10 +515,7 @@ bool QSemaphore::tryAcquire(int n, int timeout)
You can use this to reliably release a semaphore to avoid dead-lock
in the face of exceptions or early returns:
- \code
- // ... do something that may throw or return early
- sem.release();
- \endcode
+ \snippet code/src_corelib_thread_qsemaphore.cpp 4
If an early return is taken or an exception is thrown before the
\c{sem.release()} call is reached, the semaphore is not released,
@@ -531,11 +524,7 @@ bool QSemaphore::tryAcquire(int n, int timeout)
When using RAII instead:
- \code
- const QSemaphoreReleaser releaser(sem);
- // ... do something that may throw or early return
- // implicitly calls sem.release() here and at every other return in between
- \endcode
+ \snippet code/src_corelib_thread_qsemaphore.cpp 5
this can no longer happen, because the compiler will make sure that
the QSemaphoreReleaser destructor is always called, and therefore
@@ -545,17 +534,7 @@ bool QSemaphore::tryAcquire(int n, int timeout)
from functions to transfer responsibility for releasing a semaphore
out of a function or a scope:
- \code
- { // some scope
- QSemaphoreReleaser releaser; // does nothing
- // ...
- if (someCondition) {
- releaser = QSemaphoreReleaser(sem);
- // ...
- }
- // ...
- } // conditionally calls sem.release(), depending on someCondition
- \endcode
+ \snippet code/src_corelib_thread_qsemaphore.cpp 6
A QSemaphoreReleaser can be canceled by a call to cancel(). A canceled
semaphore releaser will no longer call QSemaphore::release() in its
@@ -639,14 +618,8 @@ bool QSemaphore::tryAcquire(int n, int timeout)
To enable again, assign a new QSemaphoreReleaser:
- \code
- releaser.cancel(); // avoid releasing old semaphore()
- releaser = QSemaphoreReleaser(sem, 42);
- // now will call sem.release(42) when 'releaser' is destroyed
- \endcode
+ \snippet code/src_corelib_thread_qsemaphore.cpp 7
*/
QT_END_NAMESPACE
-
-#endif // QT_NO_THREAD