| 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 >= 0x050000 |
| 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 | QFileInfo fi(*this); |
| 57 | mutexname = QString::fromLatin1(MUTEX_PREFIX) |
| 58 | + fi.absoluteFilePath().toLower(); |
| 59 | } |
| 60 | QString mname(mutexname); |
| 61 | if (idx >= 0) |
| 62 | mname += QString::number(idx); |
| 63 | |
| 64 | Qt::HANDLE mutex; |
| 65 | if (doCreate) { |
| 66 | QT_WA( { mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16()); }, |
| 67 | { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); } ); |
| 68 | if (!mutex) { |
| 69 | qErrnoWarning("QtLockedFile::lock(): CreateMutex failed"); |
| 70 | return 0; |
| 71 | } |
| 72 | } |
| 73 | else { |
| 74 | QT_WA( { mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16()); }, |
| 75 | { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); } ); |
| 76 | if (!mutex) { |
| 77 | if (GetLastError() != ERROR_FILE_NOT_FOUND) |
| 78 | qErrnoWarning("QtLockedFile::lock(): OpenMutex failed"); |
| 79 | return 0; |
| 80 | } |
| 81 | } |
| 82 | return mutex; |
| 83 | } |
| 84 | |
| 85 | bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock) |
| 86 | { |
| 87 | Q_ASSERT(mutex); |
| 88 | DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0); |
| 89 | switch (res) { |
| 90 | case WAIT_OBJECT_0: |
| 91 | case WAIT_ABANDONED: |
| 92 | return true; |
| 93 | break; |
| 94 | case WAIT_TIMEOUT: |
| 95 | break; |
| 96 | default: |
| 97 | qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed"); |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | bool QtLockedFile::lock(LockMode mode, bool block) |
| 105 | { |
| 106 | if (!isOpen()) { |
| 107 | qWarning("QtLockedFile::lock(): file is not opened"); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (mode == NoLock) |
| 112 | return unlock(); |
| 113 | |
| 114 | if (mode == m_lock_mode) |
| 115 | return true; |
| 116 | |
| 117 | if (m_lock_mode != NoLock) |
| 118 | unlock(); |
| 119 | |
| 120 | if (!wmutex && !(wmutex = getMutexHandle(-1, true))) |
| 121 | return false; |
| 122 | |
| 123 | if (!waitMutex(wmutex, block)) |
| 124 | return false; |
| 125 | |
| 126 | if (mode == ReadLock) { |
| 127 | int idx = 0; |
| 128 | for (; idx < MAX_READERS; idx++) { |
| 129 | rmutex = getMutexHandle(idx, false); |
| 130 | if (!rmutex || waitMutex(rmutex, false)) |
| 131 | break; |
| 132 | CloseHandle(rmutex); |
| 133 | } |
| 134 | bool ok = true; |
| 135 | if (idx >= MAX_READERS) { |
| 136 | qWarning("QtLockedFile::lock(): too many readers"); |
| 137 | rmutex = 0; |
| 138 | ok = false; |
| 139 | } |
| 140 | else if (!rmutex) { |
| 141 | rmutex = getMutexHandle(idx, true); |
| 142 | if (!rmutex || !waitMutex(rmutex, false)) |
| 143 | ok = false; |
| 144 | } |
| 145 | if (!ok && rmutex) { |
| 146 | CloseHandle(rmutex); |
| 147 | rmutex = 0; |
| 148 | } |
| 149 | ReleaseMutex(wmutex); |
| 150 | if (!ok) |
| 151 | return false; |
| 152 | } |
| 153 | else { |
| 154 | Q_ASSERT(rmutexes.isEmpty()); |
| 155 | for (int i = 0; i < MAX_READERS; i++) { |
| 156 | Qt::HANDLE mutex = getMutexHandle(i, false); |
| 157 | if (mutex) |
| 158 | rmutexes.append(mutex); |
| 159 | } |
| 160 | if (rmutexes.size()) { |
| 161 | DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(), |
| 162 | TRUE, block ? INFINITE : 0); |
| 163 | if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) { |
| 164 | if (res != WAIT_TIMEOUT) |
| 165 | qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed"); |
| 166 | m_lock_mode = WriteLock; // trick unlock() to clean up - semiyucky |
| 167 | unlock(); |
| 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | m_lock_mode = mode; |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | bool QtLockedFile::unlock() |
| 178 | { |
| 179 | if (!isOpen()) { |
| 180 | qWarning("QtLockedFile::unlock(): file is not opened"); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if (!isLocked()) |
| 185 | return true; |
| 186 | |
| 187 | if (m_lock_mode == ReadLock) { |
| 188 | ReleaseMutex(rmutex); |
| 189 | CloseHandle(rmutex); |
| 190 | rmutex = 0; |
| 191 | } |
| 192 | else { |
| 193 | foreach(Qt::HANDLE mutex, rmutexes) { |
| 194 | ReleaseMutex(mutex); |
| 195 | CloseHandle(mutex); |
| 196 | } |
| 197 | rmutexes.clear(); |
| 198 | ReleaseMutex(wmutex); |
| 199 | } |
| 200 | |
| 201 | m_lock_mode = QtLockedFile::NoLock; |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | QtLockedFile::~QtLockedFile() |
| 206 | { |
| 207 | if (isOpen()) |
| 208 | unlock(); |
| 209 | if (wmutex) |
| 210 | CloseHandle(wmutex); |
| 211 | } |