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