|
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 <stdio.h>
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
#include "qglobal.h"
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
#include "qmutex.h"
|
|
Packit |
1c1d7e |
#include "qmutex_p.h"
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QMutex::QMutex() : d(new QMutexPrivate())
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QMutex::~QMutex()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
delete d;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QMutex::lock()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
//printf("%p: QMutex::lock(): enter\n",this);
|
|
Packit |
1c1d7e |
bool isLocked;
|
|
Packit |
1c1d7e |
isLocked = d->contenders.testAndSet(0, 1);
|
|
Packit |
1c1d7e |
if (!isLocked)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
isLocked = d->contenders.fetchAndAdd(1)==0;
|
|
Packit |
1c1d7e |
if (!isLocked)
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
// didn't get the lock, wait for it
|
|
Packit |
1c1d7e |
//printf("%p: QMutex::lock(): wait() %d\n",this,(int)d->contenders);
|
|
Packit |
1c1d7e |
d->wait();
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// release lock
|
|
Packit |
1c1d7e |
d->contenders.fetchAndAdd(-1);
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
//printf("%p: QMutex::lock(): leave\n",this);
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
bool QMutex::tryLock()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
bool isLocked = d->contenders.testAndSet(0, 1);
|
|
Packit |
1c1d7e |
return isLocked;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void QMutex::unlock()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
//printf("%p: QMutex::unlock(): enter %d\n",this,(int)d->contenders);
|
|
Packit |
1c1d7e |
if (!d->contenders.testAndSet(1, 0))
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
//printf("%p: QMutex::unlock(): wakeUp()\n",this);
|
|
Packit |
1c1d7e |
d->wakeUp();
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
//printf("%p: QMutex::unlock(): leave\n",this);
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|