1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4#ifndef QDBUSTHREADDEBUG_P_H
5#define QDBUSTHREADDEBUG_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtDBus/private/qtdbusglobal_p.h>
19
20#ifndef QT_NO_DBUS
21
22#if !defined(QDBUS_THREAD_DEBUG) && defined(QT_BUILD_INTERNAL)
23# define QDBUS_THREAD_DEBUG 1
24#endif
25
26#if QDBUS_THREAD_DEBUG
27QT_BEGIN_NAMESPACE
28typedef void (*qdbusThreadDebugFunc)(int, int, QDBusConnectionPrivate *);
29Q_DBUS_EXPORT void qdbusDefaultThreadDebug(int, int, QDBusConnectionPrivate *);
30extern Q_DBUS_EXPORT qdbusThreadDebugFunc qdbusThreadDebug;
31QT_END_NAMESPACE
32#endif
33
34enum ThreadAction {
35 ConnectAction = 0,
36 DisconnectAction = 1,
37 RegisterObjectAction = 2,
38 UnregisterObjectAction = 3,
39 ObjectRegisteredAtAction = 4,
40
41 CloseConnectionAction = 10,
42 ObjectDestroyedAction = 11,
43 RelaySignalAction = 12,
44 HandleObjectCallAction = 13,
45 HandleSignalAction = 14,
46 ConnectRelayAction = 15,
47 DisconnectRelayAction = 16,
48 FindMetaObject1Action = 17,
49 FindMetaObject2Action = 18,
50 RegisterServiceAction = 19,
51 UnregisterServiceAction = 20,
52 UpdateSignalHookOwnerAction = 21,
53 HandleObjectCallPostEventAction = 22,
54 HandleObjectCallSemaphoreAction = 23,
55 DoDispatchAction = 24,
56 SetDispatchEnabledAction = 25,
57 MessageResultReceivedAction = 26,
58 ActivateSignalAction = 27,
59 PendingCallBlockAction = 28,
60 SendMessageAction = 29,
61 HuntAndEmitAction = 30,
62};
63
64struct QDBusLockerBase
65{
66 enum Condition
67 {
68 BeforeLock,
69 AfterLock,
70 BeforeUnlock,
71 AfterUnlock,
72
73 BeforePost,
74 AfterPost,
75 BeforeDeliver,
76 AfterDeliver,
77
78 BeforeAcquire,
79 AfterAcquire,
80 BeforeRelease,
81 AfterRelease
82 };
83
84#if QDBUS_THREAD_DEBUG
85 static inline void reportThreadAction(int action, int condition, QDBusConnectionPrivate *ptr)
86 { if (qdbusThreadDebug) qdbusThreadDebug(action, condition, ptr); }
87#else
88 static inline void reportThreadAction(int, int, QDBusConnectionPrivate *) { }
89#endif
90};
91
92struct QDBusReadLocker: QDBusLockerBase
93{
94 QDBusConnectionPrivate *self;
95 ThreadAction action;
96 Q_NODISCARD_CTOR QDBusReadLocker(ThreadAction a, QDBusConnectionPrivate *s)
97 : self(s), action(a)
98 {
99 reportThreadAction(action, condition: BeforeLock, ptr: self);
100 self->lock.lockForRead();
101 reportThreadAction(action, condition: AfterLock, ptr: self);
102 }
103
104 inline ~QDBusReadLocker()
105 {
106 reportThreadAction(action, condition: BeforeUnlock, ptr: self);
107 self->lock.unlock();
108 reportThreadAction(action, condition: AfterUnlock, ptr: self);
109 }
110};
111
112struct QDBusWriteLocker: QDBusLockerBase
113{
114 QDBusConnectionPrivate *self;
115 ThreadAction action;
116 Q_NODISCARD_CTOR QDBusWriteLocker(ThreadAction a, QDBusConnectionPrivate *s)
117 : self(s), action(a)
118 {
119 reportThreadAction(action, condition: BeforeLock, ptr: self);
120 self->lock.lockForWrite();
121 reportThreadAction(action, condition: AfterLock, ptr: self);
122 }
123
124 inline ~QDBusWriteLocker()
125 {
126 reportThreadAction(action, condition: BeforeUnlock, ptr: self);
127 self->lock.unlock();
128 reportThreadAction(action, condition: AfterUnlock, ptr: self);
129 }
130};
131
132#if QDBUS_THREAD_DEBUG
133# define SEM_ACQUIRE(action, sem) \
134 do { \
135 QDBusLockerBase::reportThreadAction(action, QDBusLockerBase::BeforeAcquire, this); \
136 sem.acquire(); \
137 QDBusLockerBase::reportThreadAction(action, QDBusLockerBase::AfterAcquire, this); \
138 } while (false)
139
140# define SEM_RELEASE(action, sem) \
141 do { \
142 QDBusLockerBase::reportThreadAction(action, QDBusLockerBase::BeforeRelease, that); \
143 sem.release(); \
144 QDBusLockerBase::reportThreadAction(action, QDBusLockerBase::AfterRelease, that); \
145 } while (false)
146
147#else
148# define SEM_ACQUIRE(action, sem) sem.acquire()
149# define SEM_RELEASE(action, sem) sem.release()
150#endif
151
152#endif // QT_NO_DBUS
153#endif
154

source code of qtbase/src/dbus/qdbusthreaddebug_p.h