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

Unified Diff: runtime/vm/os_thread_macos.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_linux.cc ('k') | runtime/vm/os_thread_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/os_thread_macos.cc
diff --git a/runtime/vm/os_thread_macos.cc b/runtime/vm/os_thread_macos.cc
index e7c7c90878908369333f7b1a7915f411cf01fdd7..851a4678ab6f783424eb877befcc7fcfae1384d5 100644
--- a/runtime/vm/os_thread_macos.cc
+++ b/runtime/vm/os_thread_macos.cc
@@ -223,8 +223,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 do track the owner.
owner_ = OSThread::kInvalidThreadId;
#endif // defined(DEBUG)
}
@@ -235,8 +235,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 do track the owner.
ASSERT(owner_ == OSThread::kInvalidThreadId);
#endif // defined(DEBUG)
}
@@ -247,8 +247,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 do track the owner.
owner_ = OSThread::GetCurrentThreadId();
#endif // defined(DEBUG)
}
@@ -261,8 +261,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 do track the owner.
owner_ = OSThread::GetCurrentThreadId();
#endif // defined(DEBUG)
return true;
@@ -270,8 +270,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 do track the owner.
ASSERT(IsOwnedByCurrentThread());
owner_ = OSThread::kInvalidThreadId;
#endif // defined(DEBUG)
@@ -300,10 +300,20 @@ Monitor::Monitor() {
result = pthread_cond_init(data_.cond(), NULL);
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);
@@ -315,12 +325,22 @@ 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);
}
@@ -332,7 +352,13 @@ Monitor::WaitResult Monitor::Wait(int64_t millis) {
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.
@@ -357,19 +383,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_linux.cc ('k') | runtime/vm/os_thread_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698