thread_safety.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef BOOST_THREAD_DETAIL_THREAD_SAFETY_HPP
  2. #define BOOST_THREAD_DETAIL_THREAD_SAFETY_HPP
  3. #if defined(__GNUC__) && !defined(__GXX_EXPERIMENTAL_CXX0X__)
  4. //
  5. // This is horrible, but it seems to be the only we can shut up the
  6. // "anonymous variadic macros were introduced in C99 [-Wvariadic-macros]"
  7. // warning that get spewed out otherwise in non-C++11 mode.
  8. //
  9. #pragma GCC system_header
  10. #endif
  11. // See https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
  12. // Un-comment to enable Thread Safety Analysis
  13. //#define BOOST_THREAD_ENABLE_THREAD_SAFETY_ANALYSIS
  14. // Enable thread safety attributes only with clang.
  15. // The attributes can be safely erased when compiling with other compilers.
  16. #if defined (BOOST_THREAD_ENABLE_THREAD_SAFETY_ANALYSIS) && defined(__clang__) && (!defined(SWIG))
  17. #define BOOST_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  18. #else
  19. #define BOOST_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
  20. #endif
  21. #define BOOST_THREAD_CAPABILITY(x) \
  22. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
  23. #define BOOST_THREAD_SCOPED_CAPABILITY \
  24. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  25. #define BOOST_THREAD_GUARDED_BY(x) \
  26. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  27. #define BOOST_THREAD_PT_GUARDED_BY(x) \
  28. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  29. #define BOOST_THREAD_ACQUIRED_BEFORE(...) \
  30. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
  31. #define BOOST_THREAD_ACQUIRED_AFTER(...) \
  32. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
  33. #define BOOST_THREAD_REQUIRES(...) \
  34. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
  35. #define BOOST_THREAD_REQUIRES_SHARED(...) \
  36. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
  37. #define BOOST_THREAD_ACQUIRE(...) \
  38. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
  39. #define BOOST_THREAD_ACQUIRE_SHARED(...) \
  40. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
  41. #define BOOST_THREAD_RELEASE(...) \
  42. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
  43. #define BOOST_THREAD_RELEASE_SHARED(...) \
  44. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
  45. #define BOOST_THREAD_TRY_ACQUIRE(...) \
  46. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
  47. #define BOOST_THREAD_TRY_ACQUIRE_SHARED(...) \
  48. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
  49. #define BOOST_THREAD_EXCLUDES(...) \
  50. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  51. #define BOOST_THREAD_ASSERT_CAPABILITY(x) \
  52. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
  53. #define BOOST_THREAD_ASSERT_SHARED_CAPABILITY(x) \
  54. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
  55. #define BOOST_THREAD_RETURN_CAPABILITY(x) \
  56. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  57. #define BOOST_THREAD_NO_THREAD_SAFETY_ANALYSIS \
  58. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  59. #if defined(__clang__) && (!defined(SWIG)) && defined(__FreeBSD__)
  60. #if __has_attribute(no_thread_safety_analysis)
  61. #define BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
  62. #else
  63. #define BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  64. #endif
  65. #else
  66. #define BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  67. #endif
  68. #ifdef USE_LOCK_STYLE_THREAD_SAFETY_ATTRIBUTES
  69. // The original version of thread safety analysis the following attribute
  70. // definitions. These use a lock-based terminology. They are still in use
  71. // by existing thread safety code, and will continue to be supported.
  72. // Deprecated.
  73. #define BOOST_THREAD_PT_GUARDED_VAR \
  74. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_var)
  75. // Deprecated.
  76. #define BOOST_THREAD_GUARDED_VAR \
  77. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(guarded_var)
  78. // Replaced by REQUIRES
  79. #define BOOST_THREAD_EXCLUSIVE_LOCKS_REQUIRED(...) \
  80. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
  81. // Replaced by REQUIRES_SHARED
  82. #define BOOST_THREAD_SHARED_LOCKS_REQUIRED(...) \
  83. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
  84. // Replaced by CAPABILITY
  85. #define BOOST_THREAD_LOCKABLE \
  86. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(lockable)
  87. // Replaced by SCOPED_CAPABILITY
  88. #define BOOST_THREAD_SCOPED_LOCKABLE \
  89. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  90. // Replaced by ACQUIRE
  91. #define BOOST_THREAD_EXCLUSIVE_LOCK_FUNCTION(...) \
  92. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
  93. // Replaced by ACQUIRE_SHARED
  94. #define BOOST_THREAD_SHARED_LOCK_FUNCTION(...) \
  95. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
  96. // Replaced by RELEASE and RELEASE_SHARED
  97. #define BOOST_THREAD_UNLOCK_FUNCTION(...) \
  98. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
  99. // Replaced by TRY_ACQUIRE
  100. #define BOOST_THREAD_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
  101. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
  102. // Replaced by TRY_ACQUIRE_SHARED
  103. #define BOOST_THREAD_SHARED_TRYLOCK_FUNCTION(...) \
  104. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
  105. // Replaced by ASSERT_CAPABILITY
  106. #define BOOST_THREAD_ASSERT_EXCLUSIVE_LOCK(...) \
  107. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__))
  108. // Replaced by ASSERT_SHARED_CAPABILITY
  109. #define BOOST_THREAD_ASSERT_SHARED_LOCK(...) \
  110. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__))
  111. // Replaced by EXCLUDE_CAPABILITY.
  112. #define BOOST_THREAD_LOCKS_EXCLUDED(...) \
  113. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  114. // Replaced by RETURN_CAPABILITY
  115. #define BOOST_THREAD_LOCK_RETURNED(x) \
  116. BOOST_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  117. #endif // USE_LOCK_STYLE_THREAD_SAFETY_ATTRIBUTES
  118. #endif // BOOST_THREAD_DETAIL_THREAD_SAFETY_HPP