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/class_linker.h b/src/class_linker.h
index 8ad8d2d..c69c442 100644
--- a/src/class_linker.h
+++ b/src/class_linker.h
@@ -45,46 +45,61 @@
  public:
   // Creates the class linker by boot strapping from dex files.
   static ClassLinker* CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
-                                         InternTable* intern_table);
+                                         InternTable* intern_table)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Creates the class linker from an image.
-  static ClassLinker* CreateFromImage(InternTable* intern_table);
+  static ClassLinker* CreateFromImage(InternTable* intern_table)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   ~ClassLinker();
 
   // Finds a class by its descriptor, loading it if necessary.
   // If class_loader is null, searches boot_class_path_.
-  Class* FindClass(const char* descriptor, ClassLoader* class_loader);
+  Class* FindClass(const char* descriptor, ClassLoader* class_loader)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Class* FindSystemClass(const char* descriptor);
+  Class* FindSystemClass(const char* descriptor)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Define a new a class based on a ClassDef from a DexFile
   Class* DefineClass(const StringPiece& descriptor, ClassLoader* class_loader,
-                     const DexFile& dex_file, const DexFile::ClassDef& dex_class_def);
+                     const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
   // by the given 'class_loader'.
-  Class* LookupClass(const char* descriptor, const ClassLoader* class_loader);
+  Class* LookupClass(const char* descriptor, const ClassLoader* class_loader)
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds all the classes with the given descriptor, regardless of ClassLoader.
-  void LookupClasses(const char* descriptor, std::vector<Class*>& classes);
+  void LookupClasses(const char* descriptor, std::vector<Class*>& classes)
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Class* FindPrimitiveClass(char type);
+  Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // General class unloading is not supported, this is used to prune
   // unwanted classes during image writing.
-  bool RemoveClass(const char* descriptor, const ClassLoader* class_loader);
+  bool RemoveClass(const char* descriptor, const ClassLoader* class_loader)
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void DumpAllClasses(int flags) const;
+  void DumpAllClasses(int flags) const
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void DumpForSigQuit(std::ostream& os) const;
+  void DumpForSigQuit(std::ostream& os) const
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_);
 
-  size_t NumLoadedClasses() const;
+  size_t NumLoadedClasses() const LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_);
 
   // Resolve a String with the given index from the DexFile, storing the
   // result in the DexCache. The referrer is used to identify the
   // target DexCache and ClassLoader to use for resolution.
