1// Copyright (C) 2017 Intel Corporation.
2// Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QTSAN_IMPL_H
6#define QTSAN_IMPL_H
7
8#include <QtCore/qglobal.h>
9
10#if (__has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)) && __has_include(<sanitizer/tsan_interface.h>)
11# define QT_BUILDING_UNDER_TSAN
12# include <sanitizer/tsan_interface.h>
13#endif
14
15QT_BEGIN_NAMESPACE
16
17namespace QtTsan {
18#ifdef QT_BUILDING_UNDER_TSAN
19inline void futexAcquire(void *addr, void *addr2 = nullptr)
20{
21 // A futex call ensures total ordering on the futex words
22 // (in either success or failure of the call). Instruct TSAN accordingly,
23 // as TSAN does not understand the futex(2) syscall (or equivalent).
24 ::__tsan_acquire(addr);
25 if (addr2)
26 ::__tsan_acquire(addr2);
27}
28
29inline void futexRelease(void *addr, void *addr2 = nullptr)
30{
31 if (addr2)
32 ::__tsan_release(addr2);
33 ::__tsan_release(addr);
34}
35
36inline void mutexPreLock(void *addr, unsigned flags)
37{
38 ::__tsan_mutex_pre_lock(addr, flags);
39}
40
41inline void mutexPostLock(void *addr, unsigned flags, int recursion)
42{
43 ::__tsan_mutex_post_lock(addr, flags, recursion);
44}
45
46inline void mutexPreUnlock(void *addr, unsigned flags)
47{
48 ::__tsan_mutex_pre_unlock(addr, flags);
49}
50
51inline void mutexPostUnlock(void *addr, unsigned flags)
52{
53 ::__tsan_mutex_post_unlock(addr, flags);
54}
55
56enum : unsigned {
57 MutexWriteReentrant = ::__tsan_mutex_write_reentrant,
58 TryLock = ::__tsan_mutex_try_lock,
59 TryLockFailed = ::__tsan_mutex_try_lock_failed,
60};
61#else
62inline void futexAcquire(void *, void * = nullptr) {}
63inline void futexRelease(void *, void * = nullptr) {}
64
65enum : unsigned {
66 MutexWriteReentrant,
67 TryLock,
68 TryLockFailed,
69};
70inline void mutexPreLock(void *, unsigned) {}
71inline void mutexPostLock(void *, unsigned, int) {}
72inline void mutexPreUnlock(void *, unsigned) {}
73inline void mutexPostUnlock(void *, unsigned) {}
74#endif // QT_BUILDING_UNDER_TSAN
75} // namespace QtTsan
76
77QT_END_NAMESPACE
78
79#endif // QTSAN_IMPL_H
80

source code of qtbase/src/corelib/thread/qtsan_impl.h