Make Thread::InitStackHwm work on Mac OS.

Change-Id: Id0170dbdff8112d8e48d41ab50a311076863151e
diff --git a/src/utils.cc b/src/utils.cc
index 67fda67..bfa0c4f 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -66,6 +66,27 @@
 #endif
 }
 
+void GetThreadStack(void*& stack_base, size_t& stack_size) {
+#if defined(__APPLE__)
+  stack_size = pthread_get_stacksize_np(pthread_self());
+  void* stack_addr = pthread_get_stackaddr_np(pthread_self());
+
+  // Check whether stack_addr is the base or end of the stack.
+  // (On Mac OS 10.7, it's the end.)
+  int stack_variable;
+  if (stack_addr > &stack_variable) {
+    stack_base = reinterpret_cast<byte*>(stack_addr) - stack_size;
+  } else {
+    stack_base = stack_addr;
+  }
+#else
+  pthread_attr_t attributes;
+  CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__);
+  CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &stack_base, &stack_size), __FUNCTION__);
+  CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
+#endif
+}
+
 bool ReadFileToString(const std::string& file_name, std::string* result) {
   UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false));
   if (file.get() == NULL) {