Everywhere: Move the thread name parameter for Thread constructors

The name parameter formats very poorly when a lambda is passed to
Thread, so let's instead put it first now that all Threads are named.
This commit is contained in:
Zaggy1024 2026-01-23 15:14:34 -06:00 committed by Gregory Bertilson
parent d0f53ddab6
commit e2635af2ed
13 changed files with 38 additions and 50 deletions

View file

@ -66,7 +66,7 @@ TransportSocket::TransportSocket(NonnullOwnPtr<Core::LocalSocket> socket)
m_notify_hook_write_fd = adopt_ref(*new AutoCloseFileDescriptor(fds[1]));
}
m_io_thread = Threading::Thread::construct([this] { return io_thread_loop(); }, "IPC IO"sv);
m_io_thread = Threading::Thread::construct("IPC IO"sv, [this] { return io_thread_loop(); });
m_io_thread->start();
}

View file

@ -36,7 +36,7 @@ ErrorOr<NonnullRefPtr<PlaybackStream>> PlaybackStreamPulseAudio::create(OutputSt
auto playback_stream = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) PlaybackStreamPulseAudio(internal_state)));
// Create the control thread and start it.
auto thread = TRY(Threading::Thread::try_create([=, sample_specification_selected_callback = move(sample_specification_selected_callback), data_request_callback = move(data_request_callback)]() mutable {
auto thread = TRY(Threading::Thread::try_create("Audio Control"sv, [=, sample_specification_selected_callback = move(sample_specification_selected_callback), data_request_callback = move(data_request_callback)]() mutable {
auto context = TRY_OR_EXIT_THREAD(PulseAudioContext::the());
internal_state->set_stream(TRY_OR_EXIT_THREAD(context->create_stream(initial_state, target_latency_ms, [data_request_callback = move(data_request_callback)](PulseAudioStream&, Span<float> buffer) {
return data_request_callback(buffer);
@ -50,8 +50,7 @@ ErrorOr<NonnullRefPtr<PlaybackStream>> PlaybackStreamPulseAudio::create(OutputSt
internal_state->thread_loop();
return 0;
},
"Audio Control"sv));
}));
thread->start();
thread->detach();

View file

@ -309,10 +309,9 @@ ErrorOr<NonnullRefPtr<PlaybackStream>> PlaybackStreamWASAPI::create(OutputState
if (initial_output_state == OutputState::Playing)
state->playing = true;
auto audio_thread = Threading::Thread::construct([state] {
auto audio_thread = Threading::Thread::construct("Audio Render"sv, [state] {
return AudioState::render_thread_loop(*state);
},
"Audio Render"sv);
});
if (initial_output_state == OutputState::Playing)
TRY_HR(state->audio_client->Start());

View file

@ -134,7 +134,7 @@ PlaybackManager::~PlaybackManager()
void PlaybackManager::add_media_source(NonnullRefPtr<IncrementallyPopulatedStream> stream)
{
auto thread = Threading::Thread::construct([playback_manager = NonnullRefPtr { *this }, stream, main_thread_event_loop_reference = Core::EventLoop::current_weak()] -> int {
auto thread = Threading::Thread::construct("Media Init"sv, [playback_manager = NonnullRefPtr { *this }, stream, main_thread_event_loop_reference = Core::EventLoop::current_weak()] -> int {
auto main_thread_event_loop = main_thread_event_loop_reference->take();
auto maybe_error = playback_manager->prepare_playback_from_media_data(stream, main_thread_event_loop_reference);
if (maybe_error.is_error()) {
@ -145,8 +145,7 @@ void PlaybackManager::add_media_source(NonnullRefPtr<IncrementallyPopulatedStrea
return 0;
}
return 0;
},
"Media Init"sv);
});
thread->start();
thread->detach();

View file

@ -28,7 +28,7 @@ DecoderErrorOr<NonnullRefPtr<AudioDataProvider>> AudioDataProvider::try_create(N
TRY(thread_data->create_decoder());
auto provider = DECODER_TRY_ALLOC(try_make_ref_counted<AudioDataProvider>(thread_data));
auto thread = DECODER_TRY_ALLOC(Threading::Thread::try_create([thread_data]() -> int {
auto thread = DECODER_TRY_ALLOC(Threading::Thread::try_create("Audio Decoder"sv, [thread_data]() -> int {
thread_data->wait_for_start();
while (!thread_data->should_thread_exit()) {
thread_data->handle_suspension();
@ -36,8 +36,7 @@ DecoderErrorOr<NonnullRefPtr<AudioDataProvider>> AudioDataProvider::try_create(N
thread_data->push_data_and_decode_a_block();
}
return 0;
},
"Audio Decoder"sv));
}));
thread->start();
thread->detach();

View file

@ -27,7 +27,7 @@ DecoderErrorOr<NonnullRefPtr<VideoDataProvider>> VideoDataProvider::try_create(N
TRY(thread_data->create_decoder());
auto provider = DECODER_TRY_ALLOC(try_make_ref_counted<VideoDataProvider>(thread_data));
auto thread = DECODER_TRY_ALLOC(Threading::Thread::try_create([thread_data]() -> int {
auto thread = DECODER_TRY_ALLOC(Threading::Thread::try_create("Video Decoder"sv, [thread_data]() -> int {
thread_data->wait_for_start();
while (!thread_data->should_thread_exit()) {
thread_data->handle_seek();
@ -35,8 +35,7 @@ DecoderErrorOr<NonnullRefPtr<VideoDataProvider>> VideoDataProvider::try_create(N
thread_data->push_data_and_decode_some_frames();
}
return 0;
},
"Video Decoder"sv));
}));
thread->start();
thread->detach();

View file

@ -42,7 +42,7 @@ static intptr_t background_thread_func()
static void init()
{
s_all_actions = new Queue<Function<void()>>;
s_background_thread = &Threading::Thread::construct(background_thread_func, "Background"sv).leak_ref();
s_background_thread = &Threading::Thread::construct("Background"sv, background_thread_func).leak_ref();
s_background_thread->start();
}

View file

@ -45,11 +45,11 @@ enum class ThreadState : u8 {
class Thread final
: public AtomicRefCounted<Thread> {
public:
static NonnullRefPtr<Thread> construct(ESCAPING Function<intptr_t()> action, StringView thread_name)
static NonnullRefPtr<Thread> construct(StringView thread_name, ESCAPING Function<intptr_t()> action)
{
return adopt_ref(*new Thread(move(action), thread_name));
}
static ErrorOr<NonnullRefPtr<Thread>> try_create(ESCAPING Function<intptr_t()> action, StringView thread_name)
static ErrorOr<NonnullRefPtr<Thread>> try_create(StringView thread_name, ESCAPING Function<intptr_t()> action)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(action), thread_name));
}

View file

@ -239,11 +239,10 @@ RenderingThread::~RenderingThread()
void RenderingThread::start(DisplayListPlayerType)
{
VERIFY(m_thread_data->has_skia_player());
m_thread = Threading::Thread::construct([thread_data = m_thread_data] {
m_thread = Threading::Thread::construct("Renderer"sv, [thread_data = m_thread_data] {
thread_data->compositor_loop();
return static_cast<intptr_t>(0);
},
"Renderer"sv);
});
m_thread->start();
}

View file

@ -13,7 +13,7 @@
namespace WebView {
MachPortServer::MachPortServer()
: m_thread(Threading::Thread::construct([this]() -> intptr_t { thread_loop(); return 0; }, "MachPortServer"sv))
: m_thread(Threading::Thread::construct("MachPortServer"sv, [this]() -> intptr_t { thread_loop(); return 0; }))
, m_server_port_name(ByteString::formatted("org.ladybird.Ladybird.helper.{}", getpid()))
{
if (auto err = allocate_server_port(); err.is_error())

View file

@ -125,12 +125,11 @@ TEST_CASE(threaded_promise_instantly_resolved)
auto promise = Core::ThreadedPromise<int>::create();
auto thread = Threading::Thread::construct([&, promise] {
auto thread = Threading::Thread::construct("PromiseResolver"sv, [&, promise] {
thread_id = pthread_self();
promise->resolve(42);
return 0;
},
"PromiseResolver"sv);
});
thread->start();
promise
@ -163,14 +162,13 @@ TEST_CASE(threaded_promise_resolved_later)
auto promise = Core::ThreadedPromise<int>::create();
auto thread = Threading::Thread::construct([&, promise] {
auto thread = Threading::Thread::construct("PromiseResolver"sv, [&, promise] {
thread_id = pthread_self();
while (!unblock_thread)
MUST(Core::System::sleep_ms(5));
promise->resolve(42);
return 0;
},
"PromiseResolver"sv);
});
thread->start();
promise

View file

@ -49,7 +49,7 @@ TEST_CASE(simple_multithread)
for (int i = 0; i < test_count; ++i)
(void)queue.enqueue(i);
auto second_thread = Threading::Thread::construct([&queue]() {
auto second_thread = Threading::Thread::construct("QueueConsumer"sv, [&queue]() {
auto copied_queue = queue;
for (int i = 0; i < test_count; ++i) {
QueueError result = TestQueue::QueueStatus::Invalid;
@ -63,8 +63,7 @@ TEST_CASE(simple_multithread)
FAIL("Unexpected error while dequeueing.");
}
return 0;
},
"QueueConsumer"sv);
});
second_thread->start();
(void)second_thread->join();
@ -80,7 +79,7 @@ TEST_CASE(producer_consumer_multithread)
IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> other_thread_running { false };
auto second_thread = Threading::Thread::construct([&queue, &other_thread_running]() {
auto second_thread = Threading::Thread::construct("QueueConsumer"sv, [&queue, &other_thread_running]() {
auto copied_queue = queue;
other_thread_running.store(true);
for (size_t i = 0; i < test_count; ++i) {
@ -95,8 +94,7 @@ TEST_CASE(producer_consumer_multithread)
FAIL("Unexpected error while dequeueing.");
}
return 0;
},
"QueueConsumer"sv);
});
second_thread->start();
while (!other_thread_running.load())
@ -126,10 +124,10 @@ TEST_CASE(multi_consumer)
Atomic<size_t> dequeue_count = 0;
auto threads = {
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
};
for (size_t i = 0; i < test_count; ++i)
@ -153,10 +151,10 @@ TEST_CASE(single_producer_multi_consumer)
Atomic<size_t> dequeue_count = 0;
auto threads = {
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct(dequeuer(queue, dequeue_count, test_count), "Dequeuer"sv),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
Threading::Thread::construct("Dequeuer"sv, dequeuer(queue, dequeue_count, test_count)),
};
for (auto thread : threads)
thread->start();

View file

@ -29,12 +29,11 @@ TEST_CASE(threads_can_detach)
{
IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<int> should_be_42 = 0;
auto thread = Threading::Thread::construct([&should_be_42]() {
auto thread = Threading::Thread::construct("DetachTest"sv, [&should_be_42]() {
(void)Core::System::sleep_ms(10);
should_be_42 = 42;
return 0;
},
"DetachTest"sv);
});
thread->start();
thread->detach();
@ -45,12 +44,11 @@ TEST_CASE(threads_can_detach)
TEST_CASE(detached_threads_do_not_need_to_be_joined)
{
IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> should_exit { false };
auto thread = Threading::Thread::construct([&]() {
auto thread = Threading::Thread::construct("DetachTest"sv, [&]() {
while (!should_exit.load())
(void)Core::System::sleep_ms(10);
return 0;
},
"DetachTest"sv);
});
thread->start();
thread->detach();
@ -64,7 +62,7 @@ TEST_CASE(detached_threads_do_not_need_to_be_joined)
TEST_CASE(join_dead_thread)
{
auto thread = Threading::Thread::construct([&]() { return 0 /*nullptr*/; }, "JoinTest"sv);
auto thread = Threading::Thread::construct("JoinTest"sv, [&]() { return 0 /*nullptr*/; });
thread->start();
// The thread should have exited by then.