-  String* ResolveString(uint32_t string_idx, const Method* referrer) {
+  String* ResolveString(uint32_t string_idx, const Method* referrer)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
     if (UNLIKELY(resolved_string == NULL)) {
       Class* declaring_class = referrer->GetDeclaringClass();
@@ -97,14 +112,14 @@
 
   // Resolve a String with the given index from the DexFile, storing the
   // result in the DexCache.
-  String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
+  String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Resolve a Type with the given index from the DexFile, storing the
   // result in the DexCache. The referrer is used to identity the
   // target DexCache and ClassLoader to use for resolution.
-  Class* ResolveType(const DexFile& dex_file,
-                     uint16_t type_idx,
-                     const Class* referrer) {
+  Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, const Class* referrer)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return ResolveType(dex_file,
                        type_idx,
                        referrer->GetDexCache(),
@@ -114,7 +129,8 @@
   // Resolve a Type with the given index from the DexFile, storing the
   // result in the DexCache. The referrer is used to identify the
   // target DexCache and ClassLoader to use for resolution.
-  Class* ResolveType(uint16_t type_idx, const Method* referrer) {
+  Class* ResolveType(uint16_t type_idx, const Method* referrer)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
     if (UNLIKELY(resolved_type == NULL)) {
       Class* declaring_class = referrer->GetDeclaringClass();
@@ -126,7 +142,8 @@
     return resolved_type;
   }
 
-  Class* ResolveType(uint16_t type_idx, const Field* referrer) {
+  Class* ResolveType(uint16_t type_idx, const Field* referrer)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     Class* declaring_class = referrer->GetDeclaringClass();
     DexCache* dex_cache = declaring_class->GetDexCache();
     Class* resolved_type = dex_cache->GetResolvedType(type_idx);
@@ -145,7 +162,8 @@
   Class* ResolveType(const DexFile& dex_file,
                      uint16_t type_idx,
                      DexCache* dex_cache,
-                     ClassLoader* class_loader);
+                     ClassLoader* class_loader)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Resolve a method with a given ID from the DexFile, storing the
   // result in DexCache. The ClassLinker and ClassLoader are used as
@@ -156,9 +174,11 @@
                         uint32_t method_idx,
                         DexCache* dex_cache,
                         ClassLoader* class_loader,
-                        bool is_direct);
+                        bool is_direct)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct) {
+  Method* ResolveMethod(uint32_t method_idx, const Method* referrer, bool is_direct)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     Method* resolved_method = referrer->GetDexCacheResolvedMethods()->Get(method_idx);
     if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {
       Class* declaring_class = referrer->GetDeclaringClass();
@@ -170,7 +190,8 @@
     return resolved_method;
   }
 
-  Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static) {
+  Field* ResolveField(uint32_t field_idx, const Method* referrer, bool is_static)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     Field* resolved_field =
         referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
     if (UNLIKELY(resolved_field == NULL)) {
@@ -192,7 +213,8 @@
                       uint32_t field_idx,
                       DexCache* dex_cache,
                       ClassLoader* class_loader,
-                      bool is_static);
+                      bool is_static)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Resolve a field with a given ID from the DexFile, storing the
   // result in DexCache. The ClassLinker and ClassLoader are used as
@@ -201,89 +223,132 @@
   Field* ResolveFieldJLS(const DexFile& dex_file,
                          uint32_t field_idx,
                          DexCache* dex_cache,
-                         ClassLoader* class_loader);
+                         ClassLoader* class_loader)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Get shorty from method index without resolution. Used to do handlerization.
-  const char* MethodShorty(uint32_t method_idx, Method* referrer, uint32_t* length);
+  const char* MethodShorty(uint32_t method_idx, Method* referrer, uint32_t* length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Returns true on success, false if there's an exception pending.
   // can_run_clinit=false allows the compiler to attempt to init a class,
   // given the restriction that no <clinit> execution is possible.
-  bool EnsureInitialized(Class* c, bool can_run_clinit, bool can_init_fields);
+  bool EnsureInitialized(Class* c, bool can_run_clinit, bool can_init_fields)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Initializes classes that have instances in the image but that have
   // <clinit> methods so they could not be initialized by the compiler.
-  void RunRootClinits();
+  void RunRootClinits() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void RegisterDexFile(const DexFile& dex_file);
-  void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
+  void RegisterDexFile(const DexFile& dex_file)
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void RegisterOatFile(const OatFile& oat_file);
+  void RegisterOatFile(const OatFile& oat_file)
+      LOCKS_EXCLUDED(dex_lock_);
 
   const std::vector<const DexFile*>& GetBootClassPath() {
     return boot_class_path_;
   }
 
-  void VisitClasses(ClassVisitor* visitor, void* arg) const;
+  void VisitClasses(ClassVisitor* visitor, void* arg) const
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_);
+  // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
+  // when calling the visitor.
+  void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) const
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_);
 
-  void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
+  void VisitRoots(Heap::RootVisitor* visitor, void* arg) const
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_, dex_lock_);
 
-  const DexFile& FindDexFile(const DexCache* dex_cache) const;
-  DexCache* FindDexCache(const DexFile& dex_file) const;
-  bool IsDexFileRegistered(const DexFile& dex_file) const;
-  void FixupDexCaches(Method* resolution_method) const;
+  const DexFile& FindDexFile(const DexCache* dex_cache) const
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  DexCache* FindDexCache(const DexFile& dex_file) const
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool IsDexFileRegistered(const DexFile& dex_file) const
+      LOCKS_EXCLUDED(dex_lock_);
+  void FixupDexCaches(Method* resolution_method) const
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Generate an oat file from a dex file
   bool GenerateOatFile(const std::string& dex_filename,
                        int oat_fd,
                        const std::string& oat_cache_filename);
 
-  const OatFile* FindOatFileFromOatLocation(const std::string& location);
+  const OatFile* FindOatFileFromOatLocation(const std::string& location)
+      LOCKS_EXCLUDED(dex_lock_);
+
+  const OatFile* FindOatFileFromOatLocationLocked(const std::string& location)
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
 
   // Finds the oat file for a dex location, generating the oat file if
   // it is missing or out of date. Returns the DexFile from within the
   // created oat file.
   const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
-                                                   const std::string& oat_location);
+                                                   const std::string& oat_location)
+      LOCKS_EXCLUDED(dex_lock_);
+  const DexFile* FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
+                                                         const std::string& oat_location)
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
   // Find a DexFile within an OatFile given a DexFile location. Note
   // that this returns null if the location checksum of the DexFile
   // does not match the OatFile.
-  const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location);
+  const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location)
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
 
   // Returns true if oat file contains the dex file with the given location and checksum
   static bool VerifyOatFileChecksums(const OatFile* oat_file,
                                      const std::string& dex_location,
-                                     uint32_t dex_location_checksum);
+                                     uint32_t dex_location_checksum)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // TODO: replace this with multiple methods that allocate the correct managed type.
   template <class T>
