Skip to content

Truly independent web browser

LibThreading: Report a thread that cannot start

Andreas Kling016686c028ca
diff --git a/Libraries/LibThreading/Thread.cpp b/Libraries/LibThreading/Thread.cpp
index b2a9d922f0b..78549ad6baf 100644
--- a/Libraries/LibThreading/Thread.cpp
+++ b/Libraries/LibThreading/Thread.cpp
@@ -54,6 +54,11 @@ bool Threading::Thread::has_exited() const
}
void Thread::start()
+{
+ MUST(try_start());
+}
+
+ErrorOr<void> Thread::try_start()
{
VERIFY(!is_started());
@@ -111,7 +116,13 @@ void Thread::start()
pthread_attr_destroy(&attr);
- VERIFY(rc == 0);
+ if (rc != 0) {
+ // NB: The thread would have adopted this reference.
+ unref();
+ m_state = Threading::ThreadState::Startable;
+ return Error::from_errno(rc);
+ }
+ return {};
}
void Thread::detach()
diff --git a/Libraries/LibThreading/Thread.h b/Libraries/LibThreading/Thread.h
index 49526f54f9e..781b6cf56e9 100644
--- a/Libraries/LibThreading/Thread.h
+++ b/Libraries/LibThreading/Thread.h
@@ -65,6 +65,8 @@ public:
// Only callable in the Startable state.
void start();
+ // For a thread that is not essential: the system may be out of threads. The thread stays Startable then.
+ ErrorOr<void> try_start();
// Only callable in the Running state.
void detach();