Blame qtools/qmutex_win32.cpp

Packit Service 50c9f2
/****************************************************************************
Packit Service 50c9f2
**
Packit Service 50c9f2
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
Packit Service 50c9f2
** Contact: Nokia Corporation (qt-info@nokia.com)
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is part of the QtCore module of the Qt Toolkit.
Packit Service 50c9f2
**
Packit Service 50c9f2
** $QT_BEGIN_LICENSE:LGPL$
Packit Service 50c9f2
** No Commercial Usage
Packit Service 50c9f2
** This file contains pre-release code and may not be distributed.
Packit Service 50c9f2
** You may use this file in accordance with the terms and conditions
Packit Service 50c9f2
** contained in the either Technology Preview License Agreement or the
Packit Service 50c9f2
** Beta Release License Agreement.
Packit Service 50c9f2
**
Packit Service 50c9f2
** GNU Lesser General Public License Usage
Packit Service 50c9f2
** Alternatively, this file may be used under the terms of the GNU Lesser
Packit Service 50c9f2
** General Public License version 2.1 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.LGPL included in the
Packit Service 50c9f2
** packaging of this file.  Please review the following information to
Packit Service 50c9f2
** ensure the GNU Lesser General Public License version 2.1 requirements
Packit Service 50c9f2
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Packit Service 50c9f2
**
Packit Service 50c9f2
** In addition, as a special exception, Nokia gives you certain
Packit Service 50c9f2
** additional rights. These rights are described in the Nokia Qt LGPL
Packit Service 50c9f2
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
Packit Service 50c9f2
** package.
Packit Service 50c9f2
**
Packit Service 50c9f2
** GNU General Public License Usage
Packit Service 50c9f2
** Alternatively, this file may be used under the terms of the GNU
Packit Service 50c9f2
** General Public License version 3.0 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.GPL included in the
Packit Service 50c9f2
** packaging of this file.  Please review the following information to
Packit Service 50c9f2
** ensure the GNU General Public License version 3.0 requirements will be
Packit Service 50c9f2
** met: http://www.gnu.org/copyleft/gpl.html.
Packit Service 50c9f2
**
Packit Service 50c9f2
** If you are unsure which license is appropriate for your use, please
Packit Service 50c9f2
** contact the sales department at http://www.qtsoftware.com/contact.
Packit Service 50c9f2
** $QT_END_LICENSE$
Packit Service 50c9f2
**
Packit Service 50c9f2
****************************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
#include <windows.h>
Packit Service 50c9f2
Packit Service 50c9f2
#include "qmutex.h"
Packit Service 50c9f2
#include "qmutex_p.h"
Packit Service 50c9f2
Packit Service 50c9f2
QMutexPrivate::QMutexPrivate()
Packit Service 50c9f2
    : contenders(0)
Packit Service 50c9f2
{
Packit Service 50c9f2
  event = CreateEvent(0, FALSE, FALSE, 0);
Packit Service 50c9f2
  if (!event)
Packit Service 50c9f2
    qWarning("QMutexPrivate::QMutexPrivate: Cannot create event");
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
QMutexPrivate::~QMutexPrivate()
Packit Service 50c9f2
{
Packit Service 50c9f2
  CloseHandle(event);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
void QMutexPrivate::wait()
Packit Service 50c9f2
{
Packit Service 50c9f2
  WaitForSingleObject(event, INFINITE);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
void QMutexPrivate::wakeUp()
Packit Service 50c9f2
{
Packit Service 50c9f2
  SetEvent(event);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
//----------------------------------------------------------------------
Packit Service 50c9f2
Packit Service 50c9f2
class QCriticalSection
Packit Service 50c9f2
{
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    QCriticalSection()  { InitializeCriticalSection(&section); }
Packit Service 50c9f2
    ~QCriticalSection() { DeleteCriticalSection(&section); }
Packit Service 50c9f2
    void lock()         { EnterCriticalSection(&section); }
Packit Service 50c9f2
    void unlock()       { LeaveCriticalSection(&section); }
Packit Service 50c9f2
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    CRITICAL_SECTION section;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
static QCriticalSection qAtomicCriticalSection;
Packit Service 50c9f2
Packit Service 50c9f2
bool QAtomicInt::testAndSet(int expectedValue,int newValue)
Packit Service 50c9f2
{
Packit Service 50c9f2
  bool returnValue = false;
Packit Service 50c9f2
  qAtomicCriticalSection.lock();
Packit Service 50c9f2
  if (m_value == expectedValue)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    m_value = newValue;
Packit Service 50c9f2
    returnValue = true;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  qAtomicCriticalSection.unlock();
Packit Service 50c9f2
  return returnValue;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
int QAtomicInt::fetchAndAdd(int valueToAdd)
Packit Service 50c9f2
{
Packit Service 50c9f2
  int returnValue;
Packit Service 50c9f2
  qAtomicCriticalSection.lock();
Packit Service 50c9f2
  returnValue = m_value;
Packit Service 50c9f2
  m_value += valueToAdd;
Packit Service 50c9f2
  qAtomicCriticalSection.unlock();
Packit Service 50c9f2
  return returnValue;
Packit Service 50c9f2
}
Packit Service 50c9f2