diff options
| author | Eric Fiselier <eric@efcs.ca> | 2016-03-16 02:30:06 +0000 | 
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2016-03-16 02:30:06 +0000 | 
| commit | 7865b2e9439839cdb354cae848038def79826796 (patch) | |
| tree | ed23877c479ac4fa6fcaf8304bde3332e3d71802 /libcxx/include/__mutex_base | |
| parent | 9fb8e1b2a5d54f205a2a5db3136ab8cdc246333c (diff) | |
| download | bcm5719-llvm-7865b2e9439839cdb354cae848038def79826796.tar.gz bcm5719-llvm-7865b2e9439839cdb354cae848038def79826796.zip  | |
Add clang thread safety annotations to mutex and lock_guard. Patch by jamesr@google.com.
This adds clang thread safety annotations to std::mutex and
std::lock_guard so code using these types can use these types directly
instead of having to wrap the types to provide annotations. These checks
when enabled by -Wthread-safety provide simple but useful static
checking to detect potential race conditions.
See http://clang.llvm.org/docs/ThreadSafetyAnalysis.html for details.
This patch was reviewed in http://reviews.llvm.org/D14731.
llvm-svn: 263611
Diffstat (limited to 'libcxx/include/__mutex_base')
| -rw-r--r-- | libcxx/include/__mutex_base | 24 | 
1 files changed, 16 insertions, 8 deletions
diff --git a/libcxx/include/__mutex_base b/libcxx/include/__mutex_base index 6165023d296..71d0fa2e846 100644 --- a/libcxx/include/__mutex_base +++ b/libcxx/include/__mutex_base @@ -26,7 +26,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD  #ifndef _LIBCPP_HAS_NO_THREADS -class _LIBCPP_TYPE_VIS mutex +#ifndef _LIBCPP_THREAD_SAFETY_ANNOTATION +#  ifdef _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS +#    define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) __attribute__((x)) +#  else +#    define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) +#  endif +#endif  // _LIBCPP_THREAD_SAFETY_ANNOTATION + +class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex  {      pthread_mutex_t __m_; @@ -44,9 +52,9 @@ private:      mutex& operator=(const mutex&);// = delete;  public: -    void lock(); -    bool try_lock() _NOEXCEPT; -    void unlock() _NOEXCEPT; +    void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability()); +    bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true)); +    void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());      typedef pthread_mutex_t* native_handle_type;      _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} @@ -71,7 +79,7 @@ constexpr adopt_lock_t  adopt_lock  = adopt_lock_t();  #endif  template <class _Mutex> -class _LIBCPP_TYPE_VIS_ONLY lock_guard +class _LIBCPP_TYPE_VIS_ONLY _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) lock_guard  {  public:      typedef _Mutex mutex_type; @@ -81,13 +89,13 @@ private:  public:      _LIBCPP_INLINE_VISIBILITY -    explicit lock_guard(mutex_type& __m) +    explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))          : __m_(__m) {__m_.lock();}      _LIBCPP_INLINE_VISIBILITY -    lock_guard(mutex_type& __m, adopt_lock_t) +    lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))          : __m_(__m) {}      _LIBCPP_INLINE_VISIBILITY -    ~lock_guard() {__m_.unlock();} +    ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}  private:      lock_guard(lock_guard const&);// = delete;  | 

