thread_annotations.h 12 KB

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