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. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@263611 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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++;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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}}
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user