1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qsystemsemaphore.h"
41 #include "qsystemsemaphore_p.h"
42 #include "qcoreapplication.h"
43 #include <qdebug.h>
44 #include <qt_windows.h>
45
46 QT_BEGIN_NAMESPACE
47
48 #ifndef QT_NO_SYSTEMSEMAPHORE
49
QSystemSemaphorePrivate()50 QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
51 semaphore(0), error(QSystemSemaphore::NoError)
52 {
53 }
54
setErrorString(const QString & function)55 void QSystemSemaphorePrivate::setErrorString(const QString &function)
56 {
57 BOOL windowsError = GetLastError();
58 if (windowsError == 0)
59 return;
60
61 switch (windowsError) {
62 case ERROR_NO_SYSTEM_RESOURCES:
63 case ERROR_NOT_ENOUGH_MEMORY:
64 error = QSystemSemaphore::OutOfResources;
65 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
66 break;
67 case ERROR_ACCESS_DENIED:
68 error = QSystemSemaphore::PermissionDenied;
69 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
70 break;
71 default:
72 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(windowsError);
73 error = QSystemSemaphore::UnknownError;
74 #if defined QSYSTEMSEMAPHORE_DEBUG
75 qDebug() << errorString << "key" << key;
76 #endif
77 }
78 }
79
handle(QSystemSemaphore::AccessMode)80 HANDLE QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode)
81 {
82 // don't allow making handles on empty keys
83 if (key.isEmpty())
84 return 0;
85
86 // Create it if it doesn't already exists.
87 if (semaphore == 0) {
88 #if defined(Q_OS_WINRT)
89 semaphore = CreateSemaphoreEx(0, initialValue, MAXLONG,
90 reinterpret_cast<const wchar_t*>(fileName.utf16()),
91 0, SEMAPHORE_ALL_ACCESS);
92 #else
93 semaphore = CreateSemaphore(0, initialValue, MAXLONG,
94 reinterpret_cast<const wchar_t*>(fileName.utf16()));
95 #endif
96 if (semaphore == NULL)
97 setErrorString(QLatin1String("QSystemSemaphore::handle"));
98 }
99
100 return semaphore;
101 }
102
cleanHandle()103 void QSystemSemaphorePrivate::cleanHandle()
104 {
105 if (semaphore && !CloseHandle(semaphore)) {
106 #if defined QSYSTEMSEMAPHORE_DEBUG
107 qDebug("QSystemSemaphorePrivate::CloseHandle: sem failed");
108 #endif
109 }
110 semaphore = 0;
111 }
112
modifySemaphore(int count)113 bool QSystemSemaphorePrivate::modifySemaphore(int count)
114 {
115 if (0 == handle())
116 return false;
117
118 if (count > 0) {
119 if (0 == ReleaseSemaphore(semaphore, count, 0)) {
120 setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
121 #if defined QSYSTEMSEMAPHORE_DEBUG
122 qDebug("QSystemSemaphore::modifySemaphore ReleaseSemaphore failed");
123 #endif
124 return false;
125 }
126 } else {
127 if (WAIT_OBJECT_0 != WaitForSingleObjectEx(semaphore, INFINITE, FALSE)) {
128 setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
129 #if defined QSYSTEMSEMAPHORE_DEBUG
130 qDebug("QSystemSemaphore::modifySemaphore WaitForSingleObject failed");
131 #endif
132 return false;
133 }
134 }
135
136 clearError();
137 return true;
138 }
139
140 #endif //QT_NO_SYSTEMSEMAPHORE
141
142 QT_END_NAMESPACE
143