Global lock levels.

Introduce the notion of the mutators/GC being a shared-exclusive (aka
reader-writer) lock. Introduce globally ordered locks, analysable by
annotalysis, statically at compile time. Add locking attributes to
methods.

More subtly, remove the heap_lock_ and split between various locks that
are held for smaller periods (where work doesn't get blocked). Remove
buggy Dalvik style thread transitions. Make GC use CMS in all cases when
concurrent is enabled. Fix bug where suspend counts rather than debug
suspend counts were sent to JDWP. Move the PathClassLoader to
WellKnownClasses. In debugger refactor calls to send request and
possibly suspend. Break apart different VmWait thread states. Move
identity hash code to a shared method.

Change-Id: Icdbfc3ce3fcccd14341860ac7305d8e97b51f5c6
diff --git a/src/object.h b/src/object.h
index 0fc6acc..03ed132 100644
--- a/src/object.h
+++ b/src/object.h
@@ -209,35 +209,48 @@
 
   void SetClass(Class* new_klass);
 
-  bool InstanceOf(const Class* klass) const;
+  bool InstanceOf(const Class* klass) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  size_t SizeOf() const;
+  size_t SizeOf() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Object* Clone();
+  Object* Clone() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+
+  int32_t IdentityHashCode() const {
+  #ifdef MOVING_GARBAGE_COLLECTOR
+    // TODO: we'll need to use the Object's internal concept of identity
+      UNIMPLEMENTED(FATAL);
+  #endif
+    return reinterpret_cast<int32_t>(this);
+  }
 
   static MemberOffset MonitorOffset() {
     return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
   }
 
   volatile int32_t* GetRawLockWordAddress() {
-    byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
+    byte* raw_addr = reinterpret_cast<byte*>(this) +
+        OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
     int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
     return const_cast<volatile int32_t*>(word_addr);
   }
 
   uint32_t GetThinLockId();
 
-  void MonitorEnter(Thread* thread);
+  void MonitorEnter(Thread* thread) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
+      EXCLUSIVE_LOCK_FUNCTION(monitor_lock_);
 
-  bool MonitorExit(Thread* thread);
+  bool MonitorExit(Thread* thread) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
+      UNLOCK_FUNCTION(monitor_lock_);
 
-  void Notify();
+  void Notify() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void NotifyAll();
+  void NotifyAll() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void Wait(int64_t timeout);
+  void Wait(int64_t timeout) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void Wait(int64_t timeout, int32_t nanos);
+  void Wait(int64_t timeout, int32_t nanos)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   bool IsClass() const;
 
@@ -285,14 +298,14 @@
     return down_cast<const Method*>(this);
   }
 
-  bool IsField() const;
+  bool IsField() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Field* AsField() {
+  Field* AsField() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(IsField());
     return down_cast<Field*>(this);
   }
 
-  const Field* AsField() const {
+  const Field* AsField() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(IsField());
     return down_cast<const Field*>(this);
   }
@@ -403,16 +416,6 @@
   DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
 };
 
