thread_annotations.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: thread_annotations.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // WARNING: This is a backwards compatible header and it will be removed after
  20. // the migration to prefixed thread annotations is finished; please include
  21. // "absl/base/thread_annotations.h".
  22. //
  23. // This header file contains macro definitions for thread safety annotations
  24. // that allow developers to document the locking policies of multi-threaded
  25. // code. The annotations can also help program analysis tools to identify
  26. // potential thread safety issues.
  27. //
  28. // These annotations are implemented using compiler attributes. Using the macros
  29. // defined here instead of raw attributes allow for portability and future
  30. // compatibility.
  31. //
  32. // When referring to mutexes in the arguments of the attributes, you should
  33. // use variable names or more complex expressions (e.g. my_object->mutex_)
  34. // that evaluate to a concrete mutex object whenever possible. If the mutex
  35. // you want to refer to is not in scope, you may use a member pointer
  36. // (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object.
  37. #ifndef ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_
  38. #define ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_
  39. // ABSL_LEGACY_THREAD_ANNOTATIONS is a *temporary* compatibility macro that can
  40. // be defined on the compile command-line to restore the legacy spellings of the
  41. // thread annotations macros/functions. The macros in this file are available
  42. // under ABSL_ prefixed spellings in absl/base/thread_annotations.h. This macro
  43. // and the legacy spellings will be removed in the future.
  44. #ifdef ABSL_LEGACY_THREAD_ANNOTATIONS
  45. #if defined(__clang__)
  46. #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  47. #else
  48. #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
  49. #endif
  50. // GUARDED_BY()
  51. //
  52. // Documents if a shared field or global variable needs to be protected by a
  53. // mutex. GUARDED_BY() allows the user to specify a particular mutex that
  54. // should be held when accessing the annotated variable.
  55. //
  56. // Although this annotation (and PT_GUARDED_BY, below) cannot be applied to
  57. // local variables, a local variable and its associated mutex can often be
  58. // combined into a small class or struct, thereby allowing the annotation.
  59. //
  60. // Example:
  61. //
  62. // class Foo {
  63. // Mutex mu_;
  64. // int p1_ GUARDED_BY(mu_);
  65. // ...
  66. // };
  67. #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  68. // PT_GUARDED_BY()
  69. //
  70. // Documents if the memory location pointed to by a pointer should be guarded
  71. // by a mutex when dereferencing the pointer.
  72. //
  73. // Example:
  74. // class Foo {
  75. // Mutex mu_;
  76. // int *p1_ PT_GUARDED_BY(mu_);
  77. // ...
  78. // };
  79. //
  80. // Note that a pointer variable to a shared memory location could itself be a
  81. // shared variable.
  82. //
  83. // Example:
  84. //
  85. // // `q_`, guarded by `mu1_`, points to a shared memory location that is
  86. // // guarded by `mu2_`:
  87. // int *q_ GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_);
  88. #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  89. // ACQUIRED_AFTER() / ACQUIRED_BEFORE()
  90. //
  91. // Documents the acquisition order between locks that can be held
  92. // simultaneously by a thread. For any two locks that need to be annotated
  93. // to establish an acquisition order, only one of them needs the annotation.
  94. // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
  95. // and ACQUIRED_BEFORE.)
  96. //
  97. // As with GUARDED_BY, this is only applicable to mutexes that are shared
  98. // fields or global variables.
  99. //
  100. // Example:
  101. //
  102. // Mutex m1_;
  103. // Mutex m2_ ACQUIRED_AFTER(m1_);
  104. #define ACQUIRED_AFTER(...) \
  105. THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
  106. #define ACQUIRED_BEFORE(...) \
  107. THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
  108. // EXCLUSIVE_LOCKS_REQUIRED() / SHARED_LOCKS_REQUIRED()
  109. //
  110. // Documents a function that expects a mutex to be held prior to entry.
  111. // The mutex is expected to be held both on entry to, and exit from, the
  112. // function.
  113. //
  114. // An exclusive lock allows read-write access to the guarded data member(s), and
  115. // only one thread can acquire a lock exclusively at any one time. A shared lock
  116. // allows read-only access, and any number of threads can acquire a shared lock
  117. // concurrently.
  118. //
  119. // Generally, non-const methods should be annotated with
  120. // EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
  121. // SHARED_LOCKS_REQUIRED.
  122. //
  123. // Example:
  124. //
  125. // Mutex mu1, mu2;
  126. // int a GUARDED_BY(mu1);
  127. // int b GUARDED_BY(mu2);
  128. //
  129. // void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
  130. // void bar() const SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
  131. #define EXCLUSIVE_LOCKS_REQUIRED(...) \
  132. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
  133. #define SHARED_LOCKS_REQUIRED(...) \
  134. THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
  135. // LOCKS_EXCLUDED()
  136. //
  137. // Documents the locks acquired in the body of the function. These locks
  138. // cannot be held when calling this function (as Abseil's `Mutex` locks are
  139. // non-reentrant).
  140. #define LOCKS_EXCLUDED(...) \
  141. THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  142. // LOCK_RETURNED()
  143. //
  144. // Documents a function that returns a mutex without acquiring it. For example,
  145. // a public getter method that returns a pointer to a private mutex should
  146. // be annotated with LOCK_RETURNED.
  147. #define LOCK_RETURNED(x) \
  148. THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  149. // LOCKABLE
  150. //
  151. // Documents if a class/type is a lockable type (such as the `Mutex` class).
  152. #define LOCKABLE \
  153. THREAD_ANNOTATION_ATTRIBUTE__(lockable)
  154. // SCOPED_LOCKABLE
  155. //
  156. // Documents if a class does RAII locking (such as the `MutexLock` class).
  157. // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
  158. // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
  159. // arguments; the analysis will assume that the destructor unlocks whatever the
  160. // constructor locked.
  161. #define SCOPED_LOCKABLE \
  162. THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  163. // EXCLUSIVE_LOCK_FUNCTION()
  164. //
  165. // Documents functions that acquire a lock in the body of a function, and do
  166. // not release it.
  167. #define EXCLUSIVE_LOCK_FUNCTION(...) \
  168. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
  169. // SHARED_LOCK_FUNCTION()
  170. //
  171. // Documents functions that acquire a shared (reader) lock in the body of a
  172. // function, and do not release it.
  173. #define SHARED_LOCK_FUNCTION(...) \
  174. THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
  175. // UNLOCK_FUNCTION()
  176. //
  177. // Documents functions that expect a lock to be held on entry to the function,
  178. // and release it in the body of the function.
  179. #define UNLOCK_FUNCTION(...) \
  180. THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
  181. // EXCLUSIVE_TRYLOCK_FUNCTION() / SHARED_TRYLOCK_FUNCTION()
  182. //
  183. // Documents functions that try to acquire a lock, and return success or failure
  184. // (or a non-boolean value that can be interpreted as a boolean).
  185. // The first argument should be `true` for functions that return `true` on
  186. // success, or `false` for functions that return `false` on success. The second
  187. // argument specifies the mutex that is locked on success. If unspecified, this
  188. // mutex is assumed to be `this`.
  189. #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
  190. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
  191. #define SHARED_TRYLOCK_FUNCTION(...) \
  192. THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
  193. // ASSERT_EXCLUSIVE_LOCK() / ASSERT_SHARED_LOCK()
  194. //
  195. // Documents functions that dynamically check to see if a lock is held, and fail
  196. // if it is not held.
  197. #define ASSERT_EXCLUSIVE_LOCK(...) \
  198. THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__))
  199. #define ASSERT_SHARED_LOCK(...) \
  200. THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__))
  201. // NO_THREAD_SAFETY_ANALYSIS
  202. //
  203. // Turns off thread safety checking within the body of a particular function.
  204. // This annotation is used to mark functions that are known to be correct, but
  205. // the locking behavior is more complicated than the analyzer can handle.
  206. #define NO_THREAD_SAFETY_ANALYSIS \
  207. THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  208. //------------------------------------------------------------------------------
  209. // Tool-Supplied Annotations
  210. //------------------------------------------------------------------------------
  211. // TS_UNCHECKED should be placed around lock expressions that are not valid
  212. // C++ syntax, but which are present for documentation purposes. These
  213. // annotations will be ignored by the analysis.
  214. #define TS_UNCHECKED(x) ""
  215. // TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
  216. // It is used by automated tools to mark and disable invalid expressions.
  217. // The annotation should either be fixed, or changed to TS_UNCHECKED.
  218. #define TS_FIXME(x) ""
  219. // Like NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body of
  220. // a particular function. However, this attribute is used to mark functions
  221. // that are incorrect and need to be fixed. It is used by automated tools to
  222. // avoid breaking the build when the analysis is updated.
  223. // Code owners are expected to eventually fix the routine.
  224. #define NO_THREAD_SAFETY_ANALYSIS_FIXME NO_THREAD_SAFETY_ANALYSIS
  225. // Similar to NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a GUARDED_BY
  226. // annotation that needs to be fixed, because it is producing thread safety
  227. // warning. It disables the GUARDED_BY.
  228. #define GUARDED_BY_FIXME(x)
  229. // Disables warnings for a single read operation. This can be used to avoid
  230. // warnings when it is known that the read is not actually involved in a race,
  231. // but the compiler cannot confirm that.
  232. #define TS_UNCHECKED_READ(x) thread_safety_analysis::ts_unchecked_read(x)
  233. namespace thread_safety_analysis {
  234. // Takes a reference to a guarded data member, and returns an unguarded
  235. // reference.
  236. template <typename T>
  237. inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS {
  238. return v;
  239. }
  240. template <typename T>
  241. inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS {
  242. return v;
  243. }
  244. } // namespace thread_safety_analysis
  245. #endif // defined(ABSL_LEGACY_THREAD_ANNOTATIONS)
  246. #endif // ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_