thread_annotations.h 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license
  5. // that can be found in the LICENSE file in the root of the source
  6. // tree. An additional intellectual property rights grant can be found
  7. // in the file PATENTS. All contributing project authors may
  8. // be found in the AUTHORS file in the root of the source tree.
  9. //
  10. // Borrowed from
  11. // https://code.google.com/p/gperftools/source/browse/src/base/thread_annotations.h
  12. // but adapted for clang attributes instead of the gcc.
  13. //
  14. // This header file contains the macro definitions for thread safety
  15. // annotations that allow the developers to document the locking policies
  16. // of their multi-threaded code. The annotations can also help program
  17. // analysis tools to identify potential thread safety issues.
  18. #ifndef RTC_BASE_THREAD_ANNOTATIONS_H_
  19. #define RTC_BASE_THREAD_ANNOTATIONS_H_
  20. #if defined(__clang__) && (!defined(SWIG))
  21. #define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  22. #else
  23. #define RTC_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
  24. #endif
  25. // Document if a shared variable/field needs to be protected by a lock.
  26. // GUARDED_BY allows the user to specify a particular lock that should be
  27. // held when accessing the annotated variable.
  28. #define RTC_GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  29. // Document if the memory location pointed to by a pointer should be guarded
  30. // by a lock when dereferencing the pointer. Note that a pointer variable to a
  31. // shared memory location could itself be a shared variable. For example, if a
  32. // shared global pointer q, which is guarded by mu1, points to a shared memory
  33. // location that is guarded by mu2, q should be annotated as follows:
  34. // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
  35. #define RTC_PT_GUARDED_BY(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  36. // Document the acquisition order between locks that can be held
  37. // simultaneously by a thread. For any two locks that need to be annotated
  38. // to establish an acquisition order, only one of them needs the annotation.
  39. // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
  40. // and ACQUIRED_BEFORE.)
  41. #define RTC_ACQUIRED_AFTER(x) \
  42. RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
  43. #define RTC_ACQUIRED_BEFORE(x) \
  44. RTC_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
  45. // The following three annotations document the lock requirements for
  46. // functions/methods.
  47. // Document if a function expects certain locks to be held before it is called
  48. #define RTC_EXCLUSIVE_LOCKS_REQUIRED(...) \
  49. RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__))
  50. #define RTC_SHARED_LOCKS_REQUIRED(...) \
  51. RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__))
  52. // Document the locks acquired in the body of the function. These locks
  53. // cannot be held when calling this function (as google3's Mutex locks are
  54. // non-reentrant).
  55. #define RTC_LOCKS_EXCLUDED(...) \
  56. RTC_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  57. // Document the lock the annotated function returns without acquiring it.
  58. #define RTC_LOCK_RETURNED(x) RTC_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  59. // Document if a class/type is a lockable type (such as the Mutex class).
  60. #define RTC_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(lockable)
  61. // Document if a class is a scoped lockable type (such as the MutexLock class).
  62. #define RTC_SCOPED_LOCKABLE RTC_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  63. // The following annotations specify lock and unlock primitives.
  64. #define RTC_EXCLUSIVE_LOCK_FUNCTION(...) \
  65. RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__))
  66. #define RTC_SHARED_LOCK_FUNCTION(...) \
  67. RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__))
  68. #define RTC_EXCLUSIVE_TRYLOCK_FUNCTION(...) \
  69. RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__))
  70. #define RTC_SHARED_TRYLOCK_FUNCTION(...) \
  71. RTC_THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__))
  72. #define RTC_UNLOCK_FUNCTION(...) \
  73. RTC_THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__))
  74. // An escape hatch for thread safety analysis to ignore the annotated function.
  75. #define RTC_NO_THREAD_SAFETY_ANALYSIS \
  76. RTC_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  77. #endif // RTC_BASE_THREAD_ANNOTATIONS_H_