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

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: self-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
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..31c2c0d9c3fab9f223a2d7713e396dda67bb54a4 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,12 @@ 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());
+ owner_ = OSThread::kInvalidThreadId;
+#endif // defined(DEBUG)
+
Monitor::WaitResult retval = kNotified;
if (micros == kNoTimeout) {
// Wait forever.
@@ -357,19 +382,31 @@ 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();
srdjan 2015/10/26 23:02:11 ditto
siva 2015/10/27 00:14:57 Done.
+#endif // defined(DEBUG)
return retval;
}
void Monitor::Notify() {
- // TODO(iposva): Do we need to track lock owners?
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(IsOwnedByCurrentThread());
+#endif // defined(DEBUG)
int result = pthread_cond_signal(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}
void Monitor::NotifyAll() {
- // TODO(iposva): Do we need to track lock owners?
+#if defined(DEBUG)
+ // When running with assertions enabled we track the owner.
+ ASSERT(IsOwnedByCurrentThread());
+#endif // defined(DEBUG)
int result = pthread_cond_broadcast(data_.cond());
VALIDATE_PTHREAD_RESULT(result);
}

Powered by Google App Engine
This is Rietveld 408576698