| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2019 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v.2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
/***************************************************************************** |
| 11 |
The file contains various planform and compiler dependent attribute definitions |
| 12 |
used by SRT library internally. |
| 13 |
*****************************************************************************/ |
| 14 |
|
| 15 |
#ifndef INC_SRT_ATTR_DEFS_H |
| 16 |
#define INC_SRT_ATTR_DEFS_H |
| 17 |
|
| 18 |
// ATTRIBUTES: |
| 19 |
// |
| 20 |
// ATR_NOEXCEPT: The true `noexcept` from C++11, or nothing if compiling in pre-C++11 mode |
| 21 |
// ATR_NOTHROW: In C++11: `noexcept`. In pre-C++11: `throw()`. Required for GNU libstdc++. |
| 22 |
// ATR_CONSTEXPR: In C++11: `constexpr`. Otherwise empty. |
| 23 |
// ATR_OVERRIDE: In C++11: `override`. Otherwise empty. |
| 24 |
// ATR_FINAL: In C++11: `final`. Otherwise empty. |
| 25 |
|
| 26 |
|
| 27 |
#ifdef __GNUG__ |
| 28 |
#define HAVE_GCC 1 |
| 29 |
#else |
| 30 |
#define HAVE_GCC 0 |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(__cplusplus) && __cplusplus > 199711L |
| 34 |
#define HAVE_CXX11 1 |
| 35 |
// For gcc 4.7, claim C++11 is supported, as long as experimental C++0x is on, |
| 36 |
// however it's only the "most required C++11 support". |
| 37 |
#if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 7 // 4.7 only! |
| 38 |
#else |
| 39 |
#define HAVE_FULL_CXX11 1 |
| 40 |
|
| 41 |
#if __cplusplus >= 201703L |
| 42 |
#define HAVE_CXX17 1 |
| 43 |
#else |
| 44 |
#define HAVE_CXX17 0 |
| 45 |
#endif |
| 46 |
|
| 47 |
#endif |
| 48 |
#elif defined(_MSC_VER) && _MSC_VER >= 1800 |
| 49 |
// Microsoft Visual Studio supports C++11, but not fully, |
| 50 |
// and still did not change the value of __cplusplus. Treat |
| 51 |
// this special way. |
| 52 |
// _MSC_VER == 1800 means Microsoft Visual Studio 2013. |
| 53 |
#define HAVE_CXX11 1 |
| 54 |
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026 |
| 55 |
#define HAVE_FULL_CXX11 1 |
| 56 |
|
| 57 |
#if __cplusplus >= 201703L |
| 58 |
#define HAVE_CXX17 1 |
| 59 |
#else |
| 60 |
#define HAVE_CXX17 0 |
| 61 |
#endif |
| 62 |
|
| 63 |
#endif |
| 64 |
#else |
| 65 |
#define HAVE_CXX11 0 |
| 66 |
#define HAVE_CXX17 0 |
| 67 |
#endif // __cplusplus |
| 68 |
|
| 69 |
#if HAVE_FULL_CXX11 |
| 70 |
#define ATR_NOEXCEPT noexcept |
| 71 |
#define ATR_NOTHROW noexcept |
| 72 |
#define ATR_CONSTEXPR constexpr |
| 73 |
#define ATR_OVERRIDE override |
| 74 |
#define ATR_FINAL final |
| 75 |
#else |
| 76 |
// These are both for HAVE_CXX11 == 1 and 0. |
| 77 |
#define ATR_NOEXCEPT |
| 78 |
#define ATR_NOTHROW throw() |
| 79 |
#define ATR_CONSTEXPR |
| 80 |
#define ATR_OVERRIDE |
| 81 |
#define ATR_FINAL |
| 82 |
#endif |
| 83 |
|
| 84 |
#if HAVE_CXX11 |
| 85 |
#define SRT_ATR_ALIGNAS(n) alignas(n) |
| 86 |
#elif HAVE_GCC |
| 87 |
#define SRT_ATR_ALIGNAS(n) __attribute__((aligned(n))) |
| 88 |
#else |
| 89 |
#define SRT_ATR_ALIGNAS(n) |
| 90 |
#endif |
| 91 |
|
| 92 |
#if !HAVE_CXX11 && defined(REQUIRE_CXX11) && REQUIRE_CXX11 == 1 |
| 93 |
#error "The currently compiled application required C++11, but your compiler doesn't support it." |
| 94 |
#endif |
| 95 |
|
| 96 |
/////////////////////////////////////////////////////////////////////////////// |
| 97 |
// Attributes for thread safety analysis |
| 98 |
// - Clang TSA (https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutexheader). |
| 99 |
// - MSVC SAL (partially). |
| 100 |
// - Other compilers: none. |
| 101 |
/////////////////////////////////////////////////////////////////////////////// |
| 102 |
#if _MSC_VER >= 1920 |
| 103 |
// In case of MSVC these attributes have to precede the attributed objects (variable, function). |
| 104 |
// E.g. SRT_ATTR_GUARDED_BY(mtx) int object; |
| 105 |
// It is tricky to annotate e.g. the following function, as clang complaints it does not know 'm'. |
| 106 |
// SRT_ATTR_EXCLUDES(m) SRT_ATTR_ACQUIRE(m) |
| 107 |
// inline void enterCS(Mutex& m) { m.lock(); } |
| 108 |
#define SRT_ATTR_CAPABILITY(expr) |
| 109 |
#define SRT_ATTR_SCOPED_CAPABILITY |
| 110 |
#define SRT_ATTR_GUARDED_BY(expr) _Guarded_by_(expr) |
| 111 |
#define SRT_ATTR_PT_GUARDED_BY(expr) |
| 112 |
#define SRT_ATTR_ACQUIRED_BEFORE(...) |
| 113 |
#define SRT_ATTR_ACQUIRED_AFTER(...) |
| 114 |
#define SRT_ATTR_REQUIRES(expr) _Requires_lock_held_(expr) |
| 115 |
#define SRT_ATTR_REQUIRES2(expr1, expr2) _Requires_lock_held_(expr1) _Requires_lock_held_(expr2) |
| 116 |
#define SRT_ATTR_REQUIRES_SHARED(...) |
| 117 |
#define SRT_ATTR_ACQUIRE(expr) _Acquires_nonreentrant_lock_(expr) |
| 118 |
#define SRT_ATTR_ACQUIRE_SHARED(...) |
| 119 |
#define SRT_ATTR_RELEASE(expr) _Releases_lock_(expr) |
| 120 |
#define SRT_ATTR_RELEASE_SHARED(...) |
| 121 |
#define SRT_ATTR_RELEASE_GENERIC(...) |
| 122 |
#define SRT_ATTR_TRY_ACQUIRE(...) _Acquires_nonreentrant_lock_(expr) |
| 123 |
#define SRT_ATTR_TRY_ACQUIRE_SHARED(...) |
| 124 |
#define SRT_ATTR_EXCLUDES(...) // the caller must not hold the given capabilities |
| 125 |
#define SRT_ATTR_ASSERT_CAPABILITY(expr) |
| 126 |
#define SRT_ATTR_ASSERT_SHARED_CAPABILITY(x) |
| 127 |
#define SRT_ATTR_RETURN_CAPABILITY(x) |
| 128 |
#define SRT_ATTR_NO_THREAD_SAFETY_ANALYSIS |
| 129 |
#else |
| 130 |
|
| 131 |
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ > 5) |
| 132 |
#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
| 133 |
#else |
| 134 |
#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
| 135 |
#endif |
| 136 |
|
| 137 |
#define SRT_ATTR_CAPABILITY(x) \ |
| 138 |
THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) |
| 139 |
|
| 140 |
#define SRT_ATTR_SCOPED_CAPABILITY \ |
| 141 |
THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 142 |
|
| 143 |
#define SRT_ATTR_GUARDED_BY(x) \ |
| 144 |
THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
| 145 |
|
| 146 |
#define SRT_ATTR_PT_GUARDED_BY(x) \ |
| 147 |
THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) |
| 148 |
|
| 149 |
#define SRT_ATTR_ACQUIRED_BEFORE(...) \ |
| 150 |
THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) |
| 151 |
|
| 152 |
#define SRT_ATTR_ACQUIRED_AFTER(...) \ |
| 153 |
THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) |
| 154 |
|
| 155 |
#define SRT_ATTR_REQUIRES(...) \ |
| 156 |
THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) |
| 157 |
|
| 158 |
#define SRT_ATTR_REQUIRES2(...) \ |
| 159 |
THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) |
| 160 |
|
| 161 |
#define SRT_ATTR_REQUIRES_SHARED(...) \ |
| 162 |
THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) |
| 163 |
|
| 164 |
#define SRT_ATTR_ACQUIRE(...) \ |
| 165 |
THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) |
| 166 |
|
| 167 |
#define SRT_ATTR_ACQUIRE_SHARED(...) \ |
| 168 |
THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) |
| 169 |
|
| 170 |
#define SRT_ATTR_RELEASE(...) \ |
| 171 |
THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) |
| 172 |
|
| 173 |
#define SRT_ATTR_RELEASE_SHARED(...) \ |
| 174 |
THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) |
| 175 |
|
| 176 |
#define SRT_ATTR_RELEASE_GENERIC(...) \ |
| 177 |
THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__)) |
| 178 |
|
| 179 |
#define SRT_ATTR_TRY_ACQUIRE(...) \ |
| 180 |
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) |
| 181 |
|
| 182 |
#define SRT_ATTR_TRY_ACQUIRE_SHARED(...) \ |
| 183 |
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) |
| 184 |
|
| 185 |
// The caller must not hold the given capabilities. |
| 186 |
#define SRT_ATTR_EXCLUDES(...) \ |
| 187 |
THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
| 188 |
|
| 189 |
#define SRT_ATTR_ASSERT_CAPABILITY(x) \ |
| 190 |
THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) |
| 191 |
|
| 192 |
#define SRT_ATTR_ASSERT_SHARED_CAPABILITY(x) \ |
| 193 |
THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) |
| 194 |
|
| 195 |
#define SRT_ATTR_RETURN_CAPABILITY(x) \ |
| 196 |
THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 197 |
|
| 198 |
#define SRT_ATTR_NO_THREAD_SAFETY_ANALYSIS \ |
| 199 |
THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
| 200 |
|
| 201 |
#endif // not _MSC_VER |
| 202 |
|
| 203 |
#endif // INC_SRT_ATTR_DEFS_H |