Add mutex lock to CompilationUnit.

Change-Id: I2555dcc72ca8a765627b9267bfc32ad97315472f
diff --git a/src/compiler_llvm/compilation_unit.cc b/src/compiler_llvm/compilation_unit.cc
index 8be388d..9092485 100644
--- a/src/compiler_llvm/compilation_unit.cc
+++ b/src/compiler_llvm/compilation_unit.cc
@@ -111,8 +111,8 @@
 
 
 CompilationUnit::CompilationUnit(InstructionSet insn_set, size_t elf_idx)
-: insn_set_(insn_set), elf_idx_(elf_idx), context_(new llvm::LLVMContext()),
-  mem_usage_(0), num_elf_funcs_(0) {
+: cunit_lock_("compilation_unit_lock"), insn_set_(insn_set), elf_idx_(elf_idx),
+  context_(new llvm::LLVMContext()), mem_usage_(0), num_elf_funcs_(0) {
 
   // Create the module and include the runtime function declaration
   module_ = new llvm::Module("art", *context_);
@@ -146,6 +146,7 @@
 
 
 bool CompilationUnit::WriteBitcodeToFile(const std::string& bitcode_filename) {
+  MutexLock GUARD(cunit_lock_);
   std::string errmsg;
 
   llvm::OwningPtr<llvm::tool_output_file> out_file(
@@ -166,6 +167,8 @@
 
 
 bool CompilationUnit::Materialize() {
+  MutexLock GUARD(cunit_lock_);
+
   // Prepare the pipe between parent process and child process
   int pipe_fd[2];
   if (pipe(pipe_fd) == -1) {
@@ -246,12 +249,14 @@
 
 void CompilationUnit::RegisterCompiledMethod(const llvm::Function* func,
                                              CompiledMethod* compiled_method) {
+  MutexLock GUARD(cunit_lock_);
   compiled_methods_map_.Put(func, compiled_method);
 }
 
 
 void CompilationUnit::UpdateFrameSizeInBytes(const llvm::Function* func,
                                              size_t frame_size_in_bytes) {
+  MutexLock GUARD(cunit_lock_);
   SafeMap<const llvm::Function*, CompiledMethod*>::iterator iter =
     compiled_methods_map_.find(func);
 
diff --git a/src/compiler_llvm/compilation_unit.h b/src/compiler_llvm/compilation_unit.h
index d050820..2f98521 100644
--- a/src/compiler_llvm/compilation_unit.h
+++ b/src/compiler_llvm/compilation_unit.h
@@ -17,6 +17,7 @@
 #ifndef ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_
 #define ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_
 
+#include "../mutex.h"
 #include "elf_image.h"
 #include "globals.h"
 #include "instruction_set.h"
@@ -55,26 +56,33 @@
   }
 
   InstructionSet GetInstructionSet() const {
+    cunit_lock_.AssertHeld();
     return insn_set_;
   }
 
   llvm::LLVMContext* GetLLVMContext() const {
+    cunit_lock_.AssertHeld();
     return context_.get();
   }
 
   llvm::Module* GetModule() const {
+    cunit_lock_.AssertHeld();
     return module_;
   }
 
   IRBuilder* GetIRBuilder() const {
+    cunit_lock_.AssertHeld();
     return irb_.get();
   }
 
   ElfImage GetElfImage() const {
+    MutexLock GUARD(cunit_lock_);
+    CHECK_GT(elf_image_.size(), 0u);
     return ElfImage(elf_image_);
   }
 
   uint16_t AcquireUniqueElfFuncIndex() {
+    cunit_lock_.AssertHeld();
     CHECK(num_elf_funcs_ < UINT16_MAX);
     return num_elf_funcs_++;
   }
@@ -84,14 +92,17 @@
   bool Materialize();
 
   bool IsMaterialized() const {
+    MutexLock GUARD(cunit_lock_);
     return (context_.get() == NULL);
   }
 
   bool IsMaterializeThresholdReached() const {
+    MutexLock GUARD(cunit_lock_);
     return (mem_usage_ > 100000000u); // (threshold: 100 MB)
   }
 
   void AddMemUsageApproximation(size_t usage) {
+    MutexLock GUARD(cunit_lock_);
     mem_usage_ += usage;
   }
 
@@ -99,6 +110,8 @@
 
   void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes);
 
+  mutable Mutex cunit_lock_;
+
  private:
   InstructionSet insn_set_;
   const size_t elf_idx_;
diff --git a/src/compiler_llvm/compiler_llvm.cc b/src/compiler_llvm/compiler_llvm.cc
index 1623b2c..ca0a4da 100644
--- a/src/compiler_llvm/compiler_llvm.cc
+++ b/src/compiler_llvm/compiler_llvm.cc
@@ -258,6 +258,8 @@
 
   EnsureCompilationUnit();
 
+  MutexLock GUARD_CUNIT(curr_cunit_->cunit_lock_);
+
   UniquePtr<MethodCompiler> method_compiler(
       new MethodCompiler(curr_cunit_, compiler_, oat_compilation_unit));
 
@@ -271,6 +273,8 @@
 
   EnsureCompilationUnit();
 
+  MutexLock GUARD_CUNIT(curr_cunit_->cunit_lock_);
+
   UniquePtr<JniCompiler> jni_compiler(
       new JniCompiler(curr_cunit_, *compiler_, oat_compilation_unit));
 
@@ -284,6 +288,8 @@
 
   EnsureCompilationUnit();
 
+  MutexLock GUARD_CUNIT(curr_cunit_->cunit_lock_);
+
   UniquePtr<UpcallCompiler> upcall_compiler(
     new UpcallCompiler(curr_cunit_, *compiler_));