Fix Mac build and make -jN use a total of N threads, not N+1.

Change-Id: I41dc613e76d6ac85a21b3286b1a555dcc79bae95
diff --git a/src/compiler.cc b/src/compiler.cc
index 45e4d04..98d42b7 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -380,27 +380,39 @@
 
 class WorkerThread {
  public:
-  WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe)
-      : context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
-    CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Init, this), "compiler worker thread");
+  WorkerThread(Context* context, size_t begin, size_t end, Callback callback, size_t stripe, bool spawn)
+      : spawn_(spawn), context_(context), begin_(begin), end_(end), callback_(callback), stripe_(stripe) {
+    if (spawn_) {
+      CHECK_PTHREAD_CALL(pthread_create, (&pthread_, NULL, &Go, this), "compiler worker thread");
+    }
   }
 
   ~WorkerThread() {
-    CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
+    if (spawn_) {
+      CHECK_PTHREAD_CALL(pthread_join, (pthread_, NULL), "compiler worker shutdown");
+    }
   }
 
  private:
-  static void* Init(void* arg) {
+  static void* Go(void* arg) {
     WorkerThread* worker = reinterpret_cast<WorkerThread*>(arg);
     Runtime* runtime = Runtime::Current();
-    runtime->AttachCurrentThread("Compiler Worker", true);
+    if (worker->spawn_) {
+      runtime->AttachCurrentThread("Compiler Worker", true);
+    }
     Thread::Current()->SetState(Thread::kRunnable);
     worker->Run();
-    Thread::Current()->SetState(Thread::kNative);
-    runtime->DetachCurrentThread();
+    if (worker->spawn_) {
+      Thread::Current()->SetState(Thread::kNative);
+      runtime->DetachCurrentThread();
+    }
     return NULL;
   }
 
+  void Go() {
+    Go(this);
+  }
+
   void Run() {
     for (size_t i = begin_; i < end_; i += stripe_) {
       callback_(context_, i);
@@ -408,20 +420,25 @@
   }
 
   pthread_t pthread_;
+  bool spawn_;
 
   Context* context_;
   size_t begin_;
   size_t end_;
   Callback* callback_;
   size_t stripe_;
+
+  friend void ForAll(Context*, size_t, size_t, Callback, size_t);
 };
 
 void ForAll(Context* context, size_t begin, size_t end, Callback callback, size_t thread_count) {
-  std::vector<WorkerThread*> threads;
+  CHECK_GT(thread_count, 0U);
 
+  std::vector<WorkerThread*> threads;
   for (size_t i = 0; i < thread_count; ++i) {
-    threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count));
+    threads.push_back(new WorkerThread(context, begin + i, end, callback, thread_count, (i != 0)));
   }
+  threads[0]->Go();
 
   // Switch to kVmWait while we're blocked waiting for the other threads to finish.
   ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index 33672b3..f8c342e 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -417,12 +417,7 @@
   uintptr_t image_base = 0;
   std::string host_prefix;
   std::vector<const char*> runtime_args;
-#if defined(__APPLE__)
-  // Apple lacks GetOwner and therefore working Mutexes, so only use one thread on that platform
-  int thread_count = 1;
-#else
   int thread_count = 2;
-#endif
 
   for (int i = 0; i < argc; i++) {
     const StringPiece option(argv[i]);
@@ -449,14 +444,11 @@
         usage();
       }
     } else if (option.starts_with("-j")) {
-#if! defined(__APPLE__)
       const char* thread_count_str = option.substr(strlen("-j")).data();
-      // Apple lacks GetOwner and therefore working Mutexes, so we ignore -j on that platform
       if (!ParseInt(thread_count_str, &thread_count)) {
         fprintf(stderr, "could not parse -j argument '%s' as an integer\n", thread_count_str);
         usage();
       }
-#endif
     } else if (option.starts_with("--oat-name=")) {
       oat_name = option.substr(strlen("--oat-name=")).data();
     } else if (option.starts_with("--image=")) {
diff --git a/src/runtime_support.cc b/src/runtime_support.cc
index f9f3d07..b68f7ac 100644
--- a/src/runtime_support.cc
+++ b/src/runtime_support.cc
@@ -494,7 +494,7 @@
     if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
                  fh.FieldSize() != expected_size)) {
       self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
-                               "Attempted read of %d-bit %s on field '%s'",
+                               "Attempted read of %zd-bit %s on field '%s'",
                                expected_size * (32 / sizeof(int32_t)),
                                is_primitive ? "primitive" : "non-primitive",
                                PrettyField(resolved_field, true).c_str());