From 838398103c2b477f7fa0195342f7956ec0899f35 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Thu, 30 Jan 2025 19:50:35 +0100
Subject: [PATCH] Revert "event/Thread: start the thread in the constructor"

This reverts commit b49cfe96f406bf0b750b7a1ea73609905d606335.  It was
a bad idea because it broke signal handlers.  I need to find a better
way to fix io_uring intialization.
---
 src/Main.cxx            | 3 +++
 src/event/Thread.hxx    | 6 ++----
 test/DumpDatabase.cxx   | 6 ++++++
 test/DumpOgg.cxx        | 1 +
 test/RunChromaprint.cxx | 2 ++
 test/dump_playlist.cxx  | 1 +
 test/dump_text_file.cxx | 1 +
 test/read_tags.cxx      | 1 +
 test/run_decoder.cxx    | 1 +
 test/run_input.cxx      | 1 +
 test/run_output.cxx     | 1 +
 test/run_storage.cxx    | 1 +
 test/visit_archive.cxx  | 1 +
 13 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/Main.cxx b/src/Main.cxx
index d37a6e3e2..71d9a2b4a 100644
--- a/src/Main.cxx
+++ b/src/Main.cxx
@@ -329,6 +329,9 @@ MainConfigured(const CommandLineOptions &options,
 	Instance instance;
 	global_instance = &instance;
 
+	instance.io_thread.Start();
+	instance.rtio_thread.Start();
+
 #ifdef ENABLE_NEIGHBOR_PLUGINS
 	instance.neighbors = std::make_unique<NeighborGlue>();
 	instance.neighbors->Init(raw_config,
diff --git a/src/event/Thread.hxx b/src/event/Thread.hxx
index 6ea71d112..a13acb54b 100644
--- a/src/event/Thread.hxx
+++ b/src/event/Thread.hxx
@@ -20,9 +20,7 @@ class EventThread final {
 public:
 	explicit EventThread(bool _realtime=false)
 		:event_loop(ThreadId::Null()), thread(BIND_THIS_METHOD(Run)),
-		 realtime(_realtime) {
-		Start();
-	}
+		 realtime(_realtime) {}
 
 	~EventThread() noexcept {
 		Stop();
@@ -32,11 +30,11 @@ public:
 		return event_loop;
 	}
 
-private:
 	void Start();
 
 	void Stop() noexcept;
 
+private:
 	void Run() noexcept;
 };
 
diff --git a/test/DumpDatabase.cxx b/test/DumpDatabase.cxx
index cff3079ca..ec9e57a04 100644
--- a/test/DumpDatabase.cxx
+++ b/test/DumpDatabase.cxx
@@ -28,6 +28,12 @@ class GlobalInit {
 	EventThread io_thread;
 
 public:
+	GlobalInit() {
+		io_thread.Start();
+	}
+
+	~GlobalInit() = default;
+
 	EventLoop &GetEventLoop() {
 		return io_thread.GetEventLoop();
 	}
diff --git a/test/DumpOgg.cxx b/test/DumpOgg.cxx
index d5b982342..12679c6bc 100644
--- a/test/DumpOgg.cxx
+++ b/test/DumpOgg.cxx
@@ -24,6 +24,7 @@ try {
 	const char *path = argv[1];
 
 	EventThread io_thread;
+	io_thread.Start();
 
 	const ScopeInputPluginsInit input_plugins_init(ConfigData(),
 						       io_thread.GetEventLoop());
diff --git a/test/RunChromaprint.cxx b/test/RunChromaprint.cxx
index e17dc534e..172ed24ea 100644
--- a/test/RunChromaprint.cxx
+++ b/test/RunChromaprint.cxx
@@ -84,6 +84,8 @@ public:
 		 input_plugins_init(config, io_thread.GetEventLoop()),
 		 decoder_plugins_init(config)
 	{
+		io_thread.Start();
+
 		pcm_convert_global_init(config);
 	}
 };
diff --git a/test/dump_playlist.cxx b/test/dump_playlist.cxx
index d03d2f0d8..883ac2878 100644
--- a/test/dump_playlist.cxx
+++ b/test/dump_playlist.cxx
@@ -47,6 +47,7 @@ try {
 	const auto config = AutoLoadConfigFile(config_path);
 
 	EventThread io_thread;
+	io_thread.Start();
 
 	const ScopeInputPluginsInit input_plugins_init(config, io_thread.GetEventLoop());
 	const ScopePlaylistPluginsInit playlist_plugins_init(config);
diff --git a/test/dump_text_file.cxx b/test/dump_text_file.cxx
index 6f3cec9b7..e2b9aad02 100644
--- a/test/dump_text_file.cxx
+++ b/test/dump_text_file.cxx
@@ -34,6 +34,7 @@ public:
 	explicit GlobalInit(Path config_path)
 		:config(AutoLoadConfigFile(config_path))
 	{
+		io_thread.Start();
 	}
 };
 
diff --git a/test/read_tags.cxx b/test/read_tags.cxx
index 0734d21cb..35b52a5c2 100644
--- a/test/read_tags.cxx
+++ b/test/read_tags.cxx
@@ -86,6 +86,7 @@ try {
 	const char *path = argv[2];
 
 	EventThread io_thread;
+	io_thread.Start();
 
 	const ScopeInputPluginsInit input_plugins_init(ConfigData(),
 						       io_thread.GetEventLoop());
diff --git a/test/run_decoder.cxx b/test/run_decoder.cxx
index 83a0cb3ff..64d57bd7b 100644
--- a/test/run_decoder.cxx
+++ b/test/run_decoder.cxx
@@ -91,6 +91,7 @@ public:
 		 input_plugins_init(config, io_thread.GetEventLoop()),
 		 decoder_plugins_init(config)
 	{
+		io_thread.Start();
 	}
 };
 
diff --git a/test/run_input.cxx b/test/run_input.cxx
index 3a924bd79..caa20d7a8 100644
--- a/test/run_input.cxx
+++ b/test/run_input.cxx
@@ -130,6 +130,7 @@ public:
 	explicit GlobalInit(Path config_path)
 		:config(AutoLoadConfigFile(config_path))
 	{
+		io_thread.Start();
 	}
 };
 
diff --git a/test/run_output.cxx b/test/run_output.cxx
index b45b248c6..d8e2fb605 100644
--- a/test/run_output.cxx
+++ b/test/run_output.cxx
@@ -158,6 +158,7 @@ try {
 	const auto config = AutoLoadConfigFile(c.config_path);
 
 	EventThread io_thread;
+	io_thread.Start();
 
 	/* initialize the audio output */
 
diff --git a/test/run_storage.cxx b/test/run_storage.cxx
index b6ebef32a..4ef2c8179 100644
--- a/test/run_storage.cxx
+++ b/test/run_storage.cxx
@@ -114,6 +114,7 @@ public:
 	GlobalInit(Path config_path)
 		:config(AutoLoadConfigFile(config_path))
 	{
+		io_thread.Start();
 	}
 
 	EventLoop &GetEventLoop() noexcept {
diff --git a/test/visit_archive.cxx b/test/visit_archive.cxx
index 96e57f44f..e4167a882 100644
--- a/test/visit_archive.cxx
+++ b/test/visit_archive.cxx
@@ -35,6 +35,7 @@ public:
 	explicit GlobalInit(Path config_path)
 		:config(AutoLoadConfigFile(config_path))
 	{
+		io_thread.Start();
 	}
 };