Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:43

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: thread_annotations.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header file contains macro definitions for thread safety annotations
0020 // that allow developers to document the locking policies of multi-threaded
0021 // code. The annotations can also help program analysis tools to identify
0022 // potential thread safety issues.
0023 //
0024 // These annotations are implemented using compiler attributes. Using the macros
0025 // defined here instead of raw attributes allow for portability and future
0026 // compatibility.
0027 //
0028 // When referring to mutexes in the arguments of the attributes, you should
0029 // use variable names or more complex expressions (e.g. my_object->mutex_)
0030 // that evaluate to a concrete mutex object whenever possible. If the mutex
0031 // you want to refer to is not in scope, you may use a member pointer
0032 // (e.g. &MyClass::mutex_) to refer to a mutex in some (unknown) object.
0033 
0034 #ifndef ABSL_BASE_THREAD_ANNOTATIONS_H_
0035 #define ABSL_BASE_THREAD_ANNOTATIONS_H_
0036 
0037 #include "absl/base/attributes.h"
0038 #include "absl/base/config.h"
0039 
0040 // ABSL_GUARDED_BY()
0041 //
0042 // Documents if a shared field or global variable needs to be protected by a
0043 // mutex. ABSL_GUARDED_BY() allows the user to specify a particular mutex that
0044 // should be held when accessing the annotated variable.
0045 //
0046 // Although this annotation (and ABSL_PT_GUARDED_BY, below) cannot be applied to
0047 // local variables, a local variable and its associated mutex can often be
0048 // combined into a small class or struct, thereby allowing the annotation.
0049 //
0050 // Example:
0051 //
0052 //   class Foo {
0053 //     Mutex mu_;
0054 //     int p1_ ABSL_GUARDED_BY(mu_);
0055 //     ...
0056 //   };
0057 #if ABSL_HAVE_ATTRIBUTE(guarded_by)
0058 #define ABSL_GUARDED_BY(x) __attribute__((guarded_by(x)))
0059 #else
0060 #define ABSL_GUARDED_BY(x)
0061 #endif
0062 
0063 // ABSL_PT_GUARDED_BY()
0064 //
0065 // Documents if the memory location pointed to by a pointer should be guarded
0066 // by a mutex when dereferencing the pointer.
0067 //
0068 // Example:
0069 //   class Foo {
0070 //     Mutex mu_;
0071 //     int *p1_ ABSL_PT_GUARDED_BY(mu_);
0072 //     ...
0073 //   };
0074 //
0075 // Note that a pointer variable to a shared memory location could itself be a
0076 // shared variable.
0077 //
0078 // Example:
0079 //
0080 //   // `q_`, guarded by `mu1_`, points to a shared memory location that is
0081 //   // guarded by `mu2_`:
0082 //   int *q_ ABSL_GUARDED_BY(mu1_) ABSL_PT_GUARDED_BY(mu2_);
0083 #if ABSL_HAVE_ATTRIBUTE(pt_guarded_by)
0084 #define ABSL_PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x)))
0085 #else
0086 #define ABSL_PT_GUARDED_BY(x)
0087 #endif
0088 
0089 // ABSL_ACQUIRED_AFTER() / ABSL_ACQUIRED_BEFORE()
0090 //
0091 // Documents the acquisition order between locks that can be held
0092 // simultaneously by a thread. For any two locks that need to be annotated
0093 // to establish an acquisition order, only one of them needs the annotation.
0094 // (i.e. You don't have to annotate both locks with both ABSL_ACQUIRED_AFTER
0095 // and ABSL_ACQUIRED_BEFORE.)
0096 //
0097 // As with ABSL_GUARDED_BY, this is only applicable to mutexes that are shared
0098 // fields or global variables.
0099 //
0100 // Example:
0101 //
0102 //   Mutex m1_;
0103 //   Mutex m2_ ABSL_ACQUIRED_AFTER(m1_);
0104 #if ABSL_HAVE_ATTRIBUTE(acquired_after)
0105 #define ABSL_ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
0106 #else
0107 #define ABSL_ACQUIRED_AFTER(...)
0108 #endif
0109 
0110 #if ABSL_HAVE_ATTRIBUTE(acquired_before)
0111 #define ABSL_ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
0112 #else
0113 #define ABSL_ACQUIRED_BEFORE(...)
0114 #endif
0115 
0116 // ABSL_EXCLUSIVE_LOCKS_REQUIRED() / ABSL_SHARED_LOCKS_REQUIRED()
0117 //
0118 // Documents a function that expects a mutex to be held prior to entry.
0119 // The mutex is expected to be held both on entry to, and exit from, the
0120 // function.
0121 //
0122 // An exclusive lock allows read-write access to the guarded data member(s), and
0123 // only one thread can acquire a lock exclusively at any one time. A shared lock
0124 // allows read-only access, and any number of threads can acquire a shared lock
0125 // concurrently.
0126 //
0127 // Generally, non-const methods should be annotated with
0128 // ABSL_EXCLUSIVE_LOCKS_REQUIRED, while const methods should be annotated with
0129 // ABSL_SHARED_LOCKS_REQUIRED.
0130 //
0131 // Example:
0132 //
0133 //   Mutex mu1, mu2;
0134 //   int a ABSL_GUARDED_BY(mu1);
0135 //   int b ABSL_GUARDED_BY(mu2);
0136 //
0137 //   void foo() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2) { ... }
0138 //   void bar() const ABSL_SHARED_LOCKS_REQUIRED(mu1, mu2) { ... }
0139 #if ABSL_HAVE_ATTRIBUTE(exclusive_locks_required)
0140 #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...) \
0141   __attribute__((exclusive_locks_required(__VA_ARGS__)))
0142 #else
0143 #define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
0144 #endif
0145 
0146 #if ABSL_HAVE_ATTRIBUTE(shared_locks_required)
0147 #define ABSL_SHARED_LOCKS_REQUIRED(...) \
0148   __attribute__((shared_locks_required(__VA_ARGS__)))
0149 #else
0150 #define ABSL_SHARED_LOCKS_REQUIRED(...)
0151 #endif
0152 
0153 // ABSL_LOCKS_EXCLUDED()
0154 //
0155 // Documents the locks that cannot be held by callers of this function, as they
0156 // might be acquired by this function (Abseil's `Mutex` locks are
0157 // non-reentrant).
0158 #if ABSL_HAVE_ATTRIBUTE(locks_excluded)
0159 #define ABSL_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
0160 #else
0161 #define ABSL_LOCKS_EXCLUDED(...)
0162 #endif
0163 
0164 // ABSL_LOCK_RETURNED()
0165 //
0166 // Documents a function that returns a mutex without acquiring it.  For example,
0167 // a public getter method that returns a pointer to a private mutex should
0168 // be annotated with ABSL_LOCK_RETURNED.
0169 #if ABSL_HAVE_ATTRIBUTE(lock_returned)
0170 #define ABSL_LOCK_RETURNED(x) __attribute__((lock_returned(x)))
0171 #else
0172 #define ABSL_LOCK_RETURNED(x)
0173 #endif
0174 
0175 // ABSL_LOCKABLE
0176 //
0177 // Documents if a class/type is a lockable type (such as the `Mutex` class).
0178 #if ABSL_HAVE_ATTRIBUTE(lockable)
0179 #define ABSL_LOCKABLE __attribute__((lockable))
0180 #else
0181 #define ABSL_LOCKABLE
0182 #endif
0183 
0184 // ABSL_SCOPED_LOCKABLE
0185 //
0186 // Documents if a class does RAII locking (such as the `MutexLock` class).
0187 // The constructor should use `LOCK_FUNCTION()` to specify the mutex that is
0188 // acquired, and the destructor should use `UNLOCK_FUNCTION()` with no
0189 // arguments; the analysis will assume that the destructor unlocks whatever the
0190 // constructor locked.
0191 #if ABSL_HAVE_ATTRIBUTE(scoped_lockable)
0192 #define ABSL_SCOPED_LOCKABLE __attribute__((scoped_lockable))
0193 #else
0194 #define ABSL_SCOPED_LOCKABLE
0195 #endif
0196 
0197 // ABSL_EXCLUSIVE_LOCK_FUNCTION()
0198 //
0199 // Documents functions that acquire a lock in the body of a function, and do
0200 // not release it.
0201 #if ABSL_HAVE_ATTRIBUTE(exclusive_lock_function)
0202 #define ABSL_EXCLUSIVE_LOCK_FUNCTION(...) \
0203   __attribute__((exclusive_lock_function(__VA_ARGS__)))
0204 #else
0205 #define ABSL_EXCLUSIVE_LOCK_FUNCTION(...)
0206 #endif
0207 
0208 // ABSL_SHARED_LOCK_FUNCTION()
0209 //
0210 // Documents functions that acquire a shared (reader) lock in the body of a
0211 // function, and do not release it.
0212 #if ABSL_HAVE_ATTRIBUTE(shared_lock_function)
0213 #define ABSL_SHARED_LOCK_FUNCTION(...) \
0214   __attribute__((shared_lock_function(__VA_ARGS__)))
0215 #else
0216 #define ABSL_SHARED_LOCK_FUNCTION(...)
0217 #endif
0218 
0219 // ABSL_UNLOCK_FUNCTION()
0220 //
0221 // Documents functions that expect a lock to be held on entry to the function,
0222 // and release it in the body of the function.
0223 #if ABSL_HAVE_ATTRIBUTE(unlock_function)
0224 #define ABSL_UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
0225 #else
0226 #define ABSL_UNLOCK_FUNCTION(...)
0227 #endif
0228 
0229 // ABSL_EXCLUSIVE_TRYLOCK_FUNCTION() / ABSL_SHARED_TRYLOCK_FUNCTION()
0230 //
0231 // Documents functions that try to acquire a lock, and return success or failure
0232 // (or a non-boolean value that can be interpreted as a boolean).
0233 // The first argument should be `true` for functions that return `true` on
0234 // success, or `false` for functions that return `false` on success. The second
0235 // argument specifies the mutex that is locked on success. If unspecified, this
0236 // mutex is assumed to be `this`.
0237 #if ABSL_HAVE_ATTRIBUTE(exclusive_trylock_function)
0238 #define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
0239   __attribute__((exclusive_trylock_function(__VA_ARGS__)))
0240 #else
0241 #define ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(...)
0242 #endif
0243 
0244 #if ABSL_HAVE_ATTRIBUTE(shared_trylock_function)
0245 #define ABSL_SHARED_TRYLOCK_FUNCTION(...) \
0246   __attribute__((shared_trylock_function(__VA_ARGS__)))
0247 #else
0248 #define ABSL_SHARED_TRYLOCK_FUNCTION(...)
0249 #endif
0250 
0251 // ABSL_ASSERT_EXCLUSIVE_LOCK() / ABSL_ASSERT_SHARED_LOCK()
0252 //
0253 // Documents functions that dynamically check to see if a lock is held, and fail
0254 // if it is not held.
0255 #if ABSL_HAVE_ATTRIBUTE(assert_exclusive_lock)
0256 #define ABSL_ASSERT_EXCLUSIVE_LOCK(...) \
0257   __attribute__((assert_exclusive_lock(__VA_ARGS__)))
0258 #else
0259 #define ABSL_ASSERT_EXCLUSIVE_LOCK(...)
0260 #endif
0261 
0262 #if ABSL_HAVE_ATTRIBUTE(assert_shared_lock)
0263 #define ABSL_ASSERT_SHARED_LOCK(...) \
0264   __attribute__((assert_shared_lock(__VA_ARGS__)))
0265 #else
0266 #define ABSL_ASSERT_SHARED_LOCK(...)
0267 #endif
0268 
0269 // ABSL_NO_THREAD_SAFETY_ANALYSIS
0270 //
0271 // Turns off thread safety checking within the body of a particular function.
0272 // This annotation is used to mark functions that are known to be correct, but
0273 // the locking behavior is more complicated than the analyzer can handle.
0274 #if ABSL_HAVE_ATTRIBUTE(no_thread_safety_analysis)
0275 #define ABSL_NO_THREAD_SAFETY_ANALYSIS \
0276   __attribute__((no_thread_safety_analysis))
0277 #else
0278 #define ABSL_NO_THREAD_SAFETY_ANALYSIS
0279 #endif
0280 
0281 //------------------------------------------------------------------------------
0282 // Tool-Supplied Annotations
0283 //------------------------------------------------------------------------------
0284 
0285 // ABSL_TS_UNCHECKED should be placed around lock expressions that are not valid
0286 // C++ syntax, but which are present for documentation purposes.  These
0287 // annotations will be ignored by the analysis.
0288 #define ABSL_TS_UNCHECKED(x) ""
0289 
0290 // ABSL_TS_FIXME is used to mark lock expressions that are not valid C++ syntax.
0291 // It is used by automated tools to mark and disable invalid expressions.
0292 // The annotation should either be fixed, or changed to ABSL_TS_UNCHECKED.
0293 #define ABSL_TS_FIXME(x) ""
0294 
0295 // Like ABSL_NO_THREAD_SAFETY_ANALYSIS, this turns off checking within the body
0296 // of a particular function.  However, this attribute is used to mark functions
0297 // that are incorrect and need to be fixed.  It is used by automated tools to
0298 // avoid breaking the build when the analysis is updated.
0299 // Code owners are expected to eventually fix the routine.
0300 #define ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME ABSL_NO_THREAD_SAFETY_ANALYSIS
0301 
0302 // Similar to ABSL_NO_THREAD_SAFETY_ANALYSIS_FIXME, this macro marks a
0303 // ABSL_GUARDED_BY annotation that needs to be fixed, because it is producing
0304 // thread safety warning. It disables the ABSL_GUARDED_BY.
0305 #define ABSL_GUARDED_BY_FIXME(x)
0306 
0307 // Disables warnings for a single read operation.  This can be used to avoid
0308 // warnings when it is known that the read is not actually involved in a race,
0309 // but the compiler cannot confirm that.
0310 #define ABSL_TS_UNCHECKED_READ(x) absl::base_internal::ts_unchecked_read(x)
0311 
0312 namespace absl {
0313 ABSL_NAMESPACE_BEGIN
0314 namespace base_internal {
0315 
0316 // Takes a reference to a guarded data member, and returns an unguarded
0317 // reference.
0318 // Do not use this function directly, use ABSL_TS_UNCHECKED_READ instead.
0319 template <typename T>
0320 inline const T& ts_unchecked_read(const T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
0321   return v;
0322 }
0323 
0324 template <typename T>
0325 inline T& ts_unchecked_read(T& v) ABSL_NO_THREAD_SAFETY_ANALYSIS {
0326   return v;
0327 }
0328 
0329 }  // namespace base_internal
0330 ABSL_NAMESPACE_END
0331 }  // namespace absl
0332 
0333 #endif  // ABSL_BASE_THREAD_ANNOTATIONS_H_