1 /****************************************************************************
3 ** This file is part of a Qt Solutions component.
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Qt Software Information (qt-info@nokia.com)
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Solutions Commercial License Agreement provided
12 ** with the Software or, alternatively, in accordance with the terms
13 ** contained in a written agreement between you and Nokia.
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** Please note Third Party Software included with Qt Solutions may impose
37 ** additional restrictions and it is the user's responsibility to ensure
38 ** that they have met the licensing requirements of the GPL, LGPL, or Qt
39 ** Solutions Commercial license and the relevant license of the Third
40 ** Party Software they are using.
42 ** If you are unsure which license is appropriate for your use, please
43 ** contact the sales department at qt-sales@nokia.com.
45 ****************************************************************************/
47 #include "qtlockedfile.h"
48 #include <qt_windows.h>
49 #include <QtCore/QFileInfo>
51 #define MUTEX_PREFIX "QtLockedFile mutex "
52 // Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
53 #define MAX_READERS MAXIMUM_WAIT_OBJECTS
55 Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
57 if (mutexname.isEmpty()) {
59 mutexname = QString::fromLatin1(MUTEX_PREFIX)
60 + fi.absoluteFilePath().toLower();
62 QString mname(mutexname);
64 mname += QString::number(idx);
68 QT_WA( { mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16()); },
69 { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); } );
71 qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
76 QT_WA( { mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16()); },
77 { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); } );
79 if (GetLastError() != ERROR_FILE_NOT_FOUND)
80 qErrnoWarning("QtLockedFile::lock(): OpenMutex failed");
87 bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
90 DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0);
99 qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
106 bool QtLockedFile::lock(LockMode mode, bool block)
109 qWarning("QtLockedFile::lock(): file is not opened");
116 if (mode == m_lock_mode)
119 if (m_lock_mode != NoLock)
122 if (!wmutex && !(wmutex = getMutexHandle(-1, true)))
125 if (!waitMutex(wmutex, block))
128 if (mode == ReadLock) {
130 for (; idx < MAX_READERS; idx++) {
131 rmutex = getMutexHandle(idx, false);
132 if (!rmutex || waitMutex(rmutex, false))
137 if (idx >= MAX_READERS) {
138 qWarning("QtLockedFile::lock(): too many readers");
143 rmutex = getMutexHandle(idx, true);
144 if (!rmutex || !waitMutex(rmutex, false))
151 ReleaseMutex(wmutex);
156 Q_ASSERT(rmutexes.isEmpty());
157 for (int i = 0; i < MAX_READERS; i++) {
158 Qt::HANDLE mutex = getMutexHandle(i, false);
160 rmutexes.append(mutex);
162 if (rmutexes.size()) {
163 DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),
164 TRUE, block ? INFINITE : 0);
165 if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) {
166 if (res != WAIT_TIMEOUT)
167 qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed");
168 m_lock_mode = WriteLock; // trick unlock() to clean up - semiyucky
179 bool QtLockedFile::unlock()
182 qWarning("QtLockedFile::unlock(): file is not opened");
189 if (m_lock_mode == ReadLock) {
190 ReleaseMutex(rmutex);
195 foreach(Qt::HANDLE mutex, rmutexes) {
200 ReleaseMutex(wmutex);
203 m_lock_mode = QtLockedFile::NoLock;
207 QtLockedFile::~QtLockedFile()