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/test | |
| 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/test')
6 files changed, 133 insertions, 0 deletions
diff --git a/libcxx/test/libcxx/test/config.py b/libcxx/test/libcxx/test/config.py index 6c528807fff..2f04f0c98fe 100644 --- a/libcxx/test/libcxx/test/config.py +++ b/libcxx/test/libcxx/test/config.py @@ -98,6 +98,7 @@ class Configuration(object): self.configure_cxx_library_root() self.configure_use_system_cxx_lib() self.configure_use_clang_verify() + self.configure_use_thread_safety() self.configure_execute_external() self.configure_ccache() self.configure_compile_flags() @@ -218,6 +219,14 @@ class Configuration(object): self.lit_config.note( "inferred use_clang_verify as: %r" % self.use_clang_verify) + def configure_use_thread_safety(self): + '''If set, run clang with -verify on failing tests.''' + has_thread_safety = self.cxx.hasCompileFlag('-Werror=thread-safety') + if has_thread_safety: + self.cxx.compile_flags += ['-Werror=thread-safety'] + self.config.available_features.add('thread-safety') + self.lit_config.note("enabling thread-safety annotations") + def configure_execute_external(self): # Choose between lit's internal shell pipeline runner and a real shell. # If LIT_USE_INTERNAL_SHELL is in the environment, we use that as the diff --git a/libcxx/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp new file mode 100644 index 00000000000..a08aa6a95a4 --- /dev/null +++ b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <mutex> + +// This test does not define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS so it +// should compile without any warnings or errors even though this pattern is not +// understood by the thread safety annotations. + +#include <mutex> + +int main() { + std::mutex m; + m.lock(); + { + std::unique_lock<std::mutex> g(m, std::adopt_lock); + } +} diff --git a/libcxx/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp new file mode 100644 index 00000000000..edf2535ac23 --- /dev/null +++ b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// REQUIRES: thread-safety + +// <mutex> + +#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS + +#include <mutex> + +std::mutex m; +int foo __attribute__((guarded_by(m))); + +int main() { + std::lock_guard<std::mutex> lock(m); + foo++; +} diff --git a/libcxx/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp new file mode 100644 index 00000000000..e17d2bb4057 --- /dev/null +++ b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// REQUIRES: thread-safety + +// <mutex> + +#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS + +#include <mutex> + +std::mutex m; +int foo __attribute__((guarded_by(m))); + +int main() { + m.lock(); + foo++; + m.unlock(); +} diff --git a/libcxx/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp new file mode 100644 index 00000000000..c02a4c7cf6e --- /dev/null +++ b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// REQUIRES: thread-safety + +// <mutex> + +#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS + +#include <mutex> + +std::mutex m; + +int main() { + m.lock(); +} // expected-error {{mutex 'm' is still held at the end of function}} diff --git a/libcxx/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp new file mode 100644 index 00000000000..2daefca4ecd --- /dev/null +++ b/libcxx/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// REQUIRES: thread-safety + +// <mutex> + +#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS + +#include <mutex> + +std::mutex m; +int foo __attribute__((guarded_by(m))); + +void increment() __attribute__((requires_capability(m))) { + foo++; +} + +int main() { + m.lock(); + increment(); + m.unlock(); +} |

