Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1258)

Unified Diff: runtime/vm/os_thread_linux.cc

Issue 1426743002: Add lock owner information for class Monitor and add assertions for wait/notify/notifyall. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: code-review-comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/os_thread_android.cc ('k') | runtime/vm/os_thread_macos.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/os_thread_linux.cc
diff --git a/runtime/vm/os_thread_linux.cc b/runtime/vm/os_thread_linux.cc
index 5ccb98a162a2f95a90bfc96a9a5e317f901f56bd..f48b6b015ea59c36c2f334aed091bb0e3893a454 100644
--- a/runtime/vm/os_thread_linux.cc
+++ b/runtime/vm/os_thread_linux.cc
@@ -230,8 +230,8 @@ Mutex::Mutex() {
result = pthread_mutexattr_destroy(&attr);
VALIDATE_PTHREAD_RESULT(result);
- // When running with assertions enabled we do track the owner.
#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
owner_ = OSThread::kInvalidThreadId;
#endif // defined(DEBUG)
}
@@ -242,8 +242,8 @@ Mutex::~Mutex() {
// Verify that the pthread_mutex was destroyed.
VALIDATE_PTHREAD_RESULT(result);
- // When running with assertions enabled we do track the owner.
#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
ASSERT(owner_ == OSThread::kInvalidThreadId);
#endif // defined(DEBUG)
}
@@ -254,8 +254,8 @@ void Mutex::Lock() {
// Specifically check for dead lock to help debugging.
ASSERT(result != EDEADLK);
ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors.
- // When running with assertions enabled we do track the owner.
#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
owner_ = OSThread::GetCurrentThreadId();
#endif // defined(DEBUG)
}
@@ -268,8 +268,8 @@ bool Mutex::TryLock() {
return false;
}
ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors.
- // When running with assertions enabled we do track the owner.
#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
owner_ = OSThread::GetCurrentThreadId();
#endif // defined(DEBUG)
return true;
@@ -277,8 +277,8 @@ bool Mutex::TryLock() {
void Mutex::Unlock() {
- // When running with assertions enabled we do track the owner.
#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
ASSERT(IsOwnedByCurrentThread());
owner_ = OSThread::kInvalidThreadId;
#endif // defined(DEBUG)
@@ -317,10 +317,20 @@ Monitor::Monitor() {
result = pthread_condattr_destroy(&cond_attr);
VALIDATE_PTHREAD_RESULT(result);
+
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ owner_ = OSThread::kInvalidThreadId;
+#endif // defined(DEBUG)
}
Monitor::~Monitor() {
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(owner_ == OSThread::kInvalidThreadId);
+#endif // defined(DEBUG)
+
int result = pthread_mutex_destroy(data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
@@ -332,24 +342,41 @@ Monitor::~Monitor() {
void Monitor::Enter() {
int result = pthread_mutex_lock(data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
- // TODO(iposva): Do we need to track lock owners?
+
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(owner_ == OSThread::kInvalidThreadId);
+ owner_ = OSThread::GetCurrentThreadId();
+#endif // defined(DEBUG)
}
void Monitor::Exit() {
- // TODO(iposva): Do we need to track lock owners?
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(IsOwnedByCurrentThread());
+ owner_ = OSThread::kInvalidThreadId;
+#endif // defined(DEBUG)
+
int result = pthread_mutex_unlock(data_.mutex());
VALIDATE_PTHREAD_RESULT(result);
}
Monitor::WaitResult Monitor::Wait(int64_t millis) {
- return WaitMicros(millis * kMicrosecondsPerMillisecond);
+ Monitor::WaitResult retval = WaitMicros(millis * kMicrosecondsPerMillisecond);
+ return retval;
}
Monitor::WaitResult Monitor::WaitMicros(int64_t micros) {
- // TODO(iposva): Do we need to track lock owners?
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(IsOwnedByCurrentThread());
+ ThreadId saved_owner = owner_;
+ owner_ = OSThread::kInvalidThreadId;
+#endif // defined(DEBUG)
+
Monitor::WaitResult retval = kNotified;
if (micros == kNoTimeout) {
// Wait forever.
@@ -364,19 +391,28 @@ Monitor::WaitResult Monitor::WaitMicros(int64_t micros) {
retval = kTimedOut;
}
}
+
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(owner_ == OSThread::kInvalidThreadId);
+ owner_ = OSThread::GetCurrentThreadId();
+ ASSERT(owner_ == saved_owner);
+#endif // defined(DEBUG)
return retval;
}
void Monitor::Notify() {
- // TODO(iposva): Do we need to track lock owners?
+ // When running with assertions enabled we track the owner.
+ ASSERT(IsOwnedByCurrentThread());
int result = pthread_cond_signal(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}
void Monitor::NotifyAll() {
- // TODO(iposva): Do we need to track lock owners?
+ // When running with assertions enabled we track the owner.
+ ASSERT(IsOwnedByCurrentThread());
int result = pthread_cond_broadcast(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}
« no previous file with comments | « runtime/vm/os_thread_android.cc ('k') | runtime/vm/os_thread_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698