2 ******************************************************************************
4 * @file qtlockedfile_win.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "qtlockedfile.h"
31 #include <qt_windows.h>
32 #include <QtCore/QFileInfo>
34 #if QT_VERSION >= 0x050000
35 #define QT_WA(unicode, ansi) unicode
38 namespace SharedTools
{
39 #define SEMAPHORE_PREFIX "QtLockedFile semaphore "
40 #define MUTEX_PREFIX "QtLockedFile mutex "
41 #define SEMAPHORE_MAX 100
43 static QString
errorCodeToString(DWORD errorCode
)
48 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
51 result
= QString::fromLocal8Bit(data
);
56 if (result
.endsWith("\n")) {
57 result
.truncate(result
.length() - 1);
63 bool QtLockedFile::lock(LockMode mode
, bool block
)
66 qWarning("QtLockedFile::lock(): file is not opened");
70 if (mode
== m_lock_mode
) {
74 if (m_lock_mode
!= 0) {
78 if (m_semaphore_hnd
== 0) {
80 QString sem_name
= QString::fromLatin1(SEMAPHORE_PREFIX
)
81 + fi
.absoluteFilePath().toLower();
84 m_semaphore_hnd
= CreateSemaphoreW(0, SEMAPHORE_MAX
, SEMAPHORE_MAX
,
85 (TCHAR
*)sem_name
.utf16());
87 m_semaphore_hnd
= CreateSemaphoreA(0, SEMAPHORE_MAX
, SEMAPHORE_MAX
,
88 sem_name
.toLocal8Bit().constData());
92 if (m_semaphore_hnd
== 0) {
93 qWarning("QtLockedFile::lock(): CreateSemaphore: %s",
94 errorCodeToString(GetLastError()).toLatin1().constData());
99 bool gotMutex
= false;
101 if (mode
== ReadLock
) {
104 decrement
= SEMAPHORE_MAX
;
105 if (m_mutex_hnd
== 0) {
107 QString mut_name
= QString::fromLatin1(MUTEX_PREFIX
)
108 + fi
.absoluteFilePath().toLower();
110 m_mutex_hnd
= CreateMutexW(NULL
, FALSE
, (TCHAR
*)mut_name
.utf16());
112 m_mutex_hnd
= CreateMutexA(NULL
, FALSE
, mut_name
.toLocal8Bit().constData());
116 if (m_mutex_hnd
== 0) {
117 qWarning("QtLockedFile::lock(): CreateMutex: %s",
118 errorCodeToString(GetLastError()).toLatin1().constData());
122 DWORD res
= WaitForSingleObject(m_mutex_hnd
, block
? INFINITE
: 0);
123 if (res
== WAIT_TIMEOUT
) {
126 if (res
== WAIT_FAILED
) {
127 qWarning("QtLockedFile::lock(): WaitForSingleObject (mutex): %s",
128 errorCodeToString(GetLastError()).toLatin1().constData());
134 for (int i
= 0; i
< decrement
; ++i
) {
135 DWORD res
= WaitForSingleObject(m_semaphore_hnd
, block
? INFINITE
: 0);
136 if (res
== WAIT_TIMEOUT
) {
138 // A failed nonblocking rw locking. Undo changes to semaphore.
139 if (ReleaseSemaphore(m_semaphore_hnd
, i
, NULL
) == 0) {
140 qWarning("QtLockedFile::unlock(): ReleaseSemaphore: %s",
141 errorCodeToString(GetLastError()).toLatin1().constData());
146 ReleaseMutex(m_mutex_hnd
);
150 if (res
!= WAIT_OBJECT_0
) {
152 ReleaseMutex(m_mutex_hnd
);
154 qWarning("QtLockedFile::lock(): WaitForSingleObject (semaphore): %s",
155 errorCodeToString(GetLastError()).toLatin1().constData());
162 ReleaseMutex(m_mutex_hnd
);
167 bool QtLockedFile::unlock()
170 qWarning("QtLockedFile::unlock(): file is not opened");
179 if (m_lock_mode
== ReadLock
) {
182 increment
= SEMAPHORE_MAX
;
185 DWORD ret
= ReleaseSemaphore(m_semaphore_hnd
, increment
, 0);
187 qWarning("QtLockedFile::unlock(): ReleaseSemaphore: %s",
188 errorCodeToString(GetLastError()).toLatin1().constData());
192 m_lock_mode
= QtLockedFile::NoLock
;
196 QtLockedFile::~QtLockedFile()
201 if (m_mutex_hnd
!= 0) {
202 DWORD ret
= CloseHandle(m_mutex_hnd
);
204 qWarning("QtLockedFile::~QtLockedFile(): CloseHandle (mutex): %s",
205 errorCodeToString(GetLastError()).toLatin1().constData());
209 if (m_semaphore_hnd
!= 0) {
210 DWORD ret
= CloseHandle(m_semaphore_hnd
);
212 qWarning("QtLockedFile::~QtLockedFile(): CloseHandle (semaphore): %s",
213 errorCodeToString(GetLastError()).toLatin1().constData());
218 } // namespace SharedTools