1 /****************************************************************************
2 **
3 ** This file is part of a Qt Solutions component.
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact:  Qt Software Information (qt-info@nokia.com)
8 **
9 ** Commercial Usage
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.
14 **
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.
22 **
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
26 ** package.
27 **
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.
35 **
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.
41 **
42 ** If you are unsure which license is appropriate for your use, please
43 ** contact the sales department at qt-sales@nokia.com.
44 **
45 ****************************************************************************/
46 
47 #include "qtlockedfile.h"
48 #include <qt_windows.h>
49 #include <QtCore/QFileInfo>
50 
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
54 
getMutexHandle(int idx,bool doCreate)55 Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
56 {
57     if (mutexname.isEmpty()) {
58         QFileInfo fi(*this);
59         mutexname = QString::fromLatin1(MUTEX_PREFIX)
60                     + fi.absoluteFilePath().toLower();
61     }
62     QString mname(mutexname);
63     if (idx >= 0)
64         mname += QString::number(idx);
65 
66     Qt::HANDLE mutex;
67     if (doCreate) {
68         mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16());
69         if (!mutex) {
70             qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
71             return 0;
72         }
73     }
74     else {
75         mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16());
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 
waitMutex(Qt::HANDLE mutex,bool doBlock)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 
lock(LockMode mode,bool block)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 
unlock()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 
~QtLockedFile()205 QtLockedFile::~QtLockedFile()
206 {
207     if (isOpen())
208         unlock();
209     if (wmutex)
210         CloseHandle(wmutex);
211 }
212