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