"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/qtsingleapplication/src/qtlockedfile_win.cpp" (12 Feb 2021, 6701 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "qtlockedfile_win.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /****************************************************************************
    2 **
    3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
    4 ** Contact: http://www.qt-project.org/legal
    5 **
    6 ** This file is part of the Qt Solutions component.
    7 **
    8 ** $QT_BEGIN_LICENSE:BSD$
    9 ** You may use this file under the terms of the BSD license as follows:
   10 **
   11 ** "Redistribution and use in source and binary forms, with or without
   12 ** modification, are permitted provided that the following conditions are
   13 ** met:
   14 **   * Redistributions of source code must retain the above copyright
   15 **     notice, this list of conditions and the following disclaimer.
   16 **   * Redistributions in binary form must reproduce the above copyright
   17 **     notice, this list of conditions and the following disclaimer in
   18 **     the documentation and/or other materials provided with the
   19 **     distribution.
   20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
   21 **     of its contributors may be used to endorse or promote products derived
   22 **     from this software without specific prior written permission.
   23 **
   24 **
   25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
   36 **
   37 ** $QT_END_LICENSE$
   38 **
   39 ****************************************************************************/
   40 
   41 #include "qtlockedfile.h"
   42 #include <qt_windows.h>
   43 #include <QFileInfo>
   44 
   45 #define MUTEX_PREFIX "QtLockedFile mutex "
   46 // Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
   47 #define MAX_READERS MAXIMUM_WAIT_OBJECTS
   48 
   49 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
   50 #define QT_WA(unicode, ansi) unicode
   51 #endif
   52 
   53 Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
   54 {
   55     if (mutexname.isEmpty())
   56     {
   57         QFileInfo fi(*this);
   58         mutexname = QString::fromLatin1(MUTEX_PREFIX)
   59                     + fi.absoluteFilePath().toLower();
   60     }
   61     QString mname(mutexname);
   62     if (idx >= 0)
   63         mname += QString::number(idx);
   64 
   65     Qt::HANDLE mutex;
   66     if (doCreate)
   67     {
   68         QT_WA({ mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16()); },
   69         { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); });
   70         if (!mutex)
   71         {
   72             qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
   73             return 0;
   74         }
   75     }
   76     else
   77     {
   78         QT_WA({ mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16()); },
   79         { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); });
   80         if (!mutex)
   81         {
   82             if (GetLastError() != ERROR_FILE_NOT_FOUND)
   83                 qErrnoWarning("QtLockedFile::lock(): OpenMutex failed");
   84             return 0;
   85         }
   86     }
   87     return mutex;
   88 }
   89 
   90 bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
   91 {
   92     Q_ASSERT(mutex);
   93     DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0);
   94     switch (res)
   95     {
   96     case WAIT_OBJECT_0:
   97     case WAIT_ABANDONED:
   98         return true;
   99         break;
  100     case WAIT_TIMEOUT:
  101         break;
  102     default:
  103         qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
  104     }
  105     return false;
  106 }
  107 
  108 
  109 
  110 bool QtLockedFile::lock(LockMode mode, bool block)
  111 {
  112     if (!isOpen())
  113     {
  114         qWarning("QtLockedFile::lock(): file is not opened");
  115         return false;
  116     }
  117 
  118     if (mode == NoLock)
  119         return unlock();
  120 
  121     if (mode == m_lock_mode)
  122         return true;
  123 
  124     if (m_lock_mode != NoLock)
  125         unlock();
  126 
  127     if (!wmutex && !(wmutex = getMutexHandle(-1, true)))
  128         return false;
  129 
  130     if (!waitMutex(wmutex, block))
  131         return false;
  132 
  133     if (mode == ReadLock)
  134     {
  135         int idx = 0;
  136         for (; idx < MAX_READERS; idx++)
  137         {
  138             rmutex = getMutexHandle(idx, false);
  139             if (!rmutex || waitMutex(rmutex, false))
  140                 break;
  141             CloseHandle(rmutex);
  142         }
  143         bool ok = true;
  144         if (idx >= MAX_READERS)
  145         {
  146             qWarning("QtLockedFile::lock(): too many readers");
  147             rmutex = 0;
  148             ok = false;
  149         }
  150         else if (!rmutex)
  151         {
  152             rmutex = getMutexHandle(idx, true);
  153             if (!rmutex || !waitMutex(rmutex, false))
  154                 ok = false;
  155         }
  156         if (!ok && rmutex)
  157         {
  158             CloseHandle(rmutex);
  159             rmutex = 0;
  160         }
  161         ReleaseMutex(wmutex);
  162         if (!ok)
  163             return false;
  164     }
  165     else
  166     {
  167         Q_ASSERT(rmutexes.isEmpty());
  168         for (int i = 0; i < MAX_READERS; i++)
  169         {
  170             Qt::HANDLE mutex = getMutexHandle(i, false);
  171             if (mutex)
  172                 rmutexes.append(mutex);
  173         }
  174         if (rmutexes.size())
  175         {
  176             DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),
  177                                                TRUE, block ? INFINITE : 0);
  178             if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED)
  179             {
  180                 if (res != WAIT_TIMEOUT)
  181                     qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed");
  182                 m_lock_mode = WriteLock;  // trick unlock() to clean up - semiyucky
  183                 unlock();
  184                 return false;
  185             }
  186         }
  187     }
  188 
  189     m_lock_mode = mode;
  190     return true;
  191 }
  192 
  193 bool QtLockedFile::unlock()
  194 {
  195     if (!isOpen())
  196     {
  197         qWarning("QtLockedFile::unlock(): file is not opened");
  198         return false;
  199     }
  200 
  201     if (!isLocked())
  202         return true;
  203 
  204     if (m_lock_mode == ReadLock)
  205     {
  206         ReleaseMutex(rmutex);
  207         CloseHandle(rmutex);
  208         rmutex = 0;
  209     }
  210     else
  211     {
  212         foreach (Qt::HANDLE mutex, rmutexes)
  213         {
  214             ReleaseMutex(mutex);
  215             CloseHandle(mutex);
  216         }
  217         rmutexes.clear();
  218         ReleaseMutex(wmutex);
  219     }
  220 
  221     m_lock_mode = QtLockedFile::NoLock;
  222     return true;
  223 }
  224 
  225 QtLockedFile::~QtLockedFile()
  226 {
  227     if (isOpen())
  228         unlock();
  229     if (wmutex)
  230         CloseHandle(wmutex);
  231 }