-  ObjectArray<T>* AllocObjectArray(size_t length) {
+  ObjectArray<T>* AllocObjectArray(size_t length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
   }
 
-  ObjectArray<Class>* AllocClassArray(size_t length) {
+  ObjectArray<Class>* AllocClassArray(size_t length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return ObjectArray<Class>::Alloc(GetClassRoot(kClassArrayClass), length);
   }
 
-  ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
+  ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void VerifyClass(Class* klass);
+  void VerifyClass(Class* klass) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
   bool VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass,
-                               Class::Status& oat_file_class_status);
-  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass);
-  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass);
+                               Class::Status& oat_file_class_status)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   Class* CreateProxyClass(String* name, ObjectArray<Class>* interfaces, ClassLoader* loader,
-                          ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws);
-  std::string GetDescriptorForProxy(const Class* proxy_class);
-  Method* FindMethodForProxy(const Class* proxy_class, const Method* proxy_method);
+                          ObjectArray<Method>* methods, ObjectArray<ObjectArray<Class> >* throws)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  std::string GetDescriptorForProxy(const Class* proxy_class)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  Method* FindMethodForProxy(const Class* proxy_class, const Method* proxy_method)
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Get the oat code for a method when its class isn't yet initialized
-  const void* GetOatCodeFor(const Method* method);
+  const void* GetOatCodeFor(const Method* method)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Relocate the OatFiles (ELF images)
-  void RelocateExecutable();
+  void RelocateExecutable() LOCKS_EXCLUDED(dex_lock_);
 
   pid_t GetClassesLockOwner(); // For SignalCatcher.
   pid_t GetDexLockOwner(); // For SignalCatcher.
@@ -291,47 +356,60 @@
  private:
   explicit ClassLinker(InternTable*);
 
-  const OatFile::OatMethod GetOatMethodFor(const Method* method);
+  const OatFile::OatMethod GetOatMethodFor(const Method* method)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Initialize class linker by bootstraping from dex files
-  void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path);
+  void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Initialize class linker from one or more images.
-  void InitFromImage();
-  OatFile* OpenOat(const ImageSpace* space);
-  static void InitFromImageCallback(Object* obj, void* arg);
+  void InitFromImage() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  OatFile* OpenOat(const ImageSpace* space)
+      LOCKS_EXCLUDED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  static void InitFromImageCallback(Object* obj, void* arg)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void FinishInit();
+  void FinishInit() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // For early bootstrapping by Init
-  Class* AllocClass(Class* java_lang_Class, size_t class_size);
+  Class* AllocClass(Class* java_lang_Class, size_t class_size)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Alloc* convenience functions to avoid needing to pass in Class*
   // values that are known to the ClassLinker such as
   // kObjectArrayClass and kJavaLangString etc.
-  Class* AllocClass(size_t class_size);
-  DexCache* AllocDexCache(const DexFile& dex_file);
-  Field* AllocField();
+  Class* AllocClass(size_t class_size) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  DexCache* AllocDexCache(const DexFile& dex_file)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  Field* AllocField() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  Method* AllocMethod() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* AllocMethod();
+  InterfaceEntry* AllocInterfaceEntry(Class* interface)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  InterfaceEntry* AllocInterfaceEntry(Class* interface);
-
-  Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type) {
+  Class* CreatePrimitiveClass(const char* descriptor, Primitive::Type type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     return InitializePrimitiveClass(AllocClass(sizeof(Class)), descriptor, type);
   }
   Class* InitializePrimitiveClass(Class* primitive_class,
                                   const char* descriptor,
-                                  Primitive::Type type);
+                                  Primitive::Type type)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
 
-  Class* CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader);
+  Class* CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void AppendToBootClassPath(const DexFile& dex_file);
-  void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
+  void AppendToBootClassPath(const DexFile& dex_file)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
-                         Class* c, SafeMap<uint32_t, Field*>& field_map);
+                         Class* c, SafeMap<uint32_t, Field*>& field_map)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   size_t SizeOfClass(const DexFile& dex_file,
                      const DexFile::ClassDef& dex_class_def);
@@ -339,15 +417,18 @@
   void LoadClass(const DexFile& dex_file,
                  const DexFile::ClassDef& dex_class_def,
                  SirtRef<Class>& klass,
-                 ClassLoader* class_loader);
+                 ClassLoader* class_loader)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, SirtRef<Class>& klass,
                  SirtRef<Field>& dst);
 
   void LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& dex_method,
-                  SirtRef<Class>& klass, SirtRef<Method>& dst);
+                  SirtRef<Class>& klass, SirtRef<Method>& dst)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void FixupStaticTrampolines(Class* klass);
+  void FixupStaticTrampolines(Class* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // Finds the associated oat class for a dex_file and descriptor
   const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor);
@@ -355,68 +436,97 @@
   // Attempts to insert a class into a class table.  Returns NULL if
   // the class was inserted, otherwise returns an existing class with
   // the same descriptor and ClassLoader.
