Use libev to drive daemon event loop. This prevents the hacky periodic

wakeups to check the (now unnecessary) global quit flag.
This commit is contained in:
Casey Langen 2018-01-30 21:44:57 -08:00
parent c8b81162f3
commit b04aa4e529
5 changed files with 85 additions and 16 deletions

View File

@ -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

View File

@ -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")

View File

@ -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 <dvdmuckle@dvdmuckle.xyz>
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

View File

@ -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()

View File

@ -5,6 +5,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <ev++.h>
#include <core/audio/PlaybackService.h>
#include <core/audio/MasterTransport.h>
@ -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<EvMessageQueue, &EvMessageQueue::ReadCallback>(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);
}