From a116f08acba19a19975df742a12ba5a9a1400484 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 24 Feb 2026 21:26:39 +0100 Subject: [PATCH] LibThreading: Allow configuring thread stack size Add set_stack_size() to Threading::Thread and use pthread_attr_t to apply it in start(). --- Libraries/LibThreading/Thread.cpp | 14 ++++++++++++-- Libraries/LibThreading/Thread.h | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Libraries/LibThreading/Thread.cpp b/Libraries/LibThreading/Thread.cpp index 3e9cfd2cf9..f64fe604ef 100644 --- a/Libraries/LibThreading/Thread.cpp +++ b/Libraries/LibThreading/Thread.cpp @@ -70,10 +70,17 @@ void Thread::start() // Set this first so that the other thread starts out seeing m_state == Running. m_state = Threading::ThreadState::Running; + pthread_attr_t attr; + pthread_attr_t* attr_ptr = nullptr; + if (m_stack_size > 0) { + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, m_stack_size); + attr_ptr = &attr; + } + int rc = pthread_create( &m_tid, - // FIXME: Use pthread_attr_t to start a thread detached if that was requested by the user before the call to start(). - nullptr, + attr_ptr, [](void* arg) -> void* { auto self = adopt_ref(*static_cast(arg)); @@ -112,6 +119,9 @@ void Thread::start() }, &NonnullRefPtr(*this).leak_ref()); + if (attr_ptr) + pthread_attr_destroy(attr_ptr); + VERIFY(rc == 0); } diff --git a/Libraries/LibThreading/Thread.h b/Libraries/LibThreading/Thread.h index bbc0b38c53..3b4951cd10 100644 --- a/Libraries/LibThreading/Thread.h +++ b/Libraries/LibThreading/Thread.h @@ -59,6 +59,8 @@ public: ErrorOr set_priority(int priority); ErrorOr get_priority() const; + void set_stack_size(size_t size) { m_stack_size = size; } + // Only callable in the Startable state. void start(); // Only callable in the Running state. @@ -81,6 +83,7 @@ private: pthread_t m_tid {}; ByteString m_thread_name; Atomic m_state { ThreadState::Startable }; + size_t m_stack_size { 0 }; }; template