File indexing completed on 2024-04-28 15:20:31
0001 /* 0002 This file is part of libkdbus 0003 0004 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0005 SPDX-FileCopyrightText: 2011 Kevin Ottens <ervin@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0008 */ 0009 0010 #ifndef KDBUSINTERPROCESSLOCK_H 0011 #define KDBUSINTERPROCESSLOCK_H 0012 0013 #include <QObject> 0014 #include <memory> 0015 0016 #include <kdbusaddons_export.h> 0017 0018 class KDBusInterProcessLockPrivate; 0019 0020 /** 0021 * @class KDBusInterProcessLock kdbusinterprocesslock.h <KDBusInterProcessLock> 0022 * 0023 * @short A class for serializing access to a resource that is shared between multiple processes. 0024 * 0025 * This class can be used to serialize access to a resource between 0026 * multiple processes. Instead of using lock files, which could 0027 * become stale easily, the registration of dummy D-Bus services is used 0028 * to allow only one process at a time to access the resource. 0029 * 0030 * Example: 0031 * 0032 * @code 0033 * 0034 * KDBusInterProcessLock *lock = new KDBusInterProcessLock("myresource"); 0035 * connect(lock, &KDBusInterProcessLock::lockGranted, this, &MyClass::doCriticalTask); 0036 * lock->lock(); 0037 * 0038 * ... 0039 * 0040 * ... ::doCriticalTask(KDBusInterProcessLock *lock) 0041 * { 0042 * // change common resource 0043 * 0044 * lock->unlock(); 0045 * } 0046 * 0047 * @endcode 0048 * 0049 * @author Tobias Koenig <tokoe@kde.org> 0050 */ 0051 class KDBUSADDONS_EXPORT KDBusInterProcessLock : public QObject 0052 { 0053 Q_OBJECT 0054 0055 public: 0056 /** 0057 * Creates a new inter process lock object. 0058 * 0059 * @param resource The identifier of the resource that shall be locked. 0060 * This identifier can be any string, however it must be unique for 0061 * the resource and every client that wants to access the resource must 0062 * know it. 0063 */ 0064 KDBusInterProcessLock(const QString &resource); 0065 0066 /** 0067 * Destroys the inter process lock object. 0068 */ 0069 ~KDBusInterProcessLock() override; 0070 0071 /** 0072 * Returns the identifier of the resource the lock is set on. 0073 */ 0074 QString resource() const; 0075 0076 /** 0077 * Requests the lock. 0078 * 0079 * The lock is granted as soon as the lockGranted() signal is emitted. 0080 */ 0081 void lock(); 0082 0083 /** 0084 * Releases the lock. 0085 * 0086 * @note This method should be called as soon as the critical area is left 0087 * in your code path and the lock is no longer needed. 0088 */ 0089 void unlock(); 0090 0091 /** 0092 * Waits for the granting of a lock by starting an internal event loop. 0093 */ 0094 void waitForLockGranted(); 0095 0096 Q_SIGNALS: 0097 /** 0098 * This signal is emitted when the requested lock has been granted. 0099 * 0100 * @param lock The lock that has been granted. 0101 */ 0102 void lockGranted(KDBusInterProcessLock *lock); 0103 0104 private: 0105 friend class KDBusInterProcessLockPrivate; 0106 std::unique_ptr<KDBusInterProcessLockPrivate> const d; 0107 }; 0108 0109 #endif