summaryrefslogtreecommitdiff
path: root/qmake/tools/qwaitcondition_unix.cpp
Side-by-side diff
Diffstat (limited to 'qmake/tools/qwaitcondition_unix.cpp') (more/less context) (show whitespace changes)
-rw-r--r--qmake/tools/qwaitcondition_unix.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/qmake/tools/qwaitcondition_unix.cpp b/qmake/tools/qwaitcondition_unix.cpp
index 99c1014..6684617 100644
--- a/qmake/tools/qwaitcondition_unix.cpp
+++ b/qmake/tools/qwaitcondition_unix.cpp
@@ -103,49 +103,49 @@ struct QWaitConditionPrivate {
avoided by, for example, doing something like this:
\code
QMutex mymutex;
QWaitCondition key_pressed;
int mycount=0;
// Worker thread code
for (;;) {
key_pressed.wait(); // This is a QWaitCondition global variable
mymutex.lock();
mycount++;
mymutex.unlock();
do_something();
mymutex.lock();
mycount--;
mymutex.unlock();
}
// Key reading thread code
for (;;) {
getchar();
mymutex.lock();
// Sleep until there are no busy worker threads
- while( count > 0 ) {
+ while( mycount > 0 ) {
mymutex.unlock();
sleep( 1 );
mymutex.lock();
}
mymutex.unlock();
key_pressed.wakeAll();
}
\endcode
The mutexes are necessary because the results of two threads
attempting to change the value of the same variable simultaneously
are unpredictable.
*/
/*!
Constructs a new event signalling, i.e. wait condition, object.
*/
QWaitCondition::QWaitCondition()
{
d = new QWaitConditionPrivate;
int ret = pthread_cond_init(&d->cond, NULL);
#ifdef QT_CHECK_RANGE
@@ -203,108 +203,113 @@ void QWaitCondition::wakeAll()
int ret = pthread_cond_broadcast(&d->cond);
#ifdef QT_CHECK_RANGE
if (ret)
qWarning("Wait condition wakeAll failure: %s", strerror(ret));
#endif
}
/*!
Wait on the thread event object. The thread calling this will
block until either of these conditions is met:
\list
\i Another thread signals it using wakeOne() or wakeAll(). This
function will return TRUE in this case.
\i \a time milliseconds has elapsed. If \a time is ULONG_MAX (the
default), then the wait will never timeout (the event must be
signalled). This function will return FALSE if the wait timed
out.
\endlist
\sa wakeOne(), wakeAll()
*/
bool QWaitCondition::wait(unsigned long time)
{
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+ pthread_mutex_t mutex;
+ pthread_mutex_init( &mutex, 0 );
+ pthread_mutex_lock( &mutex );
int ret;
if (time != ULONG_MAX) {
struct timeval tv;
gettimeofday(&tv, 0);
timespec ti;
- ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000;
+ ti.tv_nsec = ( tv.tv_usec + ( time % 1000 ) * 1000 ) * 1000;
ti.tv_sec = tv.tv_sec + (time / 1000) + ( ti.tv_nsec / 1000000000 );
ti.tv_nsec %= 1000000000;
ret = pthread_cond_timedwait(&d->cond, &mutex, &ti);
} else
ret = pthread_cond_wait(&d->cond, &mutex);
#ifdef QT_CHECK_RANGE
if (ret && ret != ETIMEDOUT)
qWarning("Wait condition wait failure: %s",strerror(ret));
#endif
+ pthread_mutex_unlock( &mutex );
+ pthread_mutex_destroy( &mutex );
+
return (ret == 0);
}
/*!
\overload
Release the locked \a mutex and wait on the thread event object.
The \a mutex must be initially locked by the calling thread. If \a
mutex is not in a locked state, this function returns immediately.
If \a mutex is a recursive mutex, this function returns
immediately. The \a mutex will be unlocked, and the calling thread
will block until either of these conditions is met:
\list
\i Another thread signals it using wakeOne() or wakeAll(). This
function will return TRUE in this case.
\i \a time milliseconds has elapsed. If \a time is ULONG_MAX (the
default), then the wait will never timeout (the event must be
signalled). This function will return FALSE if the wait timed
out.
\endlist
The mutex will be returned to the same locked state. This function
is provided to allow the atomic transition from the locked state
to the wait state.
\sa wakeOne(), wakeAll()
*/
bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
{
if (! mutex)
return FALSE;
if (mutex->d->type() == Q_MUTEX_RECURSIVE) {
#ifdef QT_CHECK_RANGE
qWarning("Wait condition warning: using recursive mutexes with\n"
" wait conditions is undefined!");
#endif
return FALSE;
}
int ret;
if (time != ULONG_MAX) {
struct timeval tv;
gettimeofday(&tv, 0);
timespec ti;
- ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000;
+ ti.tv_nsec = ( tv.tv_usec + ( time % 1000 ) * 1000 ) * 1000;
ti.tv_sec = tv.tv_sec + (time / 1000) + ( ti.tv_nsec / 1000000000 );
ti.tv_nsec %= 1000000000;
ret = pthread_cond_timedwait(&d->cond, &mutex->d->handle, &ti);
} else
ret = pthread_cond_wait(&d->cond, &mutex->d->handle);
#ifdef QT_CHECK_RANGE
if (ret && ret != ETIMEDOUT)
qWarning("Wait condition wait failure: %s",strerror(ret));
#endif
return (ret == 0);
}
#endif // QT_THREAD_SUPPORT