LibThreading: Allow configuring thread stack size
Add set_stack_size() to Threading::Thread and use pthread_attr_t to apply it in start().
This commit is contained in:
parent
5eeac1b9dd
commit
a116f08acb
2 changed files with 15 additions and 2 deletions
|
|
@ -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<Thread*>(arg));
|
||||
|
||||
|
|
@ -112,6 +119,9 @@ void Thread::start()
|
|||
},
|
||||
&NonnullRefPtr(*this).leak_ref());
|
||||
|
||||
if (attr_ptr)
|
||||
pthread_attr_destroy(attr_ptr);
|
||||
|
||||
VERIFY(rc == 0);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ public:
|
|||
ErrorOr<void> set_priority(int priority);
|
||||
ErrorOr<int> 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<ThreadState> m_state { ThreadState::Startable };
|
||||
size_t m_stack_size { 0 };
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
|
|
|||
Loading…
Reference in a new issue