LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / shared / qtlockedfile / qtlockedfile_win.cpp
blob7ad4790123342aeb1bd0bdd15ac13c0fba95debc
1 /**
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.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
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
22 * for more details.
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
36 #endif
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)
45 QString result;
46 char *data = 0;
48 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
49 0, errorCode, 0,
50 (char *)&data, 0, 0);
51 result = QString::fromLocal8Bit(data);
52 if (data != 0) {
53 LocalFree(data);
56 if (result.endsWith("\n")) {
57 result.truncate(result.length() - 1);
60 return result;
63 bool QtLockedFile::lock(LockMode mode, bool block)
65 if (!isOpen()) {
66 qWarning("QtLockedFile::lock(): file is not opened");
67 return false;
70 if (mode == m_lock_mode) {
71 return true;
74 if (m_lock_mode != 0) {
75 unlock();
78 if (m_semaphore_hnd == 0) {
79 QFileInfo fi(*this);
80 QString sem_name = QString::fromLatin1(SEMAPHORE_PREFIX)
81 + fi.absoluteFilePath().toLower();
83 QT_WA({
84 m_semaphore_hnd = CreateSemaphoreW(0, SEMAPHORE_MAX, SEMAPHORE_MAX,
85 (TCHAR *)sem_name.utf16());
86 }, {
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());
95 return false;
99 bool gotMutex = false;
100 int decrement;
101 if (mode == ReadLock) {
102 decrement = 1;
103 } else {
104 decrement = SEMAPHORE_MAX;
105 if (m_mutex_hnd == 0) {
106 QFileInfo fi(*this);
107 QString mut_name = QString::fromLatin1(MUTEX_PREFIX)
108 + fi.absoluteFilePath().toLower();
109 QT_WA({
110 m_mutex_hnd = CreateMutexW(NULL, FALSE, (TCHAR *)mut_name.utf16());
111 }, {
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());
119 return false;
122 DWORD res = WaitForSingleObject(m_mutex_hnd, block ? INFINITE : 0);
123 if (res == WAIT_TIMEOUT) {
124 return false;
126 if (res == WAIT_FAILED) {
127 qWarning("QtLockedFile::lock(): WaitForSingleObject (mutex): %s",
128 errorCodeToString(GetLastError()).toLatin1().constData());
129 return false;
131 gotMutex = true;
134 for (int i = 0; i < decrement; ++i) {
135 DWORD res = WaitForSingleObject(m_semaphore_hnd, block ? INFINITE : 0);
136 if (res == WAIT_TIMEOUT) {
137 if (i) {
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());
142 // Fall through
145 if (gotMutex) {
146 ReleaseMutex(m_mutex_hnd);
148 return false;
150 if (res != WAIT_OBJECT_0) {
151 if (gotMutex) {
152 ReleaseMutex(m_mutex_hnd);
154 qWarning("QtLockedFile::lock(): WaitForSingleObject (semaphore): %s",
155 errorCodeToString(GetLastError()).toLatin1().constData());
156 return false;
160 m_lock_mode = mode;
161 if (gotMutex) {
162 ReleaseMutex(m_mutex_hnd);
164 return true;
167 bool QtLockedFile::unlock()
169 if (!isOpen()) {
170 qWarning("QtLockedFile::unlock(): file is not opened");
171 return false;
174 if (!isLocked()) {
175 return true;
178 int increment;
179 if (m_lock_mode == ReadLock) {
180 increment = 1;
181 } else {
182 increment = SEMAPHORE_MAX;
185 DWORD ret = ReleaseSemaphore(m_semaphore_hnd, increment, 0);
186 if (ret == 0) {
187 qWarning("QtLockedFile::unlock(): ReleaseSemaphore: %s",
188 errorCodeToString(GetLastError()).toLatin1().constData());
189 return false;
192 m_lock_mode = QtLockedFile::NoLock;
193 return true;
196 QtLockedFile::~QtLockedFile()
198 if (isOpen()) {
199 unlock();
201 if (m_mutex_hnd != 0) {
202 DWORD ret = CloseHandle(m_mutex_hnd);
203 if (ret == 0) {
204 qWarning("QtLockedFile::~QtLockedFile(): CloseHandle (mutex): %s",
205 errorCodeToString(GetLastError()).toLatin1().constData());
207 m_mutex_hnd = 0;
209 if (m_semaphore_hnd != 0) {
210 DWORD ret = CloseHandle(m_semaphore_hnd);
211 if (ret == 0) {
212 qWarning("QtLockedFile::~QtLockedFile(): CloseHandle (semaphore): %s",
213 errorCodeToString(GetLastError()).toLatin1().constData());
215 m_semaphore_hnd = 0;
218 } // namespace SharedTools