|
Packit |
1c1d7e |
/****************************************************************************
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
Packit |
1c1d7e |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** This file is part of the QtCore module of the Qt Toolkit.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** $QT_BEGIN_LICENSE:LGPL$
|
|
Packit |
1c1d7e |
** No Commercial Usage
|
|
Packit |
1c1d7e |
** This file contains pre-release code and may not be distributed.
|
|
Packit |
1c1d7e |
** You may use this file in accordance with the terms and conditions
|
|
Packit |
1c1d7e |
** contained in the either Technology Preview License Agreement or the
|
|
Packit |
1c1d7e |
** Beta Release License Agreement.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** GNU Lesser General Public License Usage
|
|
Packit |
1c1d7e |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
Packit |
1c1d7e |
** General Public License version 2.1 as published by the Free Software
|
|
Packit |
1c1d7e |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
Packit |
1c1d7e |
** packaging of this file. Please review the following information to
|
|
Packit |
1c1d7e |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
Packit |
1c1d7e |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** In addition, as a special exception, Nokia gives you certain
|
|
Packit |
1c1d7e |
** additional rights. These rights are described in the Nokia Qt LGPL
|
|
Packit |
1c1d7e |
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
Packit |
1c1d7e |
** package.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** GNU General Public License Usage
|
|
Packit |
1c1d7e |
** Alternatively, this file may be used under the terms of the GNU
|
|
Packit |
1c1d7e |
** General Public License version 3.0 as published by the Free Software
|
|
Packit |
1c1d7e |
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
Packit |
1c1d7e |
** packaging of this file. Please review the following information to
|
|
Packit |
1c1d7e |
** ensure the GNU General Public License version 3.0 requirements will be
|
|
Packit |
1c1d7e |
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** If you are unsure which license is appropriate for your use, please
|
|
Packit |
1c1d7e |
** contact the sales department at http://www.qtsoftware.com/contact.
|
|
Packit |
1c1d7e |
** $QT_END_LICENSE$
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
****************************************************************************/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
#include "qwaitcondition.h"
|
|
Packit |
1c1d7e |
#include "qmutex.h"
|
|
Packit |
1c1d7e |
#include <pthread.h>
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
static void report_error(int code, const char *where, const char *what)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
if (code != 0)
|
|
Packit |
1c1d7e |
qWarning("%s: %s failure: %d", where, what, code);
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
class QWaitConditionPrivate
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
public:
|
|
Packit |
1c1d7e |
pthread_mutex_t mutex;
|
|
Packit |
1c1d7e |
pthread_cond_t cond;
|
|
Packit |
1c1d7e |
int waiters;
|
|
Packit |
1c1d7e |
int wakeups;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void wait()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
int code;
|
|
Packit |
1c1d7e |
for (;;)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
code = pthread_cond_wait(&cond, &mutex);
|
|
Packit |
1c1d7e |
if (code == 0 && wakeups == 0)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
// many vendors warn of spurious wakeups from
|
|
Packit |
1c1d7e |
// pthread_cond_wait(), especially after signal delivery,
|
|
Packit |
1c1d7e |
// even though POSIX doesn't allow for it... sigh
|
|
Packit |
1c1d7e |
continue;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
break;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
--waiters;
|
|
Packit |
1c1d7e |
if (code == 0)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
--wakeups;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
else
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
report_error(code, "QWaitCondition::wait()", "cv wait");
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_unlock(&mutex), "QWaitCondition::wait()", "mutex unlock");
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QWaitCondition::QWaitCondition()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
d = new QWaitConditionPrivate;
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_init(&d->mutex, NULL), "QWaitCondition", "mutex init");
|
|
Packit |
1c1d7e |
report_error(pthread_cond_init(&d->cond, NULL), "QWaitCondition", "cv init");
|
|
Packit |
1c1d7e |
d->waiters = d->wakeups = 0;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QWaitCondition::~QWaitCondition()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
report_error(pthread_cond_destroy(&d->cond), "QWaitCondition", "cv destroy");
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_destroy(&d->mutex), "QWaitCondition", "mutex destroy");
|
|
Packit |
1c1d7e |
delete d;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QWaitCondition::wakeOne()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeOne()", "mutex lock");
|
|
Packit |
1c1d7e |
d->wakeups = MIN(d->wakeups + 1, d->waiters);
|
|
Packit |
1c1d7e |
report_error(pthread_cond_signal(&d->cond), "QWaitCondition::wakeOne()", "cv signal");
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeOne()", "mutex unlock");
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QWaitCondition::wakeAll()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeAll()", "mutex lock");
|
|
Packit |
1c1d7e |
d->wakeups = d->waiters;
|
|
Packit |
1c1d7e |
report_error(pthread_cond_broadcast(&d->cond), "QWaitCondition::wakeAll()", "cv broadcast");
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeAll()", "mutex unlock");
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QWaitCondition::wait(QMutex *mutex)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
if (!mutex) return;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock");
|
|
Packit |
1c1d7e |
++d->waiters;
|
|
Packit |
1c1d7e |
mutex->unlock();
|
|
Packit |
1c1d7e |
d->wait();
|
|
Packit |
1c1d7e |
mutex->lock();
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|