-  Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class);
+  Class* InsertClass(const StringPiece& descriptor, Class* klass, bool image_class)
+      LOCKS_EXCLUDED(GlobalSynchronization::classlinker_classes_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
+  void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache)
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
   bool IsDexFileRegisteredLocked(const DexFile& dex_file) const EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
-  void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
+  void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
 
-  bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics);
+  bool InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
   bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
-  bool ValidateSuperClassDescriptors(const Class* klass);
-  bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields);
+  bool ValidateSuperClassDescriptors(const Class* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
   // Initialize static fields, returns true if fields were initialized.
-  bool InitializeStaticFields(Class* klass);
+  bool InitializeStaticFields(Class* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
                                                 const Class* klass1,
-                                                const Class* klass2);
+                                                const Class* klass2)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   bool IsSameMethodSignatureInDifferentClassContexts(const Method* descriptor,
                                                      const Class* klass1,
-                                                     const Class* klass2);
+                                                     const Class* klass2)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
+  bool LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LinkSuperClass(SirtRef<Class>& klass);
+  bool LinkSuperClass(SirtRef<Class>& klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file);
+  bool LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
+  bool LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LinkVirtualMethods(SirtRef<Class>& klass);
+  bool LinkVirtualMethods(SirtRef<Class>& klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces);
+  bool LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  bool LinkStaticFields(SirtRef<Class>& klass);
-  bool LinkInstanceFields(SirtRef<Class>& klass);
-  bool LinkFields(SirtRef<Class>& klass, bool is_static);
+  bool LinkStaticFields(SirtRef<Class>& klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool LinkInstanceFields(SirtRef<Class>& klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  bool LinkFields(SirtRef<Class>& klass, bool is_static)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
 
-  void CreateReferenceInstanceOffsets(SirtRef<Class>& klass);
-  void CreateReferenceStaticOffsets(SirtRef<Class>& klass);
+  void CreateReferenceInstanceOffsets(SirtRef<Class>& klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  void CreateReferenceStaticOffsets(SirtRef<Class>& klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
   void CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
-                              uint32_t reference_offsets);
+                              uint32_t reference_offsets)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // For use by ImageWriter to find DexCaches for its roots
   const std::vector<DexCache*>& GetDexCaches() {
     return dex_caches_;
   }
 
-  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file);
-  const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location);
-  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location);
+  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
+      LOCKS_EXCLUDED(dex_lock_);
+  const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
+  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
   const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
                                                  const std::string& dex_location,
                                                  uint32_t dex_location_checksum)
-      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
+      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
-  Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class);
-  Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype);
+  Method* CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
+  Method* CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   std::vector<const DexFile*> boot_class_path_;
 
-  mutable Mutex dex_lock_;
+  mutable Mutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   std::vector<const DexFile*> dex_files_ GUARDED_BY(dex_lock_);
   std::vector<DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
   std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
@@ -425,13 +535,14 @@
   // multimap from a string hash code of a class descriptor to
   // Class* instances. Results should be compared for a matching
   // Class::descriptor_ and Class::class_loader_.
-  mutable Mutex classes_lock_;
   typedef std::multimap<size_t, Class*> Table;
-  Table image_classes_  GUARDED_BY(classes_lock_);
-  Table classes_ GUARDED_BY(classes_lock_);
+  Table image_classes_  GUARDED_BY(GlobalSynchronization::classlinker_classes_lock_);
+  Table classes_ GUARDED_BY(GlobalSynchronization::classlinker_classes_lock_);
 
   Class* LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
-                           size_t hash, const Table& classes) EXCLUSIVE_LOCKS_REQUIRED(classes_lock_);
+                           size_t hash, const Table& classes)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
+      EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::classlinker_classes_lock_);
 
   // indexes into class_roots_.
   // needs to be kept in sync with class_roots_descriptors_.
@@ -447,8 +558,6 @@
     kJavaLangReflectMethod,
     kJavaLangReflectProxy,
     kJavaLangClassLoader,
-    kDalvikSystemBaseDexClassLoader,
-    kDalvikSystemPathClassLoader,
     kJavaLangThrowable,
     kJavaLangClassNotFoundException,
     kJavaLangStackTraceElement,
@@ -474,14 +583,16 @@
   };
   ObjectArray<Class>* class_roots_;
 
-  Class* GetClassRoot(ClassRoot class_root) {
+  Class* GetClassRoot(ClassRoot class_root)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
     DCHECK(class_roots_ != NULL);
     Class* klass = class_roots_->Get(class_root);
     DCHECK(klass != NULL);
     return klass;
   }
 
-  void SetClassRoot(ClassRoot class_root, Class* klass);
+  void SetClassRoot(ClassRoot class_root, Class* klass)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   ObjectArray<Class>* GetClassRoots() {
     DCHECK(class_roots_ != NULL);