From 5e081de14ab2c5c693d5bf8202f21b2ca77416e2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 10 Feb 2017 21:46:07 +0100 Subject: [PATCH] IOThread: move EventThread instance into struct Instance Eliminate global variables. --- Makefile.am | 1 - src/IOThread.cxx | 8 -------- src/IOThread.hxx | 4 ---- src/Instance.hxx | 3 +++ src/Main.cxx | 19 +++++++++---------- src/command/OtherCommands.cxx | 3 ++- src/command/StorageCommands.cxx | 9 +++++---- src/command/StorageCommands.hxx | 2 +- 8 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Makefile.am b/Makefile.am index 34b23bd4a..b9750af03 100644 --- a/Makefile.am +++ b/Makefile.am @@ -129,7 +129,6 @@ libmpd_a_SOURCES = \ src/Log.cxx src/Log.hxx src/LogV.hxx \ src/LogLevel.hxx \ src/ls.cxx src/ls.hxx \ - src/IOThread.cxx src/IOThread.hxx \ src/Instance.cxx src/Instance.hxx \ src/win32/Win32Main.cxx \ src/MixRampInfo.hxx \ diff --git a/src/IOThread.cxx b/src/IOThread.cxx index 6bad2ef3c..c65227719 100644 --- a/src/IOThread.cxx +++ b/src/IOThread.cxx @@ -51,11 +51,3 @@ io_thread_deinit(void) delete io_thread; io_thread = nullptr; } - -EventLoop & -io_thread_get() -{ - assert(io_thread != nullptr); - - return io_thread->GetEventLoop(); -} diff --git a/src/IOThread.hxx b/src/IOThread.hxx index 45ec88167..438221aca 100644 --- a/src/IOThread.hxx +++ b/src/IOThread.hxx @@ -33,8 +33,4 @@ io_thread_start(); void io_thread_deinit(); -gcc_const -EventLoop & -io_thread_get(); - #endif diff --git a/src/Instance.hxx b/src/Instance.hxx index babc0b3e6..5e1d40024 100644 --- a/src/Instance.hxx +++ b/src/Instance.hxx @@ -22,6 +22,7 @@ #include "check.h" #include "event/Loop.hxx" +#include "event/Thread.hxx" #include "event/MaskMonitor.hxx" #include "Compiler.h" @@ -64,6 +65,8 @@ struct Instance final public NeighborListener #endif { + EventThread io_thread; + MaskMonitor idle_monitor; #ifdef ENABLE_NEIGHBOR_PLUGINS diff --git a/src/Main.cxx b/src/Main.cxx index 3eec3b7cd..d833ba5e3 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -39,7 +39,6 @@ #include "LogInit.hxx" #include "input/Init.hxx" #include "event/Loop.hxx" -#include "IOThread.hxx" #include "fs/AllocatedPath.hxx" #include "fs/Config.hxx" #include "playlist/PlaylistRegistry.hxx" @@ -161,9 +160,9 @@ glue_mapper_init() #ifdef ENABLE_DATABASE static void -InitStorage() +InitStorage(EventLoop &event_loop) { - Storage *storage = CreateConfiguredStorage(io_thread_get()); + Storage *storage = CreateConfiguredStorage(event_loop); if (storage == nullptr) return; @@ -186,7 +185,7 @@ glue_db_init_and_load(void) return true; if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) { - InitStorage(); + InitStorage(instance->io_thread.GetEventLoop()); if (instance->storage == nullptr) { delete instance->database; @@ -421,7 +420,6 @@ try { IcuInit(); winsock_init(); - io_thread_init(); config_global_init(); #ifdef ANDROID @@ -453,7 +451,8 @@ try { #ifdef ENABLE_NEIGHBOR_PLUGINS instance->neighbors = new NeighborGlue(); - instance->neighbors->Init(io_thread_get(), *instance); + instance->neighbors->Init(instance->io_thread.GetEventLoop(), + *instance); if (instance->neighbors->IsEmpty()) { delete instance->neighbors; @@ -519,13 +518,13 @@ try { command_init(); - instance->partition->outputs.Configure(io_thread_get(), + instance->partition->outputs.Configure(instance->io_thread.GetEventLoop(), config.replay_gain, instance->partition->pc); instance->partition->UpdateEffectiveReplayGainMode(); client_manager_init(); - input_stream_global_init(io_thread_get()); + input_stream_global_init(instance->io_thread.GetEventLoop()); playlist_list_global_init(); #ifdef ENABLE_DAEMON @@ -538,7 +537,7 @@ try { SignalHandlersInit(instance->event_loop); #endif - io_thread_start(); + instance->io_thread.Start(); #ifdef ENABLE_NEIGHBOR_PLUGINS if (instance->neighbors != nullptr) @@ -660,7 +659,7 @@ try { archive_plugin_deinit_all(); #endif config_global_finish(); - io_thread_deinit(); + instance->io_thread.Stop(); #ifndef ANDROID SignalHandlersFinish(); #endif diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index 013d4dff2..001badcb1 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -117,7 +117,8 @@ handle_listfiles(Client &client, Request args, Response &r) case LocatedUri::Type::ABSOLUTE: #ifdef ENABLE_DATABASE /* use storage plugin to list remote directory */ - return handle_listfiles_storage(r, located_uri.canonical_uri); + return handle_listfiles_storage(client, r, + located_uri.canonical_uri); #else r.Error(ACK_ERROR_NO_EXIST, "No database"); return CommandResult::ERROR; diff --git a/src/command/StorageCommands.cxx b/src/command/StorageCommands.cxx index 7b5445e23..c77011cc0 100644 --- a/src/command/StorageCommands.cxx +++ b/src/command/StorageCommands.cxx @@ -36,7 +36,6 @@ #include "db/plugins/simple/SimpleDatabasePlugin.hxx" #include "db/update/Service.hxx" #include "TimePrint.hxx" -#include "IOThread.hxx" #include "Idle.hxx" #include @@ -107,9 +106,10 @@ handle_listfiles_storage(Response &r, Storage &storage, const char *uri) } CommandResult -handle_listfiles_storage(Response &r, const char *uri) +handle_listfiles_storage(Client &client, Response &r, const char *uri) { - std::unique_ptr storage(CreateStorageURI(io_thread_get(), uri)); + auto &event_loop = client.partition.instance.io_thread.GetEventLoop(); + std::unique_ptr storage(CreateStorageURI(event_loop, uri)); if (storage == nullptr) { r.Error(ACK_ERROR_ARG, "Unrecognized storage URI"); return CommandResult::ERROR; @@ -195,7 +195,8 @@ handle_mount(Client &client, Request args, Response &r) return CommandResult::ERROR; } - Storage *storage = CreateStorageURI(io_thread_get(), remote_uri); + auto &event_loop = client.partition.instance.io_thread.GetEventLoop(); + Storage *storage = CreateStorageURI(event_loop, remote_uri); if (storage == nullptr) { r.Error(ACK_ERROR_ARG, "Unrecognized storage URI"); return CommandResult::ERROR; diff --git a/src/command/StorageCommands.hxx b/src/command/StorageCommands.hxx index 9765c8412..9d2c34264 100644 --- a/src/command/StorageCommands.hxx +++ b/src/command/StorageCommands.hxx @@ -31,7 +31,7 @@ CommandResult handle_listfiles_storage(Response &r, Storage &storage, const char *uri); CommandResult -handle_listfiles_storage(Response &r, const char *uri); +handle_listfiles_storage(Client &client, Response &r, const char *uri); CommandResult handle_listmounts(Client &client, Request request, Response &response);