Hi all,
it's not clear to me whether our licensing policy allows exceptions or not.
https://community.kde.org/Policies/Licensing_Policy does not mention any
exceptions.
https://community.kde.org/Guidelines_and_HOWTOs/Licensing explains how to use
exceptions and gives an example with the Qt-LGPL-exception-1.1.
I have copied code from GCC's STL to implement a QMutex-compatible replacement
for std::unique_lock (because apparently Windows resp. mingw doesn't have
std::mutex). GCC's code is "GPL-3.0-or-later WITH GCC-exception-3.1". (Ignore
the bogus license id in the .cpp file. I've already fixed it.) Therefore I've
put my Qt'ified copy under the same license.
What now? Did I violate our licensing policy? Should we explicitly add allowed
exceptions to our licensing policy? I guess we don't want to allow all
exceptions listed at https://spdx.org/licenses/exceptions-index.html.
Regards,
Ingo
--- Begin Message ---
Git commit bd610f307dcd08be0e08007b483002f7c4df24ca by Ingo Klöcker.
Committed on 25/11/2021 at 11:01.
Pushed by kloecker into branch 'master'.
Add a QMutex-compatible replacement for std::unique_lock
M +3-0src/CMakeLists.txt
A +149 -0src/utils/uniquelock.cpp *
A +135 -0src/utils/uniquelock.h *
The files marked with a * at the end have a non valid license. Please read:
https://community.kde.org/Policies/Licensing_Policy and use the headers which
are listed at that page.
https://invent.kde.org/pim/libkleo/commit/bd610f307dcd08be0e08007b483002f7c4df24ca
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 561e618..d871dd3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -103,6 +103,8 @@ target_sources(KF5Libkleo PRIVATE
utils/stringutils.h
utils/test.cpp
utils/test.h
+ utils/uniquelock.cpp
+ utils/uniquelock.h
)
ecm_qt_declare_logging_category(KF5Libkleo HEADER libkleo_debug.h IDENTIFIER
LIBKLEO_LOG CATEGORY_NAME org.kde.pim.libkleo
DESCRIPTION "libkleo (kleo_core)"
@@ -255,6 +257,7 @@ ecm_generate_headers(libkleo_CamelCase_utils_HEADERS
SCDaemon
StringUtils
Test
+ UniqueLock
REQUIRED_HEADERS libkleo_utils_HEADERS
PREFIX Libkleo
RELATIVE utils
diff --git a/src/utils/uniquelock.cpp b/src/utils/uniquelock.cpp
new file mode 100644
index 000..de462cf
--- /dev/null
+++ b/src/utils/uniquelock.cpp
@@ -0,0 +1,149 @@
+/*
+utils/uniquelock.cpp
+QMutex-compatible replacement for std::UniqueLock
+
+This file is part of libkleopatra, the KDE keymanagement library
+SPDX-FileCopyrightText: 2008-2021 Free Software Foundation, Inc.
+SPDX-FileCopyrightText: 2021 g10 Code GmbH
+SPDX-FileContributor: Ingo Klöcker
+
+SPDX-License-Identifier: GPL-3.0-or-later+GCC Runtime Library Exception
+*/
+
+#include
+
+#include "uniquelock.h"
+
+#include
+
+#include
+
+namespace Kleo
+{
+
+UniqueLock::UniqueLock() noexcept
+: mMutex{nullptr}, mOwnsMutex{false}
+{
+}
+
+UniqueLock::UniqueLock(QMutex )
+: mMutex{std::addressof(mutex)}, mOwnsMutex{false}
+{
+lock();
+mOwnsMutex = true;
+}
+
+UniqueLock::UniqueLock(QMutex , DeferLockType) noexcept
+: mMutex{std::addressof(mutex)}, mOwnsMutex{false}
+{
+}
+
+UniqueLock::UniqueLock(QMutex , TryToLockType)
+: mMutex{std::addressof(mutex)}, mOwnsMutex{mMutex->try_lock()}
+{
+}
+
+UniqueLock::UniqueLock(QMutex , AdoptLockType) noexcept
+: mMutex{std::addressof(mutex)}, mOwnsMutex{true}
+{
+// XXX calling thread owns mutex
+}
+
+UniqueLock::~UniqueLock()
+{
+if (mOwnsMutex) {
+unlock();
+}
+}
+
+UniqueLock::UniqueLock(UniqueLock &) noexcept
+: mMutex{u.mMutex}, mOwnsMutex{u.mOwnsMutex}
+{
+u.mMutex = nullptr;
+u.mOwnsMutex = false;
+}
+
+UniqueLock ::operator=(UniqueLock &) noexcept
+{
+if(mOwnsMutex) {
+unlock();
+}
+
+UniqueLock(std::move(u)).swap(*this);
+
+u.mMutex = nullptr;
+u.mOwnsMutex = false;
+
+return *this;
+}
+
+void UniqueLock::lock()
+{
+Q_ASSERT(mMutex);
+Q_ASSERT(!mOwnsMutex);
+if (!mMutex) {
+qCWarning(LIBKLEO_LOG) << __func__ << "Error: operation not permitted";
+} else if (mOwnsMutex) {
+qCWarning(LIBKLEO_LOG) << __func__ << "Error: resource deadlock would
occur";
+} else {
+mMutex->lock();
+mOwnsMutex = true;
+}
+}
+
+bool UniqueLock::try_lock()
+{
+Q_ASSERT(mMutex);
+Q_ASSERT(!mOwnsMutex);
+if (!mMutex) {
+qCWarning(LIBKLEO_LOG) << __func__ << "Error: operation not permitted";
+return false;
+} else if (mOwnsMutex) {
+qCWarning(LIBKLEO_LOG) << __func__ << "Error: resource deadlock would
occur";
+return false;
+} else {
+mOwnsMutex = mMutex->try_lock();
+return mOwnsMutex;
+}
+}
+
+void UniqueLock::unlock()
+{
+if (!mOwnsMutex) {
+qCWarning(LIBKLEO_LOG) << __func__ << "Error: operation not permitted";
+} else if (mMutex) {
+mMutex->unlock();
+mOwnsMutex = false;
+}
+}
+
+void