Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsystemsemaphore_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
8#include <qdebug.h>
9#include <qt_windows.h>
10
12
13using namespace Qt::StringLiterals;
14
15#if QT_CONFIG(systemsemaphore)
16
17void QSystemSemaphorePrivate::setWindowsErrorString(QLatin1StringView function)
18{
19 BOOL windowsError = GetLastError();
20 if (windowsError == 0)
21 return;
22
23 switch (windowsError) {
24 case ERROR_NO_SYSTEM_RESOURCES:
25 case ERROR_NOT_ENOUGH_MEMORY:
26 error = QSystemSemaphore::OutOfResources;
27 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
28 break;
29 case ERROR_ACCESS_DENIED:
30 error = QSystemSemaphore::PermissionDenied;
31 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
32 break;
33 default:
34 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error: %2")
35 .arg(function, qt_error_string(windowsError));
36 error = QSystemSemaphore::UnknownError;
37#if defined QSYSTEMSEMAPHORE_DEBUG
38 qDebug() << errorString << "key" << key;
39#endif
40 }
41}
42
43HANDLE QSystemSemaphoreWin32::handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode)
44{
45 // don't allow making handles on empty keys
46 if (self->nativeKey.isEmpty())
47 return 0;
48
49 // Create it if it doesn't already exists.
50 if (semaphore == 0) {
51 semaphore = CreateSemaphore(0, self->initialValue, MAXLONG,
52 reinterpret_cast<const wchar_t*>(self->nativeKey.nativeKey().utf16()));
53 if (semaphore == NULL)
54 self->setWindowsErrorString("QSystemSemaphore::handle"_L1);
55 }
56
57 return semaphore;
58}
59
60void QSystemSemaphoreWin32::cleanHandle(QSystemSemaphorePrivate *)
61{
62 if (semaphore && !CloseHandle(semaphore)) {
63#if defined QSYSTEMSEMAPHORE_DEBUG
64 qDebug("QSystemSemaphoreWin32::CloseHandle: sem failed");
65#endif
66 }
67 semaphore = 0;
68}
69
70bool QSystemSemaphoreWin32::modifySemaphore(QSystemSemaphorePrivate *self, int count)
71{
72 if (handle(self, QSystemSemaphore::Open) == nullptr)
73 return false;
74
75 if (count > 0) {
76 if (0 == ReleaseSemaphore(semaphore, count, 0)) {
77 self->setWindowsErrorString("QSystemSemaphore::modifySemaphore"_L1);
78#if defined QSYSTEMSEMAPHORE_DEBUG
79 qDebug("QSystemSemaphore::modifySemaphore ReleaseSemaphore failed");
80#endif
81 return false;
82 }
83 } else {
84 if (WAIT_OBJECT_0 != WaitForSingleObjectEx(semaphore, INFINITE, FALSE)) {
85 self->setWindowsErrorString("QSystemSemaphore::modifySemaphore"_L1);
86#if defined QSYSTEMSEMAPHORE_DEBUG
87 qDebug("QSystemSemaphore::modifySemaphore WaitForSingleObject failed");
88#endif
89 return false;
90 }
91 }
92
93 self->clearError();
94 return true;
95}
96
97#endif // QT_CONFIG(systemsemaphore)
98
99QT_END_NAMESPACE
Combined button and popup list for selecting options.