thread_annotations.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2017 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. // This header file contains macro definitions for thread safety annotations
  20. // that allow developers to document the locking policies of multi-threaded
  21. // code. The annotations can also help program analysis tools to identify
  22. // potential thread safety issues.
  23. //
  24. // These annotations are implemented using compiler attributes. Using the macros
  25. // defined here instead of raw attributes allow for portability and future
  26. // compatibility.
  27. //
  28. // When referring to mutexes in the arguments of the attributes, you should
  29. // use variable names or more complex expressions (e.g. my_object->mutex_)
  30. // that evaluate to a concrete mutex object whenever possible. If the mutex
  31. // you want to refer to is not in scope, you may use a member pointer
  32. // (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object.
  33. #ifndef Y_ABSL_BASE_THREAD_ANNOTATIONS_H_
  34. #define Y_ABSL_BASE_THREAD_ANNOTATIONS_H_
  35. #include "y_absl/base/attributes.h"
  36. #include "y_absl/base/config.h"
  37. // TODO(mbonadei): Remove after the backward compatibility period.
  38. #include "y_absl/base/internal/thread_annotations.h" // IWYU pragma: export
  39. // Y_ABSL_GUARDED_BY()
  40. //
  41. // Documents if a shared field or global variable needs to be protected by a
  42. // mutex. Y_ABSL_GUARDED_BY() allows the user to specify a particular mutex that
  43. // should be held when accessing the annotated variable.
  44. //
  45. // Although this annotation (and Y_ABSL_PT_GUARDED_BY, below) cannot be applied to
  46. // local variables, a local variable and its associated mutex can often be
  47. // combined into a small class or struct, thereby allowing the annotation.
  48. //
  49. // Example:
  50. //
  51. // class Foo {
  52. // Mutex mu_;
  53. // int p1_ Y_ABSL_GUARDED_BY(mu_);
  54. // ...
  55. // };
  56. #if Y_ABSL_HAVE_ATTRIBUTE(guarded_by)
  57. #define Y_ABSL_GUARDED_BY(x) __attribute__((guarded_by(x)))
  58. #else
  59. #define Y_ABSL_GUARDED_BY(x)
  60. #endif
  61. // Y_ABSL_PT_GUARDED_BY()
  62. //
  63. // Documents if the memory location pointed to by a pointer should be guarded
  64. // by a mutex when dereferencing the pointer.
  65. //
  66. // Example:
  67. // class Foo {
  68. // Mutex mu_;
  69. // int *p1_ Y_ABSL_PT_GUARDED_BY(mu_);
  70. // ...
  71. // };
  72. //
  73. // Note that a pointer variable to a shared memory location could itself be a
  74. // shared variable.
  75. //
  76. // Example:
  77. //
  78. // // `q_`, guarded by `mu1_`, points to a shared memory location that is
  79. // // guarded by `mu2_`:
  80. // int *q_ Y_ABSL_GUARDED_BY(mu1_) Y_ABSL_PT_GUARDED_BY(mu2_);
  81. #if Y_ABSL_HAVE_ATTRIBUTE(pt_guarded_by)
  82. #define Y_ABSL_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
  83. #else
  84. #define Y_ABSL_PT_GUARDED_BY(x)
  85. #endif
  86. // Y_ABSL_ACQUIRED_AFTER() / Y_ABSL_ACQUIRED_BEFORE()
  87. //
  88. // Documents the acquisition order between locks that can be held
  89. // simultaneously by a thread. For any two locks that need to be annotated
  90. // to establish an acquisition order, only one of them needs the annotation.
  91. // (i.e. You don't have to annotate both locks with both Y_ABSL_ACQUIRED_AFTER
  92. // and Y_ABSL_ACQUIRED_BEFORE.)
  93. //
  94. // As with Y_ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
  95. // fields or global variables.
  96. //
  97. // Example:
  98. //
  99. // Mutex m1_;
  100. // Mutex m2_ Y_ABSL_ACQUIRED_AFTER(m1_);
  101. #if Y_ABSL_HAVE_ATTRIBUTE(acquired_after)
  102. #define Y_ABSL_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
  103. #else
  104. #define Y_ABSL_ACQUIRED_AFTER(...)
  105. #endif
  106. #if Y_ABSL_HAVE_ATTRIBUTE(acquired_before)
  107. #define Y_ABSL_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
  108. #else
  109. #define Y_ABSL_ACQUIRED_BEFORE(...)
  110. #endif
  111. // Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED() / Y_ABSL_SHARED_LOCKS_REQUIRED()
  112. //
  113. // Documents a function that expects a mutex to be held prior to entry.
  114. // The mutex is expected to be held both on entry to, and exit from, the
  115. // function.
  116. //
  117. // An exclusive lock allows read-write access to the guarded data member(s), and
  118. // only one thread can acquire a lock exclusively at any one time. A shared lock
  119. // allows read-only access, and any number of threads can acquire a shared lock
  120. // concurrently.
  121. //
  122. // Generally, non-const methods should be annotated with
  123. // Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
  124. // Y_ABSL_SHARED_LOCKS_REQUIRED.
  125. //
  126. // Example:
  127. //
  128. // Mutex mu1, mu2;
  129. // int a Y_ABSL_GUARDED_BY(mu1);
  130. // int b Y_ABSL_GUARDED_BY(mu2);
  131. //
  132. // void foo() Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
  133. // void bar() const Y_ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
  134. #if Y_ABSL_HAVE_ATTRIBUTE(exclusive_locks_required)
  135. #define Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \
  136. __attribute__((exclusive_locks_required(__VA_ARGS__)))
  137. #else
  138. #define Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
  139. #endif
  140. #if Y_ABSL_HAVE_ATTRIBUTE(shared_locks_required)
  141. #define Y_ABSL_SHARED_LOCKS_REQUIRED(...) \
  142. __attribute__((shared_locks_required(__VA_ARGS__)))
  143. #else
  144. #define Y_ABSL_SHARED_LOCKS_REQUIRED(...)
  145. #endif
  146. // Y_ABSL_LOCKS_EXCLUDED()
  147. //
  148. // Documents the locks that cannot be held by callers of this function, as they
  149. // might be acquired by this function (Abseil's `Mutex` locks are
  150. // non-reentrant).
  151. #if Y_ABSL_HAVE_ATTRIBUTE(locks_excluded)
  152. #define Y_ABSL_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
  153. #else
  154. #define Y_ABSL_LOCKS_EXCLUDED(...)
  155. #endif
  156. // Y_ABSL_LOCK_RETURNED()
  157. //
  158. // Documents a function that returns a mutex without acquiring it. For example,
  159. // a public getter method that returns a pointer to a private mutex should
  160. // be annotated with Y_ABSL_LOCK_RETURNED.
  161. #if Y_ABSL_HAVE_ATTRIBUTE(lock_returned)
  162. #define Y_ABSL_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
  163. #else
  164. #define Y_ABSL_LOCK_RETURNED(x)
  165. #endif
  166. // Y_ABSL_LOCKABLE
  167. //
  168. // Documents if a class/type is a lockable type (such as the `Mutex` class).
  169. #if Y_ABSL_HAVE_ATTRIBUTE(lockable)
  170. #define Y_ABSL_LOCKABLE __attribute__((lockable))
  171. #else
  172. #define Y_ABSL_LOCKABLE
  173. #endif
  174. // Y_ABSL_SCOPED_LOCKABLE
  175. //
  176. // Documents if a class does RAII locking (such as the `MutexLock` class).
  177. // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
  178. // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
  179. // arguments; the analysis will assume that the destructor unlocks whatever the
  180. // constructor locked.
  181. #if Y_ABSL_HAVE_ATTRIBUTE(scoped_lockable)
  182. #define Y_ABSL_SCOPED_LOCKABLE __attribute__((scoped_lockable))
  183. #else
  184. #define Y_ABSL_SCOPED_LOCKABLE
  185. #endif
  186. // Y_ABSL_EXCLUSIVE_LOCK_FUNCTION()
  187. //
  188. // Documents functions that acquire a lock in the body of a function, and do
  189. // not release it.
  190. #if Y_ABSL_HAVE_ATTRIBUTE(exclusive_lock_function)
  191. #define Y_ABSL_EXCLUSIVE_LOCK_FUNCTION(...) \
  192. __attribute__((exclusive_lock_function(__VA_ARGS__)))
  193. #else
  194. #define Y_ABSL_EXCLUSIVE_LOCK_FUNCTION(...)
  195. #endif
  196. // Y_ABSL_SHARED_LOCK_FUNCTION()
  197. //
  198. // Documents functions that acquire a shared (reader) lock in the body of a
  199. // function, and do not release it.
  200. #if Y_ABSL_HAVE_ATTRIBUTE(shared_lock_function)
  201. #define Y_ABSL_SHARED_LOCK_FUNCTION(...) \
  202. __attribute__((shared_lock_function(__VA_ARGS__)))
  203. #else
  204. #define Y_ABSL_SHARED_LOCK_FUNCTION(...)
  205. #endif
  206. // Y_ABSL_UNLOCK_FUNCTION()
  207. //
  208. // Documents functions that expect a lock to be held on entry to the function,
  209. // and release it in the body of the function.
  210. #if Y_ABSL_HAVE_ATTRIBUTE(unlock_function)
  211. #define Y_ABSL_UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
  212. #else
  213. #define Y_ABSL_UNLOCK_FUNCTION(...)
  214. #endif
  215. // Y_ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / Y_ABSL_SHARED_TRYLOCK_FUNCTION()
  216. //
  217. // Documents functions that try to acquire a lock, and return success or failure
  218. // (or a non-boolean value that can be interpreted as a boolean).
  219. // The first argument should be `true` for functions that return `true` on
  220. // success, or `false` for functions that return `false` on success. The second
  221. // argument specifies the mutex that is locked on success. If unspecified, this
  222. // mutex is assumed to be `this`.
  223. #if Y_ABSL_HAVE_ATTRIBUTE(exclusive_trylock_function)
  224. #define Y_ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
  225. __attribute__((exclusive_trylock_function(__VA_ARGS__)))
  226. #else
  227. #define Y_ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...)
  228. #endif
  229. #if Y_ABSL_HAVE_ATTRIBUTE(shared_trylock_function)
  230. #define Y_ABSL_SHARED_TRYLOCK_FUNCTION(...) \
  231. __attribute__((shared_trylock_function(__VA_ARGS__)))
  232. #else
  233. #define Y_ABSL_SHARED_TRYLOCK_FUNCTION(...)
  234. #endif
  235. // Y_ABSL_ASSERT_EXCLUSIVE_LOCK() / Y_ABSL_ASSERT_SHARED_LOCK()
  236. //
  237. // Documents functions that dynamically check to see if a lock is held, and fail
  238. // if it is not held.
  239. #if Y_ABSL_HAVE_ATTRIBUTE(assert_exclusive_lock)
  240. #define Y_ABSL_ASSERT_EXCLUSIVE_LOCK(...) \
  241. __attribute__((assert_exclusive_lock(__VA_ARGS__)))
  242. #else
  243. #define Y_ABSL_ASSERT_EXCLUSIVE_LOCK(...)
  244. #endif
  245. #if Y_ABSL_HAVE_ATTRIBUTE(assert_shared_lock)
  246. #define Y_ABSL_ASSERT_SHARED_LOCK(...) \
  247. __attribute__((assert_shared_lock(__VA_ARGS__)))
  248. #else
  249. #define Y_ABSL_ASSERT_SHARED_LOCK(...)
  250. #endif
  251. // Y_ABSL_NO_THREAD_SAFETY_ANALYSIS
  252. //
  253. // Turns off thread safety checking within the body of a particular function.
  254. // This annotation is used to mark functions that are known to be correct, but
  255. // the locking behavior is more complicated than the analyzer can handle.
  256. #if Y_ABSL_HAVE_ATTRIBUTE(no_thread_safety_analysis)
  257. #define Y_ABSL_NO_THREAD_SAFETY_ANALYSIS \
  258. __attribute__((no_thread_safety_analysis))
  259. #else
  260. #define Y_ABSL_NO_THREAD_SAFETY_ANALYSIS
  261. #endif
  262. //------------------------------------------------------------------------------
  263. // Tool-Supplied Annotations
  264. //------------------------------------------------------------------------------
  265. // Y_ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid
  266. // C++ syntax, but which are present for documentation purposes. These
  267. // annotations will be ignored by the analysis.
  268. #define Y_ABSL_TS_UNCHECKED(x) ""
  269. // Y_ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
  270. // It is used by automated tools to mark and disable invalid expressions.
  271. // The annotation should either be fixed, or changed to Y_ABSL_TS_UNCHECKED.
  272. #define Y_ABSL_TS_FIXME(x) ""
  273. // Like Y_ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body
  274. // of a particular function. However, this attribute is used to mark functions
  275. // that are incorrect and need to be fixed. It is used by automated tools to
  276. // avoid breaking the build when the analysis is updated.
  277. // Code owners are expected to eventually fix the routine.
  278. #define Y_ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME Y_ABSL_NO_THREAD_SAFETY_ANALYSIS
  279. // Similar to Y_ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a
  280. // Y_ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing
  281. // thread safety warning. It disables the Y_ABSL_GUARDED_BY.
  282. #define Y_ABSL_GUARDED_BY_FIXME(x)
  283. // Disables warnings for a single read operation. This can be used to avoid
  284. // warnings when it is known that the read is not actually involved in a race,
  285. // but the compiler cannot confirm that.
  286. #define Y_ABSL_TS_UNCHECKED_READ(x) y_absl::base_internal::y_ts_unchecked_read(x)
  287. namespace y_absl {
  288. Y_ABSL_NAMESPACE_BEGIN
  289. namespace base_internal {
  290. // Takes a reference to a guarded data member, and returns an unguarded
  291. // reference.
  292. // Do not use this function directly, use Y_ABSL_TS_UNCHECKED_READ instead.
  293. template <typename T>
  294. inline const T& y_ts_unchecked_read(const T& v) Y_ABSL_NO_THREAD_SAFETY_ANALYSIS {
  295. return v;
  296. }
  297. template <typename T>
  298. inline T& y_ts_unchecked_read(T& v) Y_ABSL_NO_THREAD_SAFETY_ANALYSIS {
  299. return v;
  300. }
  301. } // namespace base_internal
  302. Y_ABSL_NAMESPACE_END
  303. } // namespace y_absl
  304. #endif // Y_ABSL_BASE_THREAD_ANNOTATIONS_H_