diff --git a/Libraries/LibThreading/Thread.cpp b/Libraries/LibThreading/Thread.cpp index 3e9cfd2cf92..f64fe604ef3 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 bbc0b38c533..3b4951cd108 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