Skip to content

Commit 3db07cc

Browse files
committed
osx: set the default thread stack size to RLIMIT_STACK
Fixes: libuv#669 PR-URL: libuv#671 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent a564ef0 commit 3db07cc

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/unix/thread.c

+30-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <errno.h>
2828

2929
#include <sys/time.h>
30+
#include <sys/resource.h> /* getrlimit() */
3031

3132
#undef NANOSEC
3233
#define NANOSEC ((uint64_t) 1e9)
@@ -55,6 +56,11 @@ static void* uv__thread_start(void *arg)
5556
int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
5657
struct thread_ctx* ctx;
5758
int err;
59+
pthread_attr_t* attr;
60+
#if defined(__APPLE__)
61+
pthread_attr_t attr_storage;
62+
struct rlimit lim;
63+
#endif
5864

5965
ctx = uv__malloc(sizeof(*ctx));
6066
if (ctx == NULL)
@@ -63,7 +69,30 @@ int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
6369
ctx->entry = entry;
6470
ctx->arg = arg;
6571

66-
err = pthread_create(tid, NULL, uv__thread_start, ctx);
72+
/* On OSX threads other than the main thread are created with a reduced stack
73+
* size by default, adjust it to RLIMIT_STACK.
74+
*/
75+
#if defined(__APPLE__)
76+
if (getrlimit(RLIMIT_STACK, &lim))
77+
abort();
78+
79+
attr = &attr_storage;
80+
if (pthread_attr_init(attr))
81+
abort();
82+
83+
if (lim.rlim_cur != RLIM_INFINITY &&
84+
lim.rlim_cur >= PTHREAD_STACK_MIN) {
85+
if (pthread_attr_setstacksize(attr, lim.rlim_cur))
86+
abort();
87+
}
88+
#else
89+
attr = NULL;
90+
#endif
91+
92+
err = pthread_create(tid, attr, uv__thread_start, ctx);
93+
94+
if (attr != NULL)
95+
pthread_attr_destroy(attr);
6796

6897
if (err)
6998
uv__free(ctx);

test/test-list.h

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ TEST_DECLARE (threadpool_cancel_work)
294294
TEST_DECLARE (threadpool_cancel_fs)
295295
TEST_DECLARE (threadpool_cancel_single)
296296
TEST_DECLARE (thread_local_storage)
297+
TEST_DECLARE (thread_stack_size)
297298
TEST_DECLARE (thread_mutex)
298299
TEST_DECLARE (thread_rwlock)
299300
TEST_DECLARE (thread_rwlock_trylock)
@@ -717,6 +718,7 @@ TASK_LIST_START
717718
TEST_ENTRY (threadpool_cancel_fs)
718719
TEST_ENTRY (threadpool_cancel_single)
719720
TEST_ENTRY (thread_local_storage)
721+
TEST_ENTRY (thread_stack_size)
720722
TEST_ENTRY (thread_mutex)
721723
TEST_ENTRY (thread_rwlock)
722724
TEST_ENTRY (thread_rwlock_trylock)

test/test-thread.c

+21
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,24 @@ TEST_IMPL(thread_local_storage) {
209209
uv_key_delete(&tls_key);
210210
return 0;
211211
}
212+
213+
214+
#if defined(__APPLE__)
215+
static void thread_check_stack(void* arg) {
216+
/* 512KB is the default stack size of threads other than the main thread
217+
* on OSX. */
218+
ASSERT(pthread_get_stacksize_np(pthread_self()) > 512*1024);
219+
}
220+
#endif
221+
222+
223+
TEST_IMPL(thread_stack_size) {
224+
#if defined(__APPLE__)
225+
uv_thread_t thread;
226+
ASSERT(0 == uv_thread_create(&thread, thread_check_stack, NULL));
227+
ASSERT(0 == uv_thread_join(&thread));
228+
return 0;
229+
#else
230+
RETURN_SKIP("OSX only test");
231+
#endif
232+
}

0 commit comments

Comments
 (0)