-struct ObjectIdentityHash {
-  size_t operator()(const Object* const& obj) const {
-#ifdef MOVING_GARBAGE_COLLECTOR
-  // TODO: we'll need to use the Object's internal concept of identity
-    UNIMPLEMENTED(FATAL);
-#endif
-    return reinterpret_cast<size_t>(obj);
-  }
-};
-
 // C++ mirror of java.lang.reflect.Field
 class MANAGED Field : public Object {
  public:
@@ -458,32 +461,56 @@
   void SetOffset(MemberOffset num_bytes);
 
   // field access, null object for static fields
-  bool GetBoolean(const Object* object) const;
-  void SetBoolean(Object* object, bool z) const;
-  int8_t GetByte(const Object* object) const;
-  void SetByte(Object* object, int8_t b) const;
-  uint16_t GetChar(const Object* object) const;
-  void SetChar(Object* object, uint16_t c) const;
-  int16_t GetShort(const Object* object) const;
-  void SetShort(Object* object, int16_t s) const;
-  int32_t GetInt(const Object* object) const;
-  void SetInt(Object* object, int32_t i) const;
-  int64_t GetLong(const Object* object) const;
-  void SetLong(Object* object, int64_t j) const;
-  float GetFloat(const Object* object) const;
-  void SetFloat(Object* object, float f) const;
-  double GetDouble(const Object* object) const;
-  void SetDouble(Object* object, double d) const;
-  Object* GetObject(const Object* object) const;
-  void SetObject(Object* object, const Object* l) const;
+  bool GetBoolean(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetBoolean(Object* object, bool z) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  int8_t GetByte(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetByte(Object* object, int8_t b) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  uint16_t GetChar(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetChar(Object* object, uint16_t c) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  int16_t GetShort(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetShort(Object* object, int16_t s) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  int32_t GetInt(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetInt(Object* object, int32_t i) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  int64_t GetLong(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetLong(Object* object, int64_t j) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  float GetFloat(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetFloat(Object* object, float f) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  double GetDouble(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetDouble(Object* object, double d) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  Object* GetObject(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetObject(Object* object, const Object* l) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // raw field accesses
-  uint32_t Get32(const Object* object) const;
-  void Set32(Object* object, uint32_t new_value) const;
-  uint64_t Get64(const Object* object) const;
-  void Set64(Object* object, uint64_t new_value) const;
-  Object* GetObj(const Object* object) const;
-  void SetObj(Object* object, const Object* new_value) const;
+  uint32_t Get32(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void Set32(Object* object, uint32_t new_value) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  uint64_t Get64(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void Set64(Object* object, uint64_t new_value) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  Object* GetObj(const Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void SetObj(Object* object, const Object* new_value) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static Class* GetJavaLangReflectField() {
     DCHECK(java_lang_reflect_Field_ != NULL);
@@ -658,9 +685,10 @@
   void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
 
   // Find the method that this method overrides
-  Method* FindOverriddenMethod() const;
+  Method* FindOverriddenMethod() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const;
+  void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   const void* GetCode() const {
     return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
@@ -670,7 +698,7 @@
     SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
   }
 
-  uint32_t GetCodeSize() const {
+  uint32_t GetCodeSize() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
     uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
     if (code == 0) {
@@ -681,7 +709,8 @@
     return reinterpret_cast<uint32_t*>(code)[-1];
   }
 
-  bool IsWithinCode(uintptr_t pc) const {
+  bool IsWithinCode(uintptr_t pc) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
     if (code == 0) {
       return pc == 0;
@@ -689,7 +718,8 @@
     return (code <= pc && pc < code + GetCodeSize());
   }
 
-  void AssertPcIsWithinCode(uintptr_t pc) const;
+  void AssertPcIsWithinCode(uintptr_t pc) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   uint32_t GetOatCodeOffset() const {
     DCHECK(!Runtime::Current()->IsStarted());
@@ -813,9 +843,10 @@
 
   bool IsRegistered() const;
 
-  void RegisterNative(Thread* self, const void* native_method);
+  void RegisterNative(Thread* self, const void* native_method)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void UnregisterNative(Thread* self);
+  void UnregisterNative(Thread* self) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static MemberOffset NativeMethodOffset() {
     return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
@@ -915,14 +946,17 @@
 
   // Converts a native PC to a dex PC.  TODO: this is a no-op
   // until we associate a PC mapping table with each method.
-  uint32_t ToDexPC(const uintptr_t pc) const;
+  uint32_t ToDexPC(const uintptr_t pc) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Converts a dex PC to a native PC.  TODO: this is a no-op
   // until we associate a PC mapping table with each method.
-  uintptr_t ToNativePC(const uint32_t dex_pc) const;
+  uintptr_t ToNativePC(const uint32_t dex_pc) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Find the catch block for the given exception type and dex_pc
-  uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
+  uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
 
@@ -1012,9 +1046,11 @@
  public:
   // A convenience for code that doesn't know the component size,
   // and doesn't want to have to work it out itself.
-  static Array* Alloc(Class* array_class, int32_t component_count);
+  static Array* Alloc(Class* array_class, int32_t component_count)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
+  static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   size_t SizeOf() const;
 
@@ -1051,7 +1087,8 @@
   }
 
  protected:
-  bool IsValidIndex(int32_t index) const {
+  bool IsValidIndex(int32_t index) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     if (UNLIKELY(index < 0 || index >= length_)) {
       return ThrowArrayIndexOutOfBoundsException(index);
     }
@@ -1059,8 +1096,10 @@
   }
 
  protected:
-  bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
-  bool ThrowArrayStoreException(Object* object) const;
+  bool ThrowArrayIndexOutOfBoundsException(int32_t index) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool ThrowArrayStoreException(Object* object) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
  private:
   // The number of array elements.
@@ -1074,23 +1113,27 @@
 template<class T>
 class MANAGED ObjectArray : public Array {
  public:
-  static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
+  static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  T* Get(int32_t i) const;
+  T* Get(int32_t i) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void Set(int32_t i, T* object);
+  void Set(int32_t i, T* object) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Set element without bound and element type checks, to be used in limited
   // circumstances, such as during boot image writing
-  void SetWithoutChecks(int32_t i, T* object);
+  void SetWithoutChecks(int32_t i, T* object)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  T* GetWithoutChecks(int32_t i) const;
+  T* GetWithoutChecks(int32_t i) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static void Copy(const ObjectArray<T>* src, int src_pos,
                    ObjectArray<T>* dst, int dst_pos,
-                   size_t length);
+                   size_t length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  ObjectArray<T>* CopyOf(int32_t new_length);
+  ObjectArray<T>* CopyOf(int32_t new_length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
  private:
   DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
@@ -1181,7 +1224,7 @@
     return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
   }
 
-  void SetStatus(Status new_status);
+  void SetStatus(Status new_status) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Returns true if the class has failed to link.
   bool IsErroneous() const {
@@ -1291,7 +1334,8 @@
 
   String* GetName() const; // Returns the cached name
   void SetName(String* name);  // Sets the cached name
-  String* ComputeName();  // Computes the name, then sets the cached value
+  String* ComputeName()  // Computes the name, then sets the cached value
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   bool IsProxyClass() const {
     // Read access flags without using getter as whether something is a proxy can be check in
@@ -1370,7 +1414,7 @@
 
   bool IsStringClass() const;
 
-  bool IsThrowableClass() const;
+  bool IsThrowableClass() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   Class* GetComponentType() const {
     return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
@@ -1394,7 +1438,7 @@
   }
 
   // Creates a raw object instance but does not invoke the default constructor.
-  Object* AllocObject();
+  Object* AllocObject() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   bool IsVariableSize() const {
     // Classes and arrays vary in size, and so the object_size_ field cannot
@@ -1412,9 +1456,10 @@
     return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
   }
 
-  void SetClassSize(size_t new_class_size);
+  void SetClassSize(size_t new_class_size)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  size_t GetObjectSize() const {
+  size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
     DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
     size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
@@ -1429,19 +1474,21 @@
   }
 
   // Returns true if this class is in the same packages as that class.
-  bool IsInSamePackage(const Class* that) const;
+  bool IsInSamePackage(const Class* that) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
 
   // Returns true if this class can access that class.
-  bool CanAccess(Class* that) const {
+  bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return that->IsPublic() || this->IsInSamePackage(that);
   }
 
   // Can this class access a member in the provided class with the provided member access flags?
   // Note that access to the class isn't checked in case the declaring class is protected and the
   // method has been exposed by a public sub-class
-  bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
+  bool CanAccessMember(Class* access_to, uint32_t member_flags) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     // Classes can access all of their own members
     if (this == access_to) {
       return true;
@@ -1464,14 +1511,16 @@
     return this->IsInSamePackage(access_to);
   }
 
-  bool IsSubClass(const Class* klass) const;
+  bool IsSubClass(const Class* klass) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Can src be assigned to this class? For example, String can be assigned to Object (by an
   // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
   // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
   // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
   // to themselves. Classes for primitive types may not assign to each other.
-  bool IsAssignableFrom(const Class* src) const {
+  bool IsAssignableFrom(const Class* src) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(src != NULL);
     if (this == src) {
       // Can always assign to things of the same type
@@ -1526,7 +1575,8 @@
     kDumpClassInitialized = (1 << 2),
   };
 
-  void DumpClass(std::ostream& os, int flags) const;
+  void DumpClass(std::ostream& os, int flags) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   DexCache* GetDexCache() const;
 
@@ -1546,11 +1596,13 @@
                    new_direct_methods, false);
   }
 
-  Method* GetDirectMethod(int32_t i) const {
+  Method* GetDirectMethod(int32_t i) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return GetDirectMethods()->Get(i);
   }
 
-  void SetDirectMethod(uint32_t i, Method* f) {  // TODO: uint16_t
+  void SetDirectMethod(uint32_t i, Method* f)  // TODO: uint16_t
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_){
     ObjectArray<Method>* direct_methods =
         GetFieldObject<ObjectArray<Method>*>(
             OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
@@ -1581,17 +1633,20 @@
     return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
   }
 
-  Method* GetVirtualMethod(uint32_t i) const {
+  Method* GetVirtualMethod(uint32_t i) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(IsResolved() || IsErroneous());
     return GetVirtualMethods()->Get(i);
   }
 
-  Method* GetVirtualMethodDuringLinking(uint32_t i) const {
+  Method* GetVirtualMethodDuringLinking(uint32_t i) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(IsLoaded() || IsErroneous());
     return GetVirtualMethods()->Get(i);
   }
 
-  void SetVirtualMethod(uint32_t i, Method* f) {  // TODO: uint16_t
+  void SetVirtualMethod(uint32_t i, Method* f)  // TODO: uint16_t
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     ObjectArray<Method>* virtual_methods =
         GetFieldObject<ObjectArray<Method>*>(
             OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
@@ -1619,7 +1674,8 @@
   // Given a method implemented by this class but potentially from a
   // super class, return the specific implementation
   // method for this class.
-  Method* FindVirtualMethodForVirtual(Method* method) {
+  Method* FindVirtualMethodForVirtual(Method* method)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(!method->GetDeclaringClass()->IsInterface());
     // The argument method may from a super class.
     // Use the index to a potentially overridden one for this instance's class.
@@ -1629,13 +1685,17 @@
   // Given a method implemented by this class, but potentially from a
   // super class or interface, return the specific implementation
   // method for this class.
-  Method* FindVirtualMethodForInterface(Method* method);
+  Method* FindVirtualMethodForInterface(Method* method)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
+  Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
+  Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
+  Method* FindVirtualMethodForVirtualOrInterface(Method* method)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     if (method->IsDirect()) {
       return method;
     }
@@ -1645,21 +1705,29 @@
     return FindVirtualMethodForVirtual(method);
   }
 
-  Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
+  Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
+  Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
+  Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
+  Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
+  Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
+  Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
+  Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
+  Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   int32_t GetIfTableCount() const {
     ObjectArray<InterfaceEntry>* iftable = GetIfTable();
@@ -1695,12 +1763,14 @@
     return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
   }
 
-  Field* GetInstanceField(uint32_t i) const {  // TODO: uint16_t
+  Field* GetInstanceField(uint32_t i) const  // TODO: uint16_t
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_){
     DCHECK_NE(NumInstanceFields(), 0U);
     return GetIFields()->Get(i);
   }
 
-  void SetInstanceField(uint32_t i, Field* f) {  // TODO: uint16_t
+  void SetInstanceField(uint32_t i, Field* f)  // TODO: uint16_t
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_){
     ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
         OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
     ifields->Set(i, f);
@@ -1770,11 +1840,13 @@
     return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
   }
 
-  Field* GetStaticField(uint32_t i) const {  // TODO: uint16_t
+  Field* GetStaticField(uint32_t i) const  // TODO: uint16_t
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return GetSFields()->Get(i);
   }
 
-  void SetStaticField(uint32_t i, Field* f) {  // TODO: uint16_t
+  void SetStaticField(uint32_t i, Field* f)  // TODO: uint16_t
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
         OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
     sfields->Set(i, f);
@@ -1787,29 +1859,38 @@
   void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
 
   // Find a static or instance field using the JLS resolution order
-  Field* FindField(const StringPiece& name, const StringPiece& type);
+  Field* FindField(const StringPiece& name, const StringPiece& type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds the given instance field in this class or a superclass.
-  Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
+  Field* FindInstanceField(const StringPiece& name, const StringPiece& type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds the given instance field in this class or a superclass, only searches classes that
   // have the same dex cache.
-  Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
+  Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
+  Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
+  Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds the given static field in this class or a superclass.
-  Field* FindStaticField(const StringPiece& name, const StringPiece& type);
+  Field* FindStaticField(const StringPiece& name, const StringPiece& type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds the given static field in this class or superclass, only searches classes that
   // have the same dex cache.
-  Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
+  Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
+  Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
+  Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   pid_t GetClinitThreadId() const {
     DCHECK(IsIdxLoaded() || IsErroneous());
@@ -1834,14 +1915,18 @@
   }
 
  private:
-  void SetVerifyErrorClass(Class* klass) {
+  void SetVerifyErrorClass(Class* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     CHECK(klass != NULL) << PrettyClass(this);
     SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
   }
 
-  bool Implements(const Class* klass) const;
-  bool IsArrayAssignableFromArray(const Class* klass) const;
-  bool IsAssignableFromArray(const Class* klass) const;
+  bool Implements(const Class* klass) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool IsArrayAssignableFromArray(const Class* klass) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool IsAssignableFromArray(const Class* klass) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // defining class loader, or NULL for the "bootstrap" system loader
   ClassLoader* class_loader_;
@@ -1995,8 +2080,7 @@
 
 inline bool Object::IsField() const {
   Class* java_lang_Class = klass_->klass_;
-  Class* java_lang_reflect_Field =
-      java_lang_Class->GetInstanceField(0)->GetClass();
+  Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
   return GetClass() == java_lang_reflect_Field;
 }
 
@@ -2178,7 +2262,8 @@
  public:
   typedef T ElementType;
 
-  static PrimitiveArray<T>* Alloc(size_t length);
+  static PrimitiveArray<T>* Alloc(size_t length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   const T* GetData() const {
     intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
@@ -2190,14 +2275,14 @@
     return reinterpret_cast<T*>(data);
   }
 
-  T Get(int32_t i) const {
+  T Get(int32_t i) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     if (!IsValidIndex(i)) {
       return T(0);
     }
     return GetData()[i];
   }
 
-  void Set(int32_t i, T value) {
+  void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     // TODO: ArrayStoreException
     if (IsValidIndex(i)) {
       GetData()[i] = value;
@@ -2251,9 +2336,9 @@
 
   int32_t GetLength() const;
 
-  int32_t GetHashCode();
+  int32_t GetHashCode() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void ComputeHashCode() {
+  void ComputeHashCode() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
   }
 
@@ -2261,36 +2346,44 @@
     return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
   }
 
-  uint16_t CharAt(int32_t index) const;
+  uint16_t CharAt(int32_t index) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  String* Intern();
+  String* Intern() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static String* AllocFromUtf16(int32_t utf16_length,
                                 const uint16_t* utf16_data_in,
-                                int32_t hash_code = 0);
+                                int32_t hash_code = 0)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  static String* AllocFromModifiedUtf8(const char* utf);
+  static String* AllocFromModifiedUtf8(const char* utf)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static String* AllocFromModifiedUtf8(int32_t utf16_length,
-                                       const char* utf8_data_in);
+                                       const char* utf8_data_in)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  static String* Alloc(Class* java_lang_String, int32_t utf16_length);
+  static String* Alloc(Class* java_lang_String, int32_t utf16_length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  static String* Alloc(Class* java_lang_String, CharArray* array);
+  static String* Alloc(Class* java_lang_String, CharArray* array)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool Equals(const char* modified_utf8) const;
+  bool Equals(const char* modified_utf8) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // TODO: do we need this overload? give it a more intention-revealing name.
-  bool Equals(const StringPiece& modified_utf8) const;
+  bool Equals(const StringPiece& modified_utf8) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool Equals(const String* that) const;
+  bool Equals(const String* that) const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Compare UTF-16 code point values not in a locale-sensitive manner
   int Compare(int32_t utf16_length, const char* utf8_data_in);
 
   // TODO: do we need this overload? give it a more intention-revealing name.
   bool Equals(const uint16_t* that_chars, int32_t that_offset,
-              int32_t that_length) const;
+              int32_t that_length) const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Create a modified UTF-8 encoded std::string from a java/lang/String object.
   std::string ToModifiedUtf8() const;
@@ -2343,6 +2436,7 @@
   DISALLOW_IMPLICIT_CONSTRUCTORS(String);
 };
 
+// TODO: remove? only used in a unit test of itself.
 struct StringHashCode {
   int32_t operator()(String* string) const {
     return string->GetHashCode();
@@ -2425,13 +2519,13 @@
   String* GetDetailMessage() const {
     return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
   }
-  std::string Dump() const;
+  std::string Dump() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // This is a runtime version of initCause, you shouldn't use it if initCause may have been
   // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
   // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
   void SetCause(Throwable* cause);
-  bool IsCheckedException() const;
+  bool IsCheckedException() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static Class* GetJavaLangThrowable() {
     DCHECK(java_lang_Throwable_ != NULL);
@@ -2485,7 +2579,8 @@
   static StackTraceElement* Alloc(String* declaring_class,
                                   String* method_name,
                                   String* file_name,
-                                  int32_t line_number);
+                                  int32_t line_number)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   static void SetClass(Class* java_lang_StackTraceElement);
 
@@ -2511,20 +2606,20 @@
 
 class MANAGED InterfaceEntry : public ObjectArray<Object> {
  public:
-  Class* GetInterface() const {
+  Class* GetInterface() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     Class* interface = Get(kInterface)->AsClass();
     DCHECK(interface != NULL);
     return interface;
   }
 
-  void SetInterface(Class* interface) {
+  void SetInterface(Class* interface) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(interface != NULL);
     DCHECK(interface->IsInterface());
     DCHECK(Get(kInterface) == NULL);
     Set(kInterface, interface);
   }
 
-  size_t GetMethodArrayCount() const {
+  size_t GetMethodArrayCount() const SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
     if (method_array == NULL) {
       return 0;
@@ -2532,13 +2627,15 @@
     return method_array->GetLength();
   }
 
-  ObjectArray<Method>* GetMethodArray() const {
+  ObjectArray<Method>* GetMethodArray() const
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
     DCHECK(method_array != NULL);
     return method_array;
   }
 
-  void SetMethodArray(ObjectArray<Method>* new_ma) {
+  void SetMethodArray(ObjectArray<Method>* new_ma)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(new_ma != NULL);
     DCHECK(Get(kMethodArray) == NULL);
     Set(kMethodArray, new_ma);