-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang][ThreadSafety] Handle mutex scope #157171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6af544
e9e92e5
a7e28b9
e4e193f
78c98c5
bd4eb50
0939f0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// RUN: %clang_cc1 -include %s -verify -fsyntax-only -Wthread-safety %s | ||
|
||
#ifndef HEADER | ||
#define HEADER | ||
|
||
#define LOCKABLE __attribute__ ((lockable)) | ||
#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) | ||
|
||
class LOCKABLE Mutex{}; | ||
|
||
template<typename T> | ||
struct lock_guard { | ||
lock_guard<T>(T) {} | ||
~lock_guard<T>() {} | ||
}; | ||
template<typename T> | ||
struct unique_lock { | ||
unique_lock<T>(T) {} | ||
~unique_lock<T>() {} | ||
}; | ||
|
||
template <class T, class... Ts> | ||
void LockMutexes(T &m, Ts &...ms) EXCLUSIVE_LOCK_FUNCTION(m, ms...); | ||
|
||
#else | ||
|
||
Mutex m0, m1; | ||
void non_local_mutex_held() { | ||
LockMutexes(m0, m1); // expected-note {{mutex acquired here}} \ | ||
// expected-note {{mutex acquired here}} | ||
} // expected-warning{{mutex 'm0' is still held at the end of function}} \ | ||
// expected-warning{{mutex 'm1' is still held at the end of function}} | ||
|
||
void no_local_mutex_held_warning() { | ||
Mutex local_m0; | ||
Mutex local_m1; | ||
LockMutexes(local_m0, local_m1); | ||
} // No warnings expected at end of function scope as the mutexes are function local. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, not sure I agree here. I think mutexes should be properly released even if they go out of scope. |
||
|
||
void no_local_unique_locks_held_warning() { | ||
unique_lock<Mutex> ul0(m0); | ||
unique_lock<Mutex> ul1(m1); | ||
LockMutexes(ul0, ul1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several issues here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we're still not able to emit attribute warnings on template instantiation. The reason is that we're doing Sema on |
||
} // No warnings expected at end of function scope as the unique_locks held are function local. | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scoped lockables should be unlocked by the automatic destructor. I don't think we should add this code.