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 QtCore module 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 |
#include "qsystemsemaphore.h"
|
|
43 |
#include "qsystemsemaphore_p.h"
|
|
44 |
#include "qcoreapplication.h"
|
|
45 |
#include <qdebug.h>
|
|
46 |
|
|
47 |
QT_BEGIN_NAMESPACE
|
|
48 |
|
|
49 |
#ifndef QT_NO_SYSTEMSEMAPHORE
|
|
50 |
|
|
51 |
QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
|
|
52 |
semaphore(0), error(QSystemSemaphore::NoError)
|
|
53 |
{
|
|
54 |
}
|
|
55 |
|
|
56 |
void QSystemSemaphorePrivate::setErrorString(const QString &function)
|
|
57 |
{
|
|
58 |
BOOL windowsError = GetLastError();
|
|
59 |
if (windowsError == 0)
|
|
60 |
return;
|
|
61 |
|
|
62 |
switch (windowsError) {
|
|
63 |
case ERROR_NO_SYSTEM_RESOURCES:
|
|
64 |
case ERROR_NOT_ENOUGH_MEMORY:
|
|
65 |
error = QSystemSemaphore::OutOfResources;
|
|
66 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
|
|
67 |
break;
|
|
68 |
case ERROR_ACCESS_DENIED:
|
|
69 |
error = QSystemSemaphore::PermissionDenied;
|
|
70 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
|
|
71 |
break;
|
|
72 |
default:
|
|
73 |
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(windowsError);
|
|
74 |
error = QSystemSemaphore::UnknownError;
|
|
75 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
76 |
qDebug() << errorString << "key" << key;
|
|
77 |
#endif
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
HANDLE QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode)
|
|
82 |
{
|
|
83 |
// don't allow making handles on empty keys
|
|
84 |
if (key.isEmpty())
|
|
85 |
return 0;
|
|
86 |
|
|
87 |
// Create it if it doesn't already exists.
|
|
88 |
if (semaphore == 0) {
|
|
89 |
QString safeName = makeKeyFileName();
|
|
90 |
semaphore = CreateSemaphore(0, initialValue, MAXLONG, (wchar_t*)safeName.utf16());
|
|
91 |
if (semaphore == NULL)
|
|
92 |
setErrorString(QLatin1String("QSystemSemaphore::handle"));
|
|
93 |
}
|
|
94 |
|
|
95 |
return semaphore;
|
|
96 |
}
|
|
97 |
|
|
98 |
void QSystemSemaphorePrivate::cleanHandle()
|
|
99 |
{
|
|
100 |
if (semaphore && !CloseHandle(semaphore)) {
|
|
101 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
102 |
qDebug() << QLatin1String("QSystemSemaphorePrivate::CloseHandle: sem failed");
|
|
103 |
#endif
|
|
104 |
}
|
|
105 |
semaphore = 0;
|
|
106 |
}
|
|
107 |
|
|
108 |
bool QSystemSemaphorePrivate::modifySemaphore(int count)
|
|
109 |
{
|
|
110 |
if (0 == handle())
|
|
111 |
return false;
|
|
112 |
|
|
113 |
if (count > 0) {
|
|
114 |
if (0 == ReleaseSemaphore(semaphore, count, 0)) {
|
|
115 |
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
|
|
116 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
117 |
qDebug() << QLatin1String("QSystemSemaphore::modifySemaphore ReleaseSemaphore failed");
|
|
118 |
#endif
|
|
119 |
return false;
|
|
120 |
}
|
|
121 |
} else {
|
|
122 |
if (WAIT_OBJECT_0 != WaitForSingleObject(semaphore, INFINITE)) {
|
|
123 |
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
|
|
124 |
#if defined QSYSTEMSEMAPHORE_DEBUG
|
|
125 |
qDebug() << QLatin1String("QSystemSemaphore::modifySemaphore WaitForSingleObject failed");
|
|
126 |
#endif
|
|
127 |
return false;
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
return true;
|
|
132 |
}
|
|
133 |
|
|
134 |
#endif //QT_NO_SYSTEMSEMAPHORE
|
|
135 |
|
|
136 |
QT_END_NAMESPACE
|