Skip to content

Commit b320a65

Browse files
committed
Increase stack size for all secondary threads on Apple platforms
1 parent 0fdb93c commit b320a65

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

core/object/worker_thread_pool.cpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -364,24 +364,12 @@ WorkerThreadPool::TaskID WorkerThreadPool::_add_task(const Callable &p_callable,
364364
if (pump_task_count >= thread_count) {
365365
print_verbose(vformat("A greater number of dedicated threads were requested (%d) than threads available (%d). Please increase the number of available worker task threads. Recovering this session by spawning more worker task threads.", pump_task_count + 1, thread_count)); // +1 because we want to keep a Thread without any pump tasks free.
366366

367-
Thread::Settings settings;
368-
#ifdef __APPLE__
369-
// The default stack size for new threads on Apple platforms is 512KiB.
370-
// This is insufficient when using a library like SPIRV-Cross,
371-
// which can generate deep stacks and result in a stack overflow.
372-
#ifdef DEV_ENABLED
373-
// Debug builds need an even larger stack size.
374-
settings.stack_size = 2 * 1024 * 1024; // 2 MiB
375-
#else
376-
settings.stack_size = 1 * 1024 * 1024; // 1 MiB
377-
#endif
378-
#endif
379367
// Re-sizing implies relocation, which is not supported for this array.
380368
CRASH_COND_MSG(thread_count + 1 > (int)threads.get_capacity(), "Reserve trick for worker thread pool failed. Crashing.");
381369
threads.resize_initialized(thread_count + 1);
382370
threads[thread_count].index = thread_count;
383371
threads[thread_count].pool = this;
384-
threads[thread_count].thread.start(&WorkerThreadPool::_thread_function, &threads[thread_count], settings);
372+
threads[thread_count].thread.start(&WorkerThreadPool::_thread_function, &threads[thread_count]);
385373
thread_ids.insert(threads[thread_count].thread.get_id(), thread_count);
386374
}
387375
}
@@ -838,23 +826,10 @@ void WorkerThreadPool::init(int p_thread_count, float p_low_priority_task_ratio)
838826
#endif
839827
threads.resize(p_thread_count);
840828

841-
Thread::Settings settings;
842-
#ifdef __APPLE__
843-
// The default stack size for new threads on Apple platforms is 512KiB.
844-
// This is insufficient when using a library like SPIRV-Cross,
845-
// which can generate deep stacks and result in a stack overflow.
846-
#ifdef DEV_ENABLED
847-
// Debug builds need an even larger stack size.
848-
settings.stack_size = 2 * 1024 * 1024; // 2 MiB
849-
#else
850-
settings.stack_size = 1 * 1024 * 1024; // 1 MiB
851-
#endif
852-
#endif
853-
854829
for (uint32_t i = 0; i < threads.size(); i++) {
855830
threads[i].index = i;
856831
threads[i].pool = this;
857-
threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i], settings);
832+
threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
858833
thread_ids.insert(threads[i].thread.get_id(), i);
859834
}
860835
}

drivers/apple/thread_apple.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,20 @@ Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settin
9292
break;
9393
}
9494

95-
if (p_settings.stack_size > 0) {
96-
pthread_attr_setstacksize(&attr, p_settings.stack_size);
97-
}
95+
// The default stack size for secondary threads on Apple platforms is 512KiB.
96+
// This is insufficient when using a library like SPIRV-Cross, which can generate deep stacks and result in a stack overflow.
97+
// It also creates a problematic discrepancy with other platforms, where secondary threads are often at least 1 MiB.
98+
pthread_attr_setstacksize(&attr,
99+
#if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer)
100+
// ASan (and to some degree TSan) needs a lot of extra stack size.
101+
4 * 1024 * 1024 // 4 MiB
102+
#elif !defined(__OPTIMIZE__)
103+
// Unoptimized builds also need a larger stack size.
104+
2 * 1024 * 1024 // 2 MiB
105+
#else
106+
1 * 1024 * 1024 // 1 MiB
107+
#endif
108+
);
98109

99110
// Create the thread
100111
pthread_create(&pthread, &attr, thread_callback, thread_data);

drivers/apple/thread_apple.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ class Thread {
5757

5858
struct Settings {
5959
Priority priority;
60-
/// Override the default stack size (0 means default)
61-
uint64_t stack_size = 0;
6260
Settings() { priority = PRIORITY_NORMAL; }
6361
};
6462

0 commit comments

Comments
 (0)