srt_attr_defs.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef INC_SRT_ATTR_DEFS_H
  15. #define INC_SRT_ATTR_DEFS_H
  16. // ATTRIBUTES:
  17. //
  18. // SRT_ATR_UNUSED: declare an entity ALLOWED to be unused (prevents warnings)
  19. // ATR_DEPRECATED: declare an entity deprecated (compiler should warn when used)
  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. #ifdef __GNUG__
  26. #define ATR_DEPRECATED __attribute__((deprecated))
  27. #else
  28. #define ATR_DEPRECATED
  29. #endif
  30. #if defined(__cplusplus) && __cplusplus > 199711L
  31. #define HAVE_CXX11 1
  32. // For gcc 4.7, claim C++11 is supported, as long as experimental C++0x is on,
  33. // however it's only the "most required C++11 support".
  34. #if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 7 // 4.7 only!
  35. #define ATR_NOEXCEPT
  36. #define ATR_NOTHROW throw()
  37. #define ATR_CONSTEXPR
  38. #define ATR_OVERRIDE
  39. #define ATR_FINAL
  40. #else
  41. #define HAVE_FULL_CXX11 1
  42. #define ATR_NOEXCEPT noexcept
  43. #define ATR_NOTHROW noexcept
  44. #define ATR_CONSTEXPR constexpr
  45. #define ATR_OVERRIDE override
  46. #define ATR_FINAL final
  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. #define ATR_NOEXCEPT noexcept
  57. #define ATR_NOTHROW noexcept
  58. #define ATR_CONSTEXPR constexpr
  59. #define ATR_OVERRIDE override
  60. #define ATR_FINAL final
  61. #else
  62. #define ATR_NOEXCEPT
  63. #define ATR_NOTHROW throw()
  64. #define ATR_CONSTEXPR
  65. #define ATR_OVERRIDE
  66. #define ATR_FINAL
  67. #endif
  68. #else
  69. #define HAVE_CXX11 0
  70. #define ATR_NOEXCEPT
  71. #define ATR_NOTHROW throw()
  72. #define ATR_CONSTEXPR
  73. #define ATR_OVERRIDE
  74. #define ATR_FINAL
  75. #endif // __cplusplus
  76. #if !HAVE_CXX11 && defined(REQUIRE_CXX11) && REQUIRE_CXX11 == 1
  77. #error "The currently compiled application required C++11, but your compiler doesn't support it."
  78. #endif
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // Attributes for thread safety analysis
  81. // - Clang TSA (https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutexheader).
  82. // - MSVC SAL (partially).
  83. // - Other compilers: none.
  84. ///////////////////////////////////////////////////////////////////////////////
  85. #if _MSC_VER >= 1920
  86. // In case of MSVC these attributes have to precede the attributed objects (variable, function).
  87. // E.g. SRT_ATTR_GUARDED_BY(mtx) int object;
  88. // It is tricky to annotate e.g. the following function, as clang complaints it does not know 'm'.
  89. // SRT_ATTR_EXCLUDES(m) SRT_ATTR_ACQUIRE(m)
  90. // inline void enterCS(Mutex& m) { m.lock(); }
  91. #define SRT_ATTR_CAPABILITY(expr)
  92. #define SRT_ATTR_SCOPED_CAPABILITY
  93. #define SRT_ATTR_GUARDED_BY(expr) _Guarded_by_(expr)
  94. #define SRT_ATTR_PT_GUARDED_BY(expr)
  95. #define SRT_ATTR_ACQUIRED_BEFORE(...)
  96. #define SRT_ATTR_ACQUIRED_AFTER(...)
  97. #define SRT_ATTR_REQUIRES(expr) _Requires_lock_held_(expr)
  98. #define SRT_ATTR_REQUIRES2(expr1, expr2) _Requires_lock_held_(expr1) _Requires_lock_held_(expr2)
  99. #define SRT_ATTR_REQUIRES_SHARED(...)
  100. #define SRT_ATTR_ACQUIRE(expr) _Acquires_nonreentrant_lock_(expr)
  101. #define SRT_ATTR_ACQUIRE_SHARED(...)
  102. #define SRT_ATTR_RELEASE(expr) _Releases_lock_(expr)
  103. #define SRT_ATTR_RELEASE_SHARED(...)
  104. #define SRT_ATTR_RELEASE_GENERIC(...)
  105. #define SRT_ATTR_TRY_ACQUIRE(...) _Acquires_nonreentrant_lock_(expr)
  106. #define SRT_ATTR_TRY_ACQUIRE_SHARED(...)
  107. #define SRT_ATTR_EXCLUDES(...)
  108. #define SRT_ATTR_ASSERT_CAPABILITY(expr)
  109. #define SRT_ATTR_ASSERT_SHARED_CAPABILITY(x)
  110. #define SRT_ATTR_RETURN_CAPABILITY(x)
  111. #define SRT_ATTR_NO_THREAD_SAFETY_ANALYSIS
  112. #else
  113. #if defined(__clang__) && defined(__clang_major__) && (__clang_major__ > 5)
  114. #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  115. #else
  116. #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
  117. #endif
  118. #define SRT_ATTR_CAPABILITY(x) \
  119. THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
  120. #define SRT_ATTR_SCOPED_CAPABILITY \
  121. THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  122. #define SRT_ATTR_GUARDED_BY(x) \
  123. THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  124. #define SRT_ATTR_PT_GUARDED_BY(x) \
  125. THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  126. #define SRT_ATTR_ACQUIRED_BEFORE(...) \
  127. THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
  128. #define SRT_ATTR_ACQUIRED_AFTER(...) \
  129. THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
  130. #define SRT_ATTR_REQUIRES(...) \
  131. THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
  132. #define SRT_ATTR_REQUIRES2(...) \
  133. THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
  134. #define SRT_ATTR_REQUIRES_SHARED(...) \
  135. THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
  136. #define SRT_ATTR_ACQUIRE(...) \
  137. THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
  138. #define SRT_ATTR_ACQUIRE_SHARED(...) \
  139. THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
  140. #define SRT_ATTR_RELEASE(...) \
  141. THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
  142. #define SRT_ATTR_RELEASE_SHARED(...) \
  143. THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
  144. #define SRT_ATTR_RELEASE_GENERIC(...) \
  145. THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__))
  146. #define SRT_ATTR_TRY_ACQUIRE(...) \
  147. THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
  148. #define SRT_ATTR_TRY_ACQUIRE_SHARED(...) \
  149. THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
  150. #define SRT_ATTR_EXCLUDES(...) \
  151. THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  152. #define SRT_ATTR_ASSERT_CAPABILITY(x) \
  153. THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
  154. #define SRT_ATTR_ASSERT_SHARED_CAPABILITY(x) \
  155. THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
  156. #define SRT_ATTR_RETURN_CAPABILITY(x) \
  157. THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  158. #define SRT_ATTR_NO_THREAD_SAFETY_ANALYSIS \
  159. THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  160. #endif // not _MSC_VER
  161. #endif // INC_SRT_ATTR_DEFS_H