Turn on some basic tsan annotations.

The most useful bit here is that tsan now knows the names of our threads.

Change-Id: I8eef8f31e954ffc373555b392d6d9678d76ead34
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 26d4d22..a4cdc34 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -59,6 +59,7 @@
 endif
 
 art_debug_cflags := \
+	-DDYNAMIC_ANNOTATIONS_ENABLED=1 \
 	-UNDEBUG
 
 ART_HOST_CFLAGS := $(art_cflags) -DANDROID_SMP=1
diff --git a/build/Android.executable.mk b/build/Android.executable.mk
index 2d9bd23..cc71aac 100644
--- a/build/Android.executable.mk
+++ b/build/Android.executable.mk
@@ -75,6 +75,11 @@
     LOCAL_SHARED_LIBRARIES += libart
   else # debug
     LOCAL_SHARED_LIBRARIES += libartd
+    ifeq ($$(art_target_or_host),target)
+      LOCAL_SHARED_LIBRARIES += libdynamic_annotations
+    else
+      LOCAL_SHARED_LIBRARIES += libdynamic_annotations-host
+    endif
   endif
 
   ifeq ($$(art_target_or_host),target)
@@ -111,4 +116,3 @@
   $(eval $(call build-art-executable,oatdump,$(OATDUMP_SRC_FILES),host,debug))
   $(eval $(call build-art-executable,oatexec,$(OATEXEC_SRC_FILES),host,debug))
 endif
-
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index 92fd96a..a2dc923 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -44,6 +44,11 @@
   LOCAL_SRC_FILES := $$(art_gtest_filename)
   LOCAL_C_INCLUDES += $(ART_C_INCLUDES)
   LOCAL_SHARED_LIBRARIES := libartd
+  ifeq ($$(art_target_or_host),target)
+    LOCAL_SHARED_LIBRARIES += libdynamic_annotations
+  else
+    LOCAL_SHARED_LIBRARIES += libdynamic_annotations-host
+  endif
 
   # Mac OS linker doesn't understand --export-dynamic.
   ifneq ($(HOST_OS)-$$(art_target_or_host),darwin-host)
diff --git a/build/Android.libart.mk b/build/Android.libart.mk
index bdd5514..872c6d3 100644
--- a/build/Android.libart.mk
+++ b/build/Android.libart.mk
@@ -70,10 +70,10 @@
   LOCAL_C_INCLUDES += $(ART_C_INCLUDES)
   LOCAL_SHARED_LIBRARIES := liblog libnativehelper
   ifeq ($$(art_target_or_host),target)
-    LOCAL_SHARED_LIBRARIES += libcutils libstlport libz libdl
+    LOCAL_SHARED_LIBRARIES += libcutils libstlport libz libdl libdynamic_annotations
   else # host
     LOCAL_STATIC_LIBRARIES += libcutils
-    LOCAL_SHARED_LIBRARIES += libz-host
+    LOCAL_SHARED_LIBRARIES += libz-host libdynamic_annotations-host
     LOCAL_LDLIBS := -ldl -lpthread
     ifeq ($(HOST_OS),linux)
       LOCAL_LDLIBS += -lrt
diff --git a/build/Android.libarttest.mk b/build/Android.libarttest.mk
index 7726df0..bb2600c 100644
--- a/build/Android.libarttest.mk
+++ b/build/Android.libarttest.mk
@@ -37,12 +37,13 @@
   LOCAL_C_INCLUDES += $(ART_C_INCLUDES)
   ifeq ($$(art_target_or_host),target)
     LOCAL_CFLAGS := $(ART_TARGET_CFLAGS) $(ART_TARGET_DEBUG_CFLAGS)
-    LOCAL_SHARED_LIBRARIES += libdl libstlport
+    LOCAL_SHARED_LIBRARIES += libdl libstlport libdynamic_annotations
     LOCAL_STATIC_LIBRARIES := libgtest
     LOCAL_MODULE_PATH := $(ART_TEST_OUT)
     include $(BUILD_SHARED_LIBRARY)
   else # host
     LOCAL_CFLAGS := $(ART_HOST_CFLAGS) $(ART_HOST_DEBUG_CFLAGS)
+    LOCAL_SHARED_LIBRARIES += libdynamic_annotations-host
     LOCAL_LDLIBS := -ldl -lpthread
     ifeq ($(HOST_OS),linux)
       LOCAL_LDLIBS += -lrt
diff --git a/src/thread.cc b/src/thread.cc
index a484f92..6be7f7d 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -290,6 +290,10 @@
   // a native peer!
   if (self->thin_lock_id_ != ThreadList::kMainId && !Runtime::Current()->IsCompiler()) {
     self->CreatePeer(name, as_daemon);
+  } else {
+    // These aren't necessary, but they improve diagnostics for unit tests & command-line tools.
+    self->name_->assign(name);
+    ::art::SetThreadName(name);
   }
 
   self->GetJniEnv()->locals.AssertEmpty();
@@ -643,7 +647,10 @@
      * on SMP and slightly more correct, but less convenient.
      */
     android_atomic_acquire_store(new_state, addr);
-    if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) {
+    ANNOTATE_IGNORE_READS_BEGIN();
+    int suspend_count = suspend_count_;
+    ANNOTATE_IGNORE_READS_END();
+    if (suspend_count != 0) {
       Runtime::Current()->GetThreadList()->FullSuspendCheck(this);
     }
   } else {
@@ -661,7 +668,10 @@
 }
 
 bool Thread::IsSuspended() {
-  return ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0 && GetState() != Thread::kRunnable;
+  ANNOTATE_IGNORE_READS_BEGIN();
+  int suspend_count = suspend_count_;
+  ANNOTATE_IGNORE_READS_END();
+  return suspend_count != 0 && GetState() != Thread::kRunnable;
 }
 
 static void ReportThreadSuspendTimeout(Thread* waiting_thread) {
diff --git a/src/utils.cc b/src/utils.cc
index cb76636..78f524f 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -16,6 +16,7 @@
 
 #include "utils.h"
 
+#include <dynamic_annotations.h>
 #include <pthread.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
@@ -651,6 +652,8 @@
 }
 
 void SetThreadName(const char* threadName) {
+  ANNOTATE_THREAD_NAME(threadName); // For tsan.
+
   int hasAt = 0;
   int hasDot = 0;
   const char* s = threadName;