osx: set the default thread stack size to RLIMIT_STACK
Fixes: https://github.com/libuv/libuv/issues/669 PR-URL: https://github.com/libuv/libuv/pull/671 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
a564ef09dc
commit
3db07cc379
@ -27,6 +27,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <sys/resource.h> /* getrlimit() */
|
||||||
|
|
||||||
#undef NANOSEC
|
#undef NANOSEC
|
||||||
#define NANOSEC ((uint64_t) 1e9)
|
#define NANOSEC ((uint64_t) 1e9)
|
||||||
@ -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) {
|
int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
|
||||||
struct thread_ctx* ctx;
|
struct thread_ctx* ctx;
|
||||||
int err;
|
int err;
|
||||||
|
pthread_attr_t* attr;
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
pthread_attr_t attr_storage;
|
||||||
|
struct rlimit lim;
|
||||||
|
#endif
|
||||||
|
|
||||||
ctx = uv__malloc(sizeof(*ctx));
|
ctx = uv__malloc(sizeof(*ctx));
|
||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
@ -63,7 +69,30 @@ int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
|
|||||||
ctx->entry = entry;
|
ctx->entry = entry;
|
||||||
ctx->arg = arg;
|
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)
|
if (err)
|
||||||
uv__free(ctx);
|
uv__free(ctx);
|
||||||
|
|||||||
@ -294,6 +294,7 @@ TEST_DECLARE (threadpool_cancel_work)
|
|||||||
TEST_DECLARE (threadpool_cancel_fs)
|
TEST_DECLARE (threadpool_cancel_fs)
|
||||||
TEST_DECLARE (threadpool_cancel_single)
|
TEST_DECLARE (threadpool_cancel_single)
|
||||||
TEST_DECLARE (thread_local_storage)
|
TEST_DECLARE (thread_local_storage)
|
||||||
|
TEST_DECLARE (thread_stack_size)
|
||||||
TEST_DECLARE (thread_mutex)
|
TEST_DECLARE (thread_mutex)
|
||||||
TEST_DECLARE (thread_rwlock)
|
TEST_DECLARE (thread_rwlock)
|
||||||
TEST_DECLARE (thread_rwlock_trylock)
|
TEST_DECLARE (thread_rwlock_trylock)
|
||||||
@ -717,6 +718,7 @@ TASK_LIST_START
|
|||||||
TEST_ENTRY (threadpool_cancel_fs)
|
TEST_ENTRY (threadpool_cancel_fs)
|
||||||
TEST_ENTRY (threadpool_cancel_single)
|
TEST_ENTRY (threadpool_cancel_single)
|
||||||
TEST_ENTRY (thread_local_storage)
|
TEST_ENTRY (thread_local_storage)
|
||||||
|
TEST_ENTRY (thread_stack_size)
|
||||||
TEST_ENTRY (thread_mutex)
|
TEST_ENTRY (thread_mutex)
|
||||||
TEST_ENTRY (thread_rwlock)
|
TEST_ENTRY (thread_rwlock)
|
||||||
TEST_ENTRY (thread_rwlock_trylock)
|
TEST_ENTRY (thread_rwlock_trylock)
|
||||||
|
|||||||
@ -209,3 +209,24 @@ TEST_IMPL(thread_local_storage) {
|
|||||||
uv_key_delete(&tls_key);
|
uv_key_delete(&tls_key);
|
||||||
return 0;
|
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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user