File indexing completed on 2024-05-05 16:13:52

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