File indexing completed on 2024-06-02 05:01:49

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
0004 ** Contact: http://www.qt-project.org/legal
0005 **
0006 ** This file is part of the Qt Solutions component.
0007 **
0008 ** $QT_BEGIN_LICENSE:BSD$
0009 ** You may use this file under the terms of the BSD license as follows:
0010 **
0011 ** "Redistribution and use in source and binary forms, with or without
0012 ** modification, are permitted provided that the following conditions are
0013 ** met:
0014 **   * Redistributions of source code must retain the above copyright
0015 **     notice, this list of conditions and the following disclaimer.
0016 **   * Redistributions in binary form must reproduce the above copyright
0017 **     notice, this list of conditions and the following disclaimer in
0018 **     the documentation and/or other materials provided with the
0019 **     distribution.
0020 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
0021 **     of its contributors may be used to endorse or promote products derived
0022 **     from this software without specific prior written permission.
0023 **
0024 **
0025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
0036 **
0037 ** $QT_END_LICENSE$
0038 **
0039 ****************************************************************************/
0040 
0041 #ifndef QTLOCKEDFILE_H
0042 #define QTLOCKEDFILE_H
0043 
0044 #include <QFile>
0045 #ifdef Q_OS_WIN
0046 #include <QVector>
0047 #endif
0048 
0049 #if defined(Q_OS_WIN)
0050 #  if !defined(QT_QTLOCKEDFILE_EXPORT) && !defined(QT_QTLOCKEDFILE_IMPORT)
0051 #    define QT_QTLOCKEDFILE_EXPORT
0052 #  elif defined(QT_QTLOCKEDFILE_IMPORT)
0053 #    if defined(QT_QTLOCKEDFILE_EXPORT)
0054 #      undef QT_QTLOCKEDFILE_EXPORT
0055 #    endif
0056 #    define QT_QTLOCKEDFILE_EXPORT __declspec(dllimport)
0057 #  elif defined(QT_QTLOCKEDFILE_EXPORT)
0058 #    undef QT_QTLOCKEDFILE_EXPORT
0059 #    define QT_QTLOCKEDFILE_EXPORT __declspec(dllexport)
0060 #  endif
0061 #else
0062 #  define QT_QTLOCKEDFILE_EXPORT
0063 #endif
0064 
0065 namespace QtLP_Private {
0066 
0067 class QT_QTLOCKEDFILE_EXPORT QtLockedFile : public QFile
0068 {
0069 public:
0070     enum LockMode { NoLock = 0, ReadLock, WriteLock };
0071 
0072     QtLockedFile();
0073     QtLockedFile(const QString &name);
0074     ~QtLockedFile() override;
0075 
0076     bool open(OpenMode mode) override;
0077 
0078     bool lock(LockMode mode, bool block = true);
0079     bool unlock();
0080     bool isLocked() const;
0081     LockMode lockMode() const;
0082 
0083 private:
0084 #ifdef Q_OS_WIN
0085     Qt::HANDLE wmutex;
0086     Qt::HANDLE rmutex;
0087     QVector<Qt::HANDLE> rmutexes;
0088     QString mutexname;
0089 
0090     Qt::HANDLE getMutexHandle(int idx, bool doCreate);
0091     bool waitMutex(Qt::HANDLE mutex, bool doBlock);
0092 
0093 #endif
0094     LockMode m_lock_mode;
0095 };
0096 }
0097 #endif