thread_annotations.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #if defined(__clang__)
  40. #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  41. #else
  42. #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
  43. #endif
  44. // GUARDED_BY()
  45. //
  46. // Documents if a shared field or global variable needs to be protected by a
  47. // mutex. GUARDED_BY() allows the user to specify a particular mutex that
  48. // should be held when accessing the annotated variable.
  49. //
  50. // Although this annotation (and PT_GUARDED_BY, below) cannot be applied to
  51. // local variables, a local variable and its associated mutex can often be
  52. // combined into a small class or struct, thereby allowing the annotation.
  53. //
  54. // Example:
  55. //
  56. // class Foo {
  57. // Mutex mu_;
  58. // int p1_ GUARDED_BY(mu_);
  59. // ...
  60. // };
  61. #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  62. // PT_GUARDED_BY()
  63. //
  64. // Documents if the memory location pointed to by a pointer should be guarded
  65. // by a mutex when dereferencing the pointer.
  66. //
  67. // Example:
  68. // class Foo {
  69. // Mutex mu_;
  70. // int *p1_ PT_GUARDED_BY(mu_);
  71. // ...
  72. // };
  73. //
  74. // Note that a pointer variable to a shared memory location could itself be a
  75. // shared variable.
  76. //
  77. // Example:
  78. //
  79. // // `q_`, guarded by `mu1_`, points to a shared memory location that is
  80. // // guarded by `mu2_`:
  81. // int *q_ GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_);
  82. #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  83. // ACQUIRED_AFTER() / ACQUIRED_BEFORE()
  84. //
  85. // Documents the acquisition order between locks that can be held
  86. // simultaneously by a thread. For any two locks that need to be annotated
  87. // to establish an acquisition order, only one of them needs the annotation.
  88. // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
  89. // and ACQUIRED_BEFORE.)
  90. //
  91. // As with GUARDED_BY, this is only applicable to mutexes that are shared
  92. // fields or global variables.
  93. //
  94. // Example:
  95. //
  96. // Mutex m1_;
  97. // Mutex m2_ ACQUIRED_AFTER(m1_);
  98. #define ACQUIRED_AFTER(...) \
  99. THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
  100. #define ACQUIRED_BEFORE(...) \
  101. THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
  102. // EXCLUSIVE_LOCKS_REQUIRED() / SHARED_LOCKS_REQUIRED()
  103. //
  104. // Documents a function that expects a mutex to be held prior to entry.
  105. // The mutex is expected to be held both on entry to, and exit from, the
  106. // function.
  107. //
  108. // An exclusive lock allows read-write access to the guarded data member(s), and
  109. // only one thread can acquire a lock exclusively at any one time. A shared lock
  110. // allows read-only access, and any number of threads can acquire a shared lock
  111. // concurrently.
  112. //
  113. // Generally, non-const methods should be annotated with
  114. // EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
  115. // SHARED_LOCKS_REQUIRED.
  116. //
  117. // Example:
  118. //
  119. // Mutex mu1, mu2;
  120. // int a GUARDED_BY(mu1);
  121. // int b GUARDED_BY(mu2);
  122. //
  123. // void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
  124. // void bar() const SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
  125. #define EXCLUSIVE_LOCKS_REQUIRED(...) \
  126. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
  127. #define SHARED_LOCKS_REQUIRED(...) \
  128. THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
  129. // LOCKS_EXCLUDED()
  130. //
  131. // Documents the locks acquired in the body of the function. These locks
  132. // cannot be held when calling this function (as Abseil's `Mutex` locks are
  133. // non-reentrant).
  134. #define LOCKS_EXCLUDED(...) \
  135. THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  136. // LOCK_RETURNED()
  137. //
  138. // Documents a function that returns a mutex without acquiring it. For example,
  139. // a public getter method that returns a pointer to a private mutex should
  140. // be annotated with LOCK_RETURNED.
  141. #define LOCK_RETURNED(x) \
  142. THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  143. // LOCKABLE
  144. //
  145. // Documents if a class/type is a lockable type (such as the `Mutex` class).
  146. #define LOCKABLE \
  147. THREAD_ANNOTATION_ATTRIBUTE__(lockable)
  148. // SCOPED_LOCKABLE
  149. //
  150. // Documents if a class does RAII locking (such as the `MutexLock` class).
  151. // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
  152. // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
  153. // arguments; the analysis will assume that the destructor unlocks whatever the
  154. // constructor locked.
  155. #define SCOPED_LOCKABLE \
  156. THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  157. // EXCLUSIVE_LOCK_FUNCTION()
  158. //
  159. // Documents functions that acquire a lock in the body of a function, and do
  160. // not release it.
  161. #define EXCLUSIVE_LOCK_FUNCTION(...) \
  162. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
  163. // SHARED_LOCK_FUNCTION()
  164. //
  165. // Documents functions that acquire a shared (reader) lock in the body of a
  166. // function, and do not release it.
  167. #define SHARED_LOCK_FUNCTION(...) \
  168. THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
  169. // UNLOCK_FUNCTION()
  170. //
  171. // Documents functions that expect a lock to be held on entry to the function,
  172. // and release it in the body of the function.
  173. #define UNLOCK_FUNCTION(...) \
  174. THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
  175. // EXCLUSIVE_TRYLOCK_FUNCTION() / SHARED_TRYLOCK_FUNCTION()
  176. //
  177. // Documents functions that try to acquire a lock, and return success or failure
  178. // (or a non-boolean value that can be interpreted as a boolean).
  179. // The first argument should be `true` for functions that return `true` on
  180. // success, or `false` for functions that return `false` on success. The second
  181. // argument specifies the mutex that is locked on success. If unspecified, this
  182. // mutex is assumed to be `this`.
  183. #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
  184. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
  185. #define SHARED_TRYLOCK_FUNCTION(...) \
  186. THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
  187. // ASSERT_EXCLUSIVE_LOCK() / ASSERT_SHARED_LOCK()
  188. //
  189. // Documents functions that dynamically check to see if a lock is held, and fail
  190. // if it is not held.
  191. #define ASSERT_EXCLUSIVE_LOCK(...) \
  192. THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__))
  193. #define ASSERT_SHARED_LOCK(...) \
  194. THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__))
  195. // NO_THREAD_SAFETY_ANALYSIS
  196. //
  197. // Turns off thread safety checking within the body of a particular function.
  198. // This annotation is used to mark functions that are known to be correct, but
  199. // the locking behavior is more complicated than the analyzer can handle.
  200. #define NO_THREAD_SAFETY_ANALYSIS \
  201. THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  202. //------------------------------------------------------------------------------
  203. // Tool-Supplied Annotations
  204. //------------------------------------------------------------------------------
  205. // TS_UNCHECKED should be placed around lock expressions that are not valid
  206. // C++ syntax, but which are present for documentation purposes. These
  207. // annotations will be ignored by the analysis.
  208. #define TS_UNCHECKED(x) ""
  209. // TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
  210. // It is used by automated tools to mark and disable invalid expressions.
  211. // The annotation should either be fixed, or changed to TS_UNCHECKED.
  212. #define TS_FIXME(x) ""
  213. // Like NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body of
  214. // a particular function. However, this attribute is used to mark functions
  215. // that are incorrect and need to be fixed. It is used by automated tools to
  216. // avoid breaking the build when the analysis is updated.
  217. // Code owners are expected to eventually fix the routine.
  218. #define NO_THREAD_SAFETY_ANALYSIS_FIXME NO_THREAD_SAFETY_ANALYSIS
  219. // Similar to NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a GUARDED_BY
  220. // annotation that needs to be fixed, because it is producing thread safety
  221. // warning. It disables the GUARDED_BY.
  222. #define GUARDED_BY_FIXME(x)
  223. // Disables warnings for a single read operation. This can be used to avoid
  224. // warnings when it is known that the read is not actually involved in a race,
  225. // but the compiler cannot confirm that.
  226. #define TS_UNCHECKED_READ(x) thread_safety_analysis::ts_unchecked_read(x)
  227. namespace thread_safety_analysis {
  228. // Takes a reference to a guarded data member, and returns an unguarded
  229. // reference.
  230. template <typename T>
  231. inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS {
  232. return v;
  233. }
  234. template <typename T>
  235. inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS {
  236. return v;
  237. }
  238. } // namespace thread_safety_analysis
  239. #endif // ABSL_BASE_INTERNAL_THREAD_ANNOTATIONS_H_