Skip to content

Commit

Permalink
osx: set the default thread stack size to RLIMIT_STACK
Browse files Browse the repository at this point in the history
Fixes: libuv#669
PR-URL: libuv#671
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
saghul committed Jan 5, 2016
1 parent a564ef0 commit 3db07cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/unix/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <errno.h>

#include <sys/time.h>
#include <sys/resource.h> /* getrlimit() */

#undef NANOSEC
#define NANOSEC ((uint64_t) 1e9)
Expand Down Expand Up @@ -55,6 +56,11 @@ static void* uv__thread_start(void *arg)
int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
struct thread_ctx* ctx;
int err;
pthread_attr_t* attr;
#if defined(__APPLE__)
pthread_attr_t attr_storage;
struct rlimit lim;
#endif

ctx = uv__malloc(sizeof(*ctx));
if (ctx == NULL)
Expand All @@ -63,7 +69,30 @@ int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
ctx->entry = entry;
ctx->arg = arg;

err = pthread_create(tid, NULL, uv__thread_start, ctx);
/* On OSX threads other than the main thread are created with a reduced stack
* size by default, adjust it to RLIMIT_STACK.
*/
#if defined(__APPLE__)
if (getrlimit(RLIMIT_STACK, &lim))
abort();

attr = &attr_storage;
if (pthread_attr_init(attr))
abort();

if (lim.rlim_cur != RLIM_INFINITY &&
lim.rlim_cur >= PTHREAD_STACK_MIN) {
if (pthread_attr_setstacksize(attr, lim.rlim_cur))
abort();
}
#else
attr = NULL;
#endif

err = pthread_create(tid, attr, uv__thread_start, ctx);

if (attr != NULL)
pthread_attr_destroy(attr);

if (err)
uv__free(ctx);
Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ TEST_DECLARE (threadpool_cancel_work)
TEST_DECLARE (threadpool_cancel_fs)
TEST_DECLARE (threadpool_cancel_single)
TEST_DECLARE (thread_local_storage)
TEST_DECLARE (thread_stack_size)
TEST_DECLARE (thread_mutex)
TEST_DECLARE (thread_rwlock)
TEST_DECLARE (thread_rwlock_trylock)
Expand Down Expand Up @@ -717,6 +718,7 @@ TASK_LIST_START
TEST_ENTRY (threadpool_cancel_fs)
TEST_ENTRY (threadpool_cancel_single)
TEST_ENTRY (thread_local_storage)
TEST_ENTRY (thread_stack_size)
TEST_ENTRY (thread_mutex)
TEST_ENTRY (thread_rwlock)
TEST_ENTRY (thread_rwlock_trylock)
Expand Down
21 changes: 21 additions & 0 deletions test/test-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,24 @@ TEST_IMPL(thread_local_storage) {
uv_key_delete(&tls_key);
return 0;
}


#if defined(__APPLE__)
static void thread_check_stack(void* arg) {
/* 512KB is the default stack size of threads other than the main thread
* on OSX. */
ASSERT(pthread_get_stacksize_np(pthread_self()) > 512*1024);
}
#endif


TEST_IMPL(thread_stack_size) {
#if defined(__APPLE__)
uv_thread_t thread;
ASSERT(0 == uv_thread_create(&thread, thread_check_stack, NULL));
ASSERT(0 == uv_thread_join(&thread));
return 0;
#else
RETURN_SKIP("OSX only test");
#endif
}

0 comments on commit 3db07cc

Please sign in to comment.