thread_annotations.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. // Author: Le-Chun Wu
  31. //
  32. // This header file contains the macro definitions for thread safety
  33. // annotations that allow the developers to document the locking policies
  34. // of their multi-threaded code. The annotations can also help program
  35. // analysis tools to identify potential thread safety issues.
  36. //
  37. // The annotations are implemented using GCC's "attributes" extension.
  38. // Using the macros defined here instead of the raw GCC attributes allows
  39. // for portability and future compatibility.
  40. //
  41. // This functionality is not yet fully implemented in perftools,
  42. // but may be one day.
  43. #ifndef BASE_THREAD_ANNOTATIONS_H_
  44. #define BASE_THREAD_ANNOTATIONS_H_
  45. #if defined(__GNUC__) \
  46. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) \
  47. && defined(__SUPPORT_TS_ANNOTATION__) && (!defined(SWIG))
  48. #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  49. #else
  50. #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
  51. #endif
  52. // Document if a shared variable/field needs to be protected by a lock.
  53. // GUARDED_BY allows the user to specify a particular lock that should be
  54. // held when accessing the annotated variable, while GUARDED_VAR only
  55. // indicates a shared variable should be guarded (by any lock). GUARDED_VAR
  56. // is primarily used when the client cannot express the name of the lock.
  57. #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  58. #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded)
  59. // Document if the memory location pointed to by a pointer should be guarded
  60. // by a lock when dereferencing the pointer. Similar to GUARDED_VAR,
  61. // PT_GUARDED_VAR is primarily used when the client cannot express the name
  62. // of the lock. Note that a pointer variable to a shared memory location
  63. // could itself be a shared variable. For example, if a shared global pointer
  64. // q, which is guarded by mu1, points to a shared memory location that is
  65. // guarded by mu2, q should be annotated as follows:
  66. // int *q GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
  67. #define PT_GUARDED_BY(x) \
  68. THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
  69. #define PT_GUARDED_VAR \
  70. THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded)
  71. // Document the acquisition order between locks that can be held
  72. // simultaneously by a thread. For any two locks that need to be annotated
  73. // to establish an acquisition order, only one of them needs the annotation.
  74. // (i.e. You don't have to annotate both locks with both ACQUIRED_AFTER
  75. // and ACQUIRED_BEFORE.)
  76. #define ACQUIRED_AFTER(x) \
  77. THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
  78. #define ACQUIRED_BEFORE(x) \
  79. THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
  80. // The following three annotations document the lock requirements for
  81. // functions/methods.
  82. // Document if a function expects certain locks to be held before it is called
  83. #define EXCLUSIVE_LOCKS_REQUIRED(x) \
  84. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(x))
  85. #define SHARED_LOCKS_REQUIRED(x) \
  86. THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(x))
  87. // Document the locks acquired in the body of the function. These locks
  88. // cannot be held when calling this function (as google3's Mutex locks are
  89. // non-reentrant).
  90. #define LOCKS_EXCLUDED(x) \
  91. THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
  92. // Document the lock the annotated function returns without acquiring it.
  93. #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  94. // Document if a class/type is a lockable type (such as the Mutex class).
  95. #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable)
  96. // Document if a class is a scoped lockable type (such as the MutexLock class).
  97. #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  98. // The following annotations specify lock and unlock primitives.
  99. #define EXCLUSIVE_LOCK_FUNCTION(x) \
  100. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(x))
  101. #define SHARED_LOCK_FUNCTION(x) \
  102. THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(x))
  103. #define EXCLUSIVE_TRYLOCK_FUNCTION(x) \
  104. THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(x))
  105. #define SHARED_TRYLOCK_FUNCTION(x) \
  106. THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(x))
  107. #define UNLOCK_FUNCTION(x) \
  108. THREAD_ANNOTATION_ATTRIBUTE__(unlock(x))
  109. // An escape hatch for thread safety analysis to ignore the annotated function.
  110. #define NO_THREAD_SAFETY_ANALYSIS \
  111. THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  112. #endif // BASE_THREAD_ANNOTATIONS_H_