0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include "qsystemlock.h"
|
|
44 |
#include "qsystemlock_p.h"
|
|
45 |
#include <qdebug.h>
|
|
46 |
#include <QtCore>
|
|
47 |
QSystemLockPrivate::QSystemLockPrivate() :
|
|
48 |
semaphore(0), semaphoreLock(0),
|
|
49 |
lockCount(0), error(QSystemLock::NoError)
|
|
50 |
{
|
|
51 |
}
|
|
52 |
|
|
53 |
void QSystemLockPrivate::setErrorString(const QString &function)
|
|
54 |
{
|
|
55 |
BOOL windowsError = GetLastError();
|
|
56 |
if (windowsError == 0)
|
|
57 |
return;
|
|
58 |
errorString = function + QLatin1String(": ")
|
|
59 |
+ QLatin1String("Unknown error");
|
|
60 |
error = QSystemLock::UnknownError;
|
|
61 |
qWarning() << errorString << "key" << key << (int)windowsError << semaphore << semaphoreLock;
|
|
62 |
}
|
|
63 |
|
|
64 |
/*!
|
|
65 |
\internal
|
|
66 |
|
|
67 |
Setup the semaphore
|
|
68 |
*/
|
|
69 |
HANDLE QSystemLockPrivate::handle()
|
|
70 |
{
|
|
71 |
// don't allow making handles on empty keys
|
|
72 |
if (key.isEmpty())
|
|
73 |
return 0;
|
|
74 |
|
|
75 |
// Create it if it doesn't already exists.
|
|
76 |
if (semaphore == 0) {
|
|
77 |
QString safeName = makeKeyFileName();
|
|
78 |
semaphore = CreateSemaphore(0, MAX_LOCKS, MAX_LOCKS, (wchar_t*)safeName.utf16());
|
|
79 |
|
|
80 |
if (semaphore == 0) {
|
|
81 |
setErrorString(QLatin1String("QSystemLockPrivate::handle"));
|
|
82 |
return 0;
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
if (semaphoreLock == 0) {
|
|
87 |
QString safeLockName = QSharedMemoryPrivate::makePlatformSafeKey(key + QLatin1String("lock"), QLatin1String("qipc_systemlock_"));
|
|
88 |
semaphoreLock = CreateSemaphore(0, 1, 1, (wchar_t*)safeLockName.utf16());
|
|
89 |
|
|
90 |
if (semaphoreLock == 0) {
|
|
91 |
setErrorString(QLatin1String("QSystemLockPrivate::handle"));
|
|
92 |
return 0;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
return semaphore;
|
|
97 |
}
|
|
98 |
|
|
99 |
/*!
|
|
100 |
\internal
|
|
101 |
|
|
102 |
Cleanup the semaphore
|
|
103 |
*/
|
|
104 |
void QSystemLockPrivate::cleanHandle()
|
|
105 |
{
|
|
106 |
if (semaphore && !CloseHandle(semaphore))
|
|
107 |
setErrorString(QLatin1String("QSystemLockPrivate::cleanHandle:"));
|
|
108 |
if (semaphoreLock && !CloseHandle(semaphoreLock))
|
|
109 |
setErrorString(QLatin1String("QSystemLockPrivate::cleanHandle:"));
|
|
110 |
semaphore = 0;
|
|
111 |
semaphoreLock = 0;
|
|
112 |
}
|
|
113 |
|
|
114 |
bool QSystemLockPrivate::lock(HANDLE handle, int count)
|
|
115 |
{
|
|
116 |
if (count == 1) {
|
|
117 |
WaitForSingleObject(handle, INFINITE);
|
|
118 |
return true;
|
|
119 |
}
|
|
120 |
|
|
121 |
int i = count;
|
|
122 |
while (i > 0) {
|
|
123 |
if (WAIT_OBJECT_0 == WaitForSingleObject(handle, 0)) {
|
|
124 |
--i;
|
|
125 |
} else {
|
|
126 |
// undo what we have done, sleep and then try again later
|
|
127 |
ReleaseSemaphore(handle, (count - i), 0);
|
|
128 |
i = count;
|
|
129 |
ReleaseSemaphore(semaphoreLock, 1, 0);
|
|
130 |
Sleep(1);
|
|
131 |
WaitForSingleObject(semaphoreLock, INFINITE);
|
|
132 |
}
|
|
133 |
}
|
|
134 |
return true;
|
|
135 |
}
|
|
136 |
|
|
137 |
bool QSystemLockPrivate::unlock(HANDLE handle, int count)
|
|
138 |
{
|
|
139 |
if (0 == ReleaseSemaphore(handle, count, 0)) {
|
|
140 |
setErrorString(QLatin1String("QSystemLockPrivate::unlock"));
|
|
141 |
return false;
|
|
142 |
}
|
|
143 |
return true;
|
|
144 |
}
|
|
145 |
|
|
146 |
/*!
|
|
147 |
\internal
|
|
148 |
|
|
149 |
modifySemaphore handles recursive behavior and modifies the semaphore.
|
|
150 |
*/
|
|
151 |
bool QSystemLockPrivate::modifySemaphore(QSystemLockPrivate::Operation op,
|
|
152 |
QSystemLock::LockMode mode)
|
|
153 |
{
|
|
154 |
if (0 == handle())
|
|
155 |
return false;
|
|
156 |
|
|
157 |
if ((lockCount == 0 && op == Lock) || (lockCount > 0 && op == Unlock)) {
|
|
158 |
if (op == Unlock) {
|
|
159 |
--lockCount;
|
|
160 |
Q_ASSERT(lockCount >= 0);
|
|
161 |
if (lockCount > 0)
|
|
162 |
return true;
|
|
163 |
}
|
|
164 |
|
|
165 |
int count = (mode == QSystemLock::ReadWrite) ? MAX_LOCKS : 1;
|
|
166 |
if (op == Lock) {
|
|
167 |
lock(semaphoreLock, 1);
|
|
168 |
lock(semaphore, count);
|
|
169 |
if (count != MAX_LOCKS) unlock(semaphoreLock, 1);
|
|
170 |
lockedMode = mode;
|
|
171 |
} else {
|
|
172 |
if (count == MAX_LOCKS) unlock(semaphoreLock, 1);
|
|
173 |
unlock(semaphore, count);
|
|
174 |
}
|
|
175 |
|
|
176 |
}
|
|
177 |
if (op == Lock)
|
|
178 |
lockCount++;
|
|
179 |
|
|
180 |
return true;
|
|
181 |
}
|
|
182 |
|