Instance: add attribute "event_loop"

Replaces global variable "main_loop".
This commit is contained in:
Max Kellermann 2014-02-04 23:58:03 +01:00
parent e8938b1069
commit d380db25be
4 changed files with 22 additions and 18 deletions

View File

@ -34,6 +34,7 @@ class Database;
class UpdateService; class UpdateService;
#endif #endif
class EventLoop;
class Error; class Error;
class ClientList; class ClientList;
struct Partition; struct Partition;
@ -52,6 +53,8 @@ struct Instance final
public NeighborListener public NeighborListener
#endif #endif
{ {
EventLoop *event_loop;
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS
NeighborGlue *neighbors; NeighborGlue *neighbors;
#endif #endif

View File

@ -110,7 +110,6 @@ static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10;
static constexpr Domain main_domain("main"); static constexpr Domain main_domain("main");
ThreadId main_thread; ThreadId main_thread;
EventLoop *main_loop;
Instance *instance; Instance *instance;
@ -197,7 +196,8 @@ glue_db_init_and_load(void)
bool is_simple; bool is_simple;
Error error; Error error;
instance->database = DatabaseGlobalInit(*main_loop, *instance, *param, instance->database = DatabaseGlobalInit(*instance->event_loop,
*instance, *param,
is_simple, error); is_simple, error);
if (instance->database == nullptr) if (instance->database == nullptr)
FatalError(error); FatalError(error);
@ -211,7 +211,8 @@ glue_db_init_and_load(void)
return true; return true;
SimpleDatabase &db = *(SimpleDatabase *)instance->database; SimpleDatabase &db = *(SimpleDatabase *)instance->database;
instance->update = new UpdateService(*main_loop, db, *instance); instance->update = new UpdateService(*instance->event_loop, db,
*instance);
/* run database update after daemonization? */ /* run database update after daemonization? */
return db.FileExists(); return db.FileExists();
@ -247,7 +248,8 @@ glue_state_file_init(Error &error)
return !error.IsDefined(); return !error.IsDefined();
state_file = new StateFile(std::move(path_fs), state_file = new StateFile(std::move(path_fs),
*instance->partition, *main_loop); *instance->partition,
*instance->event_loop);
state_file->Read(); state_file->Read();
return true; return true;
} }
@ -353,7 +355,7 @@ idle_event_emitted(void)
static void static void
shutdown_event_emitted(void) shutdown_event_emitted(void)
{ {
main_loop->Break(); instance->event_loop->Break();
} }
#endif #endif
@ -411,9 +413,9 @@ int mpd_main(int argc, char *argv[])
} }
main_thread = ThreadId::GetCurrent(); main_thread = ThreadId::GetCurrent();
main_loop = new EventLoop();
instance = new Instance(); instance = new Instance();
instance->event_loop = new EventLoop();
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS
instance->neighbors = new NeighborGlue(); instance->neighbors = new NeighborGlue();
@ -431,7 +433,7 @@ int mpd_main(int argc, char *argv[])
const unsigned max_clients = config_get_positive(CONF_MAX_CONN, 10); const unsigned max_clients = config_get_positive(CONF_MAX_CONN, 10);
instance->client_list = new ClientList(max_clients); instance->client_list = new ClientList(max_clients);
if (!listen_global_init(*main_loop, error)) { if (!listen_global_init(*instance->event_loop, error)) {
LogError(error); LogError(error);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -439,7 +441,7 @@ int mpd_main(int argc, char *argv[])
daemonize_set_user(); daemonize_set_user();
daemonize_begin(options.daemon); daemonize_begin(options.daemon);
GlobalEvents::Initialize(*main_loop); GlobalEvents::Initialize(*instance->event_loop);
GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted); GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted);
#ifdef WIN32 #ifdef WIN32
GlobalEvents::Register(GlobalEvents::SHUTDOWN, shutdown_event_emitted); GlobalEvents::Register(GlobalEvents::SHUTDOWN, shutdown_event_emitted);
@ -476,7 +478,7 @@ int mpd_main(int argc, char *argv[])
initialize_decoder_and_player(); initialize_decoder_and_player();
volume_init(); volume_init();
initAudioConfig(); initAudioConfig();
instance->partition->outputs.Configure(*main_loop, instance->partition->outputs.Configure(*instance->event_loop,
instance->partition->pc); instance->partition->pc);
client_manager_init(); client_manager_init();
replay_gain_global_init(); replay_gain_global_init();
@ -492,7 +494,7 @@ int mpd_main(int argc, char *argv[])
setup_log_output(options.log_stderr); setup_log_output(options.log_stderr);
SignalHandlersInit(*main_loop); SignalHandlersInit(*instance->event_loop);
io_thread_start(); io_thread_start();
@ -502,7 +504,7 @@ int mpd_main(int argc, char *argv[])
FatalError(error); FatalError(error);
#endif #endif
ZeroconfInit(*main_loop); ZeroconfInit(*instance->event_loop);
player_create(instance->partition->pc); player_create(instance->partition->pc);
@ -527,7 +529,7 @@ int mpd_main(int argc, char *argv[])
#ifdef ENABLE_INOTIFY #ifdef ENABLE_INOTIFY
if (mapper_has_music_directory() && if (mapper_has_music_directory() &&
instance->update != nullptr) instance->update != nullptr)
mpd_inotify_init(*main_loop, *instance->update, mpd_inotify_init(*instance->event_loop, *instance->update,
config_get_unsigned(CONF_AUTO_UPDATE_DEPTH, config_get_unsigned(CONF_AUTO_UPDATE_DEPTH,
G_MAXUINT)); G_MAXUINT));
#else #else
@ -551,7 +553,7 @@ int mpd_main(int argc, char *argv[])
SetThreadTimerSlackMS(100); SetThreadTimerSlackMS(100);
/* run the main loop */ /* run the main loop */
main_loop->Run(); instance->event_loop->Run();
#ifdef WIN32 #ifdef WIN32
win32_app_stopping(); win32_app_stopping();
@ -608,8 +610,8 @@ int mpd_main(int argc, char *argv[])
config_global_finish(); config_global_finish();
io_thread_deinit(); io_thread_deinit();
SignalHandlersFinish(); SignalHandlersFinish();
delete instance->event_loop;
delete instance; delete instance;
delete main_loop;
daemonize_finish(); daemonize_finish();
#ifdef WIN32 #ifdef WIN32
WSACleanup(); WSACleanup();

View File

@ -26,8 +26,6 @@ struct Instance;
extern ThreadId main_thread; extern ThreadId main_thread;
extern EventLoop *main_loop;
extern Instance *instance; extern Instance *instance;
/** /**

View File

@ -19,7 +19,8 @@
#include "config.h" #include "config.h"
#include "ClientInternal.hxx" #include "ClientInternal.hxx"
#include "Main.hxx" #include "Partition.hxx"
#include "Instance.hxx"
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "util/CharUtil.hxx" #include "util/CharUtil.hxx"
@ -53,7 +54,7 @@ Client::OnSocketInput(void *data, size_t length)
case CommandResult::KILL: case CommandResult::KILL:
Close(); Close();
main_loop->Break(); partition.instance.event_loop->Break();
return InputResult::CLOSED; return InputResult::CLOSED;
case CommandResult::FINISH: case CommandResult::FINISH: