Make pthread_mutex_t destruction safer during shutdown.
If we have suspended daemon threads, they might be holding some of the mutexes
we need to destroy, so we can't assert that we successfully destroyed them. We
still want to assert success where possible, though.
Bug: 5869254
Change-Id: Id4f3af3d40b10045958e7d692b3c9eebafec566d
diff --git a/src/mutex.cc b/src/mutex.cc
index 0dfab11..7507566 100644
--- a/src/mutex.cc
+++ b/src/mutex.cc
@@ -21,6 +21,7 @@
#include "heap.h" // for VERIFY_OBJECT_ENABLED
#include "logging.h"
#include "utils.h"
+#include "runtime.h"
#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
@@ -36,7 +37,12 @@
}
Mutex::~Mutex() {
- CHECK_MUTEX_CALL(pthread_mutex_destroy, (&mutex_));
+ int rc = pthread_mutex_destroy(&mutex_);
+ if (rc != 0) {
+ errno = rc;
+ bool shutting_down = Runtime::Current()->IsShuttingDown();
+ PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
+ }
}
void Mutex::Lock() {
diff --git a/src/runtime.cc b/src/runtime.cc
index e9d5677..c203ffd 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -43,6 +43,7 @@
java_vm_(NULL),
jni_stub_array_(NULL),
abstract_method_error_stub_array_(NULL),
+ shutting_down_(false),
started_(false),
vfprintf_(NULL),
exit_(NULL),
@@ -58,6 +59,8 @@
}
Runtime::~Runtime() {
+ shutting_down_ = true;
+
Dbg::StopJdwp();
// Make sure our internal threads are dead before we start tearing down things they're using.
@@ -525,6 +528,10 @@
VLOG(startup) << "Runtime::StartDaemonThreads exiting";
}
+bool Runtime::IsShuttingDown() const {
+ return shutting_down_;
+}
+
bool Runtime::IsStarted() const {
return started_;
}
diff --git a/src/runtime.h b/src/runtime.h
index d30f465..913779a 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -85,6 +85,7 @@
// Starts a runtime, which may cause threads to be started and code to run.
void Start();
+ bool IsShuttingDown() const;
bool IsStarted() const;
static Runtime* Current() {
@@ -268,6 +269,7 @@
// As returned by ClassLoader.getSystemClassLoader()
ClassLoader* system_class_loader_;
+ bool shutting_down_;
bool started_;
// Hooks supported by JNI_CreateJavaVM