Skip to content

Instantly share code, notes, and snippets.

@EricWF
Created March 11, 2016 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricWF/12f77078e4efc6610572 to your computer and use it in GitHub Desktop.
Save EricWF/12f77078e4efc6610572 to your computer and use it in GitHub Desktop.
diff --git a/include/__config b/include/__config
index a62b2be..e57e63e 100644
--- a/include/__config
+++ b/include/__config
@@ -859,6 +859,11 @@ extern "C" void __sanitizer_annotate_contiguous_container(
#endif
#endif
+#if (!defined(_LIBCPP_HAS_NO_THREAD_ANNOTATIONS) && (!defined(__clang__) \
+ || !__has_attribute(acquire_capability)))
+#define _LIBCPP_HAS_NO_THREAD_ANNOTATIONS
+#endif
+
#endif // __cplusplus
#endif // _LIBCPP_CONFIG
diff --git a/include/__mutex_base b/include/__mutex_base
index b019b47..4d8e3fe 100644
--- a/include/__mutex_base
+++ b/include/__mutex_base
@@ -26,7 +26,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#ifndef _LIBCPP_HAS_NO_THREADS
-class _LIBCPP_TYPE_VIS mutex
+#ifndef _LIBCPP_THREAD_ANNOTATION
+# ifndef _LIBCPP_HAS_NO_THREAD_ANNOTATIONS
+# define _LIBCPP_THREAD_ANNOTATION(x) __attribute__((x))
+# else
+# define _LIBCPP_THREAD_ANNOTATION(x)
+# endif
+#endif // _LIBCPP_THREAD_ANNOTATION
+
+class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_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_ANNOTATION(acquire_capability());
+ bool try_lock() _NOEXCEPT _LIBCPP_THREAD_ANNOTATION(try_acquire_capability(true));
+ void unlock() _NOEXCEPT _LIBCPP_THREAD_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_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_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_ANNOTATION(requires_capability(__m))
: __m_(__m) {}
_LIBCPP_INLINE_VISIBILITY
- ~lock_guard() {__m_.unlock();}
+ ~lock_guard() _LIBCPP_THREAD_ANNOTATION(release_capability()) {__m_.unlock();}
private:
lock_guard(lock_guard const&);// = delete;
diff --git a/test/libcxx/test/format.py b/test/libcxx/test/format.py
index 865869a..c21118a 100644
--- a/test/libcxx/test/format.py
+++ b/test/libcxx/test/format.py
@@ -167,7 +167,7 @@ class LibcxxTestFormat(object):
# when using Clang.
extra_flags = []
if self.cxx.type != 'gcc':
- extra_flags += ['-fsyntax-only']
+ extra_flags += ['-fsyntax-only', '-Werror=thread-safety']
if use_verify:
extra_flags += ['-Xclang', '-verify',
'-Xclang', '-verify-ignore-unexpected=note']
diff --git a/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp b/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp
new file mode 100644
index 0000000..c9fb09d
--- /dev/null
+++ b/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp
@@ -0,0 +1,6 @@
+#include <mutex>
+
+int main() {
+ std::mutex m;
+ m.lock();
+} // expected-error {{mutex 'm' is still held at the end of function}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment