Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qsystemsemaphore.h"
6#include "qcoreapplication.h"
7#include <qdebug.h>
8#include <qt_windows.h>
9
11
12using namespace Qt::StringLiterals;
13
14#if QT_CONFIG(systemsemaphore)
15
16void QSystemSemaphorePrivate::setWindowsErrorString(QLatin1StringView function)
17{
18 BOOL windowsError = GetLastError();
19 if (windowsError == 0)
20 return;
21
22 switch (windowsError) {
23 case ERROR_NO_SYSTEM_RESOURCES:
24 case ERROR_NOT_ENOUGH_MEMORY:
25 error = QSystemSemaphore::OutOfResources;
26 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
27 break;
28 case ERROR_ACCESS_DENIED:
29 error = QSystemSemaphore::PermissionDenied;
30 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
31 break;
32 default:
33 errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error: %2")
34 .arg(function, qt_error_string(windowsError));
35 error = QSystemSemaphore::UnknownError;
36#if defined QSYSTEMSEMAPHORE_DEBUG
37 qDebug() << errorString << "key" << key;
38#endif
39 }
40}
41
42HANDLE QSystemSemaphoreWin32::handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode)
43{
44 // don't allow making handles on empty keys
45 if (self->nativeKey.isEmpty())
46 return 0;
47
48 // Create it if it doesn't already exists.
49 if (semaphore == 0) {
50 semaphore = CreateSemaphore(0, self->initialValue, MAXLONG,
51 reinterpret_cast<const wchar_t*>(self->nativeKey.nativeKey().utf16()));
52 if (semaphore == NULL)
53 self->setWindowsErrorString("QSystemSemaphore::handle"_L1);
54 }
55
56 return semaphore;
57}
58
59void QSystemSemaphoreWin32::cleanHandle(QSystemSemaphorePrivate *)
60{
61 if (semaphore && !CloseHandle(semaphore)) {
62#if defined QSYSTEMSEMAPHORE_DEBUG
63 qDebug("QSystemSemaphoreWin32::CloseHandle: sem failed");
64#endif
65 }
66 semaphore = 0;
67}
68
69bool QSystemSemaphoreWin32::modifySemaphore(QSystemSemaphorePrivate *self, int count)
70{
71 if (handle(self, QSystemSemaphore::Open) == nullptr)
72 return false;
73
74 if (count > 0) {
75 if (0 == ReleaseSemaphore(semaphore, count, 0)) {
76 self->setWindowsErrorString("QSystemSemaphore::modifySemaphore"_L1);
77#if defined QSYSTEMSEMAPHORE_DEBUG
78 qDebug("QSystemSemaphore::modifySemaphore ReleaseSemaphore failed");
79#endif
80 return false;
81 }
82 } else {
83 if (WAIT_OBJECT_0 != WaitForSingleObjectEx(semaphore, INFINITE, FALSE)) {
84 self->setWindowsErrorString("QSystemSemaphore::modifySemaphore"_L1);
85#if defined QSYSTEMSEMAPHORE_DEBUG
86 qDebug("QSystemSemaphore::modifySemaphore WaitForSingleObject failed");
87#endif
88 return false;
89 }
90 }
91
92 self->clearError();
93 return true;
94}
95
96#endif // QT_CONFIG(systemsemaphore)
97
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
Definition qstring.cpp:6995
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
Combined button and popup list for selecting options.
Q_MULTIMEDIA_EXPORT QString errorString(HRESULT hr)
void * HANDLE
QString self
Definition language.cpp:58
DBusConnection const char DBusError * error
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
#define qDebug
[1]
Definition qlogging.h:164
GLuint64 GLenum void * handle
GLuint64 key
GLenum GLenum GLsizei count