From b04aa4e5295bad9dd35ae133177ce8c1e6e98a2b Mon Sep 17 00:00:00 2001 From: Casey Langen Date: Tue, 30 Jan 2018 21:44:57 -0800 Subject: [PATCH] Use libev to drive daemon event loop. This prevents the hacky periodic wakeups to check the (now unnecessary) global quit flag. --- .circleci/config.yml | 6 +-- CMakeLists.txt | 2 +- musikcube.spec | 4 +- src/musikcubed/CMakeLists.txt | 8 +++- src/musikcubed/main.cpp | 81 +++++++++++++++++++++++++++++++---- 5 files changed, 85 insertions(+), 16 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 62a758ae9..3bd9a2b3e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,7 @@ jobs: steps: - checkout - run: apt-get update - - run: apt-get install -y build-essential clang cmake libboost-thread1.58-dev libboost-system1.58-dev libboost-filesystem1.58-dev libboost-date-time1.58-dev libboost-atomic1.58-dev libboost-chrono1.58-dev libogg-dev libvorbis-dev libflac-dev libfaad-dev libncursesw5-dev libasound2-dev libpulse-dev pulseaudio libmicrohttpd-dev libmp3lame-dev libcurl4-openssl-dev + - run: apt-get install -y build-essential clang cmake libboost-thread1.58-dev libboost-system1.58-dev libboost-filesystem1.58-dev libboost-date-time1.58-dev libboost-atomic1.58-dev libboost-chrono1.58-dev libogg-dev libvorbis-dev libflac-dev libfaad-dev libncursesw5-dev libasound2-dev libpulse-dev pulseaudio libmicrohttpd-dev libmp3lame-dev libcurl4-openssl-dev libev-dev - run: cmake -DGENERATE_DEB=1 -DDEB_ARCHITECTURE=amd64 -DDEB_DISTRO=xenial -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release . - run: make -j2 - run: make package @@ -21,7 +21,7 @@ jobs: steps: - checkout - run: apt-get update - - run: apt-get install -y build-essential clang cmake libboost-thread1.63-dev libboost-system1.63-dev libboost-filesystem1.63-dev libboost-date-time1.63-dev libboost-atomic1.63-dev libboost-chrono1.63-dev libogg-dev libvorbis-dev libflac-dev libfaad-dev libncursesw5-dev libasound2-dev libpulse-dev pulseaudio libmicrohttpd-dev libmp3lame-dev libcurl4-openssl-dev + - run: apt-get install -y build-essential clang cmake libboost-thread1.63-dev libboost-system1.63-dev libboost-filesystem1.63-dev libboost-date-time1.63-dev libboost-atomic1.63-dev libboost-chrono1.63-dev libogg-dev libvorbis-dev libflac-dev libfaad-dev libncursesw5-dev libasound2-dev libpulse-dev pulseaudio libmicrohttpd-dev libmp3lame-dev libcurl4-openssl-dev libev-dev - run: cmake -DGENERATE_DEB=1 -DDEB_ARCHITECTURE=amd64 -DDEB_DISTRO=artful -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release . - run: make -j2 - run: make package @@ -36,7 +36,7 @@ jobs: steps: - checkout - run: apt-get update - - run: apt-get -y install build-essential clang cmake libboost-thread1.62-dev libboost-system1.62-dev libboost-filesystem1.62-dev libboost-date-time1.62-dev libboost-atomic1.62-dev libboost-chrono1.62-dev libogg-dev libvorbis-dev libflac-dev libfaad-dev libncursesw5-dev libasound2-dev libpulse-dev pulseaudio libmicrohttpd-dev libmp3lame-dev libcurl4-openssl-dev + - run: apt-get -y install build-essential clang cmake libboost-thread1.62-dev libboost-system1.62-dev libboost-filesystem1.62-dev libboost-date-time1.62-dev libboost-atomic1.62-dev libboost-chrono1.62-dev libogg-dev libvorbis-dev libflac-dev libfaad-dev libncursesw5-dev libasound2-dev libpulse-dev pulseaudio libmicrohttpd-dev libmp3lame-dev libcurl4-openssl-dev libev-dev - run: cmake -DGENERATE_DEB=1 -DDEB_ARCHITECTURE=amd64 -DDEB_DISTRO=zesty -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release . - run: make -j2 - run: make package diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a8154635..e68eb0ae1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -204,7 +204,7 @@ if (GENERATE_DEB MATCHES "1") set(DEB_MICROHTTPD_VERSION "10") endif() - set(DEPENDENCIES "libboost-thread${DEB_BOOST_VERSION}, libboost-system${DEB_BOOST_VERSION}, libboost-filesystem${DEB_BOOST_VERSION}, libboost-date-time${DEB_BOOST_VERSION}, libboost-atomic${DEB_BOOST_VERSION}, libboost-chrono${DEB_BOOST_VERSION}, libmicrohttpd${DEB_MICROHTTPD_VERSION}, libcurl3, libogg0, libvorbis0a, libvorbisfile3, libncursesw5, libasound2, libpulse0, pulseaudio, libmp3lame0,") + set(DEPENDENCIES "libboost-thread${DEB_BOOST_VERSION}, libboost-system${DEB_BOOST_VERSION}, libboost-filesystem${DEB_BOOST_VERSION}, libboost-date-time${DEB_BOOST_VERSION}, libboost-atomic${DEB_BOOST_VERSION}, libboost-chrono${DEB_BOOST_VERSION}, libmicrohttpd${DEB_MICROHTTPD_VERSION}, libcurl3, libogg0, libvorbis0a, libvorbisfile3, libncursesw5, libasound2, libpulse0, pulseaudio, libmp3lame0, libev4,") if (${FFMPEG_DECODER} MATCHES "true") set(DEPENDENCIES "${DEPENDENCIES} libavcodec-extra, libavutil55, libavformat57, libswresample2") diff --git a/musikcube.spec b/musikcube.spec index 41b3cd6cf..ee2f3111b 100644 --- a/musikcube.spec +++ b/musikcube.spec @@ -8,8 +8,8 @@ Summary: A cross-platform, terminal-based audio engine, library, player and serv Source0: https://github.com/clangen/musikcube/archive/%{version}.tar.gz#/%{name}-%{version}.tar.gz License: BSD-3-Clause Packager: David Muckle -BuildRequires: gcc-c++ cmake boost-devel libogg-devel libvorbis-devel flac-devel faad2-devel ncurses-devel zlib-devel alsa-lib-devel pulseaudio-libs-devel libcurl-devel libmicrohttpd-devel lame-devel make -Requires: boost libogg libvorbis flac ncurses zlib alsa-lib pulseaudio-libs libcurl libmicrohttpd lame +BuildRequires: gcc-c++ cmake boost-devel libogg-devel libvorbis-devel flac-devel faad2-devel ncurses-devel zlib-devel alsa-lib-devel pulseaudio-libs-devel libcurl-devel libmicrohttpd-devel lame-devel libev-devel make +Requires: boost libogg libvorbis flac ncurses zlib alsa-lib pulseaudio-libs libcurl libmicrohttpd lame libev Recommends: faad2 %description diff --git a/src/musikcubed/CMakeLists.txt b/src/musikcubed/CMakeLists.txt index 43d59e7bb..2b2e7bd3b 100644 --- a/src/musikcubed/CMakeLists.txt +++ b/src/musikcubed/CMakeLists.txt @@ -10,5 +10,11 @@ endif() configure_file("musikcubed.in" "musikcubed" @ONLY) add_executable(musikcubed ${DAEMON_SRCS}) -target_link_libraries(musikcubed ${musikcube_LINK_LIBS} musikcore) set_target_properties(musikcubed PROPERTIES LINK_FLAGS "-Wl,-rpath,./") + +if (${LINK_STATICALLY} MATCHES "true") + find_library(EVLIB NAMES libev.a ev) + target_link_libraries(musikcubed ${musikcube_LINK_LIBS} ${EVLIB} musikcore) +else() + target_link_libraries(musikcubed ${musikcube_LINK_LIBS} musikcore ev) +endif() \ No newline at end of file diff --git a/src/musikcubed/main.cpp b/src/musikcubed/main.cpp index 9ff3ee0f4..1c7bbb5d9 100644 --- a/src/musikcubed/main.cpp +++ b/src/musikcubed/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -32,10 +33,77 @@ using namespace musik::core::runtime; #define LOCKFILE "/tmp/musikcubed.lock" #endif -static volatile bool quit = false; +static const short EVENT_DISPATCH = 1; +static const short EVENT_QUIT = 2; -static void sigtermHandler(int signal) { - quit = true; +static void sigtermHandler(ev::sig &signal, int revents); + +class EvMessageQueue: public MessageQueue { + public: + void Post(IMessagePtr message, int64_t delayMs) { + MessageQueue::Post(message, delayMs); + + if (delayMs <= 0) { + write(fd[1], &EVENT_DISPATCH, sizeof(EVENT_DISPATCH)); + } + else { + double delayTs = (double) delayMs / 1000.0; + loop.once< + EvMessageQueue, + &EvMessageQueue::DelayedDispatch + >(-1, ev::TIMER, (ev::tstamp) delayTs, this); + } + } + + void DelayedDispatch(int revents) { + this->Dispatch(); + } + + void ReadCallback(ev::io& watcher, int revents) { + short type; + if (read(this->fd[0], &type, sizeof(type)) == 0) { + std::cerr << "read() failed.\n"; + exit(EXIT_FAILURE); + } + switch (type) { + case EVENT_DISPATCH: this->Dispatch(); break; + case EVENT_QUIT: loop.break_loop(ev::ALL); break; + } + } + + void Run() { + if (pipe(this->fd) != 0) { + std::cerr << "couldn't create pipe\n"; + exit(EXIT_FAILURE); + } + + io.set(loop); + io.set(this->fd[0], ev::READ); + io.set(this); + io.start(); + + sio.set(loop); + sio.set<&sigtermHandler>(); + sio.start(SIGTERM); + + write(fd[1], &EVENT_DISPATCH, sizeof(EVENT_DISPATCH)); + + loop.run(0); + } + + void Quit() { + write(fd[1], &EVENT_QUIT, sizeof(EVENT_QUIT)); + } + + private: + int fd[2]; + ev::dynamic_loop loop; + ev::io io; + ev::sig sio; +} messageQueue; + +static void sigtermHandler(ev::sig &signal, int revents) { + messageQueue.Quit(); } static bool exitIfRunning() { @@ -91,15 +159,12 @@ int main() { srand((unsigned int) time(0)); - std::signal(SIGTERM, sigtermHandler); - std::locale locale = std::locale(); std::locale utf8Locale(locale, new boost::filesystem::detail::utf8_codecvt_facet); boost::filesystem::path::imbue(utf8Locale); debug::init(); - MessageQueue messageQueue; MasterTransport transport; auto library = LibraryFactory::Libraries().at(0); auto prefs = Preferences::ForComponent(prefs::components::Settings); @@ -113,9 +178,7 @@ int main() { library->Indexer()->Schedule(IIndexer::SyncType::All); } - while (!quit) { - messageQueue.WaitAndDispatch(MESSAGE_QUEUE_TIMEOUT_MS); - } + messageQueue.Run(); remove(LOCKFILE); }