fix(util/stackinfo): try to fix incorrect main thread stack size on OSX

This fix tries to fix two failures on our unit tests.
     tests/kernel/normalizer
     tests/kernel/type_checker

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-12-04 08:56:41 -08:00
parent d481cb251d
commit def186a9cd
3 changed files with 27 additions and 15 deletions

View file

@ -71,7 +71,7 @@ public:
m_thread(
[&](Function&& fun, Args&&... args) {
m_flag_addr.store(get_flag_addr());
save_stack_info();
save_stack_info(false);
fun(std::forward<Args>(args)...);
m_flag_addr.store(&m_dummy_addr); // see comment before m_dummy_addr
},

View file

@ -17,22 +17,34 @@ void throw_get_stack_size_failed() {
}
#ifdef LEAN_WINDOWS
size_t get_stack_size() {
size_t get_stack_size(int main) {
return LEAN_WIN_STACK_SIZE;
}
#elif defined (__APPLE__)
size_t get_stack_size() {
pthread_attr_t attr;
memset (&attr, 0, sizeof(attr));
pthread_attr_init(&attr);
size_t result;
if (pthread_attr_getstacksize(&attr, &result) != 0) {
throw_get_stack_size_failed();
#include <sys/resource.h>
size_t get_stack_size(int main) {
if (main) {
// Retrieve stack size of the main thread.
struct rlimit curr;
if (getrlimit(RLIMIT_STACK, &curr) != 0) {
throw_get_stack_size_failed();
}
return curr.rlim_max;
} else {
// This branch retrieves the default thread size for pthread threads.
// This is *not* the stack size of the main thread.
pthread_attr_t attr;
memset (&attr, 0, sizeof(attr));
pthread_attr_init(&attr);
size_t result;
if (pthread_attr_getstacksize(&attr, &result) != 0) {
throw_get_stack_size_failed();
}
return result;
}
return result;
}
#else
size_t get_stack_size() {
size_t get_stack_size(int ) {
pthread_attr_t attr;
memset (&attr, 0, sizeof(attr));
if (pthread_getattr_np(pthread_self(), &attr) != 0) {
@ -53,8 +65,8 @@ size_t get_stack_size() {
static thread_local size_t g_stack_size;
static thread_local size_t g_stack_base;
void save_stack_info() {
g_stack_size = get_stack_size();
void save_stack_info(bool main) {
g_stack_size = get_stack_size(main);
char x;
g_stack_base = reinterpret_cast<size_t>(&x);
}

View file

@ -6,8 +6,8 @@ Author: Leonardo de Moura
*/
#pragma once
namespace lean {
size_t get_stack_size();
void save_stack_info();
size_t get_stack_size(bool main);
void save_stack_info(bool main = true);
size_t get_used_stack_size();
size_t get_available_stack_size();
/**