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:
@@ -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