1/*
2 This file is part of the KDE
3 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-only
6*/
7
8#ifndef KINTERPROCESSLOCK_H
9#define KINTERPROCESSLOCK_H
10
11#include <QObject>
12
13class KInterProcessLockPrivate;
14
15/**
16 * @short A class for serializing access to a resource that is shared between multiple processes.
17 *
18 * This class can be used to serialize access to a resource between
19 * multiple processes. Instead of using lock files, which could
20 * become stale easily, the registration of dummy dbus services is used
21 * to allow only one process at a time to access the resource.
22 *
23 * Example:
24 *
25 * @code
26 *
27 * KInterProcessLock *lock = new KInterProcessLock("myresource");
28 * connect(lock, KInterProcessLock::lockGranted, this, [this, lock] { doCriticalTask(lock); });
29 * lock->lock();
30 *
31 * ...
32 *
33 * ... ::doCriticalTask(KInterProcessLock *lock)
34 * {
35 * // change common resource
36 *
37 * lock->unlock();
38 * }
39 *
40 * @endcode
41 *
42 * @author Tobias Koenig <tokoe@kde.org>
43 */
44class KInterProcessLock : public QObject
45{
46 Q_OBJECT
47 Q_DECLARE_PRIVATE(KInterProcessLock)
48
49public:
50 /**
51 * Creates a new inter process lock object.
52 *
53 * @param resource The identifier of the resource that shall be locked.
54 * This identifier can be any string, however it must be unique for
55 * the resource and every client that wants to access the resource must
56 * know it.
57 */
58 explicit KInterProcessLock(const QString &resource);
59
60 /**
61 * Destroys the inter process lock object.
62 */
63 ~KInterProcessLock() override;
64
65 /**
66 * Returns the identifier of the resource the lock is set on.
67 */
68 QString resource() const;
69
70 /**
71 * Requests the lock.
72 *
73 * The lock is granted as soon as the lockGranted() signal is emitted.
74 */
75 void lock();
76
77 /**
78 * Releases the lock.
79 *
80 * @note This method should be called as soon as the critical area is left
81 * in your code path and the lock is no longer needed.
82 */
83 void unlock();
84
85 /**
86 * Waits for the granting of a lock by starting an internal event loop.
87 */
88 void waitForLockGranted();
89
90Q_SIGNALS:
91 /**
92 * This signal is emitted when the requested lock has been granted.
93 *
94 * @param lock The lock that has been granted.
95 */
96 void lockGranted(KInterProcessLock *lock);
97
98private:
99 KInterProcessLockPrivate *const d_ptr;
100};
101
102#endif
103

source code of kio/src/kioworkers/trash/kinterprocesslock.h