From f4da4e700dd072198aa98250fcb358c7beefcacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sat, 23 Jun 2018 13:25:38 +0300 Subject: [PATCH] libcore: Running a function right after starting event loop --- doomsday/libs/core/include/de/core/app.h | 16 +--------------- doomsday/libs/core/include/de/core/eventloop.h | 2 +- doomsday/libs/core/include/de/core/textapp.h | 13 +++++++------ doomsday/libs/core/src/core/app.cpp | 16 ++-------------- doomsday/libs/core/src/core/config.cpp | 4 +++- doomsday/libs/core/src/core/eventloop.cpp | 7 +++++-- doomsday/libs/core/src/core/textapp.cpp | 10 ++++++---- doomsday/libs/core/src/core/timer.cpp | 10 ++++++---- 8 files changed, 31 insertions(+), 47 deletions(-) diff --git a/doomsday/libs/core/include/de/core/app.h b/doomsday/libs/core/include/de/core/app.h index 6a586029f0..c5e78fe36e 100644 --- a/doomsday/libs/core/include/de/core/app.h +++ b/doomsday/libs/core/include/de/core/app.h @@ -379,20 +379,6 @@ class DE_PUBLIC App : DE_OBSERVES(Clock, TimeChange) */ static UnixInfo &unixInfo(); - /** - * Starts the application's main loop. - * - * @return Return code after the loop exits. - */ - virtual int execLoop() = 0; - - /** - * Stops the application's main loop. - * - * @param code Return code from the loop. - */ - virtual void stopLoop(int code) = 0; - /** * Requests engine shutdown by calling the specified termination callback * (see setTerminateFunc()). Called when an exception is caught at the @@ -405,7 +391,7 @@ class DE_PUBLIC App : DE_OBSERVES(Clock, TimeChange) * * @param message Error message to be shown to the user. */ - void handleUncaughtException(String message); + void handleUncaughtException(const String& message); /** * Events received from the operating system should be passed here; the diff --git a/doomsday/libs/core/include/de/core/eventloop.h b/doomsday/libs/core/include/de/core/eventloop.h index aef1c211b8..cb8e4e1760 100644 --- a/doomsday/libs/core/include/de/core/eventloop.h +++ b/doomsday/libs/core/include/de/core/eventloop.h @@ -54,7 +54,7 @@ class DE_PUBLIC EventLoop virtual ~EventLoop(); - int exec(); + int exec(const std::function &postExec = {}); void quit(int exitCode); diff --git a/doomsday/libs/core/include/de/core/textapp.h b/doomsday/libs/core/include/de/core/textapp.h index 9bb5837d37..762e77745b 100644 --- a/doomsday/libs/core/include/de/core/textapp.h +++ b/doomsday/libs/core/include/de/core/textapp.h @@ -42,13 +42,14 @@ class DE_PUBLIC TextApp : public App public: TextApp(const StringList &args); -/* void setMetadata(String const &orgName, String const &orgDomain, - String const &appName, String const &appVersion); -*/ -// bool notify(QObject *receiver, QEvent *event); + /** + * Start the application event loop. + * + * @param postExec Function to call immediately after starting the event loop. + */ + int exec(const std::function &postExec = {}); - int execLoop(); - void stopLoop(int code); + void quit(int code); Loop &loop(); diff --git a/doomsday/libs/core/src/core/app.cpp b/doomsday/libs/core/src/core/app.cpp index 7c1611d47f..ed9c514266 100644 --- a/doomsday/libs/core/src/core/app.cpp +++ b/doomsday/libs/core/src/core/app.cpp @@ -486,18 +486,6 @@ void App::addInitPackage(String const &identifier) d->packagesToLoadAtInit << identifier; } -/*void App::setConfigScript(Path const &path) -{ - d->configPath = path; -}*/ - -/* -void App::setName(String const &appName) -{ - d->appName = appName; -} -*/ - void App::setUnixHomeFolderName(String const &name) { d->metadata.set(UNIX_HOME, name); @@ -526,7 +514,7 @@ void App::setTerminateFunc(void (*func)(char const *)) d->terminateFunc = func; } -void App::handleUncaughtException(String message) +void App::handleUncaughtException(const String& message) { LOG_CRITICAL(message); @@ -672,7 +660,7 @@ Archive &App::mutablePersistentData() bool App::hasPersistentData() { - return DE_APP->d->persistentData != 0; + return DE_APP->d->persistentData != nullptr; } ArchiveFolder &App::persistPackFolder() diff --git a/doomsday/libs/core/src/core/config.cpp b/doomsday/libs/core/src/core/config.cpp index 3a7877047b..c7e1765913 100644 --- a/doomsday/libs/core/src/core/config.cpp +++ b/doomsday/libs/core/src/core/config.cpp @@ -74,7 +74,9 @@ DE_PIMPL_NOREF(Config) } }; -Config::Config(Path const &path) : RecordAccessor(0), d(new Impl(path)) +Config::Config(Path const &path) + : RecordAccessor(nullptr) + , d(new Impl(path)) { setAccessedRecord(objectNamespace()); } diff --git a/doomsday/libs/core/src/core/eventloop.cpp b/doomsday/libs/core/src/core/eventloop.cpp index 52338fffce..d114e253a6 100644 --- a/doomsday/libs/core/src/core/eventloop.cpp +++ b/doomsday/libs/core/src/core/eventloop.cpp @@ -19,10 +19,11 @@ #include "de/EventLoop" #include "de/CoreEvent" +#include "de/Garbage" +#include "de/Log" #include "de/NumberValue" #include "de/ThreadLocal" #include "de/WaitableFIFO" -#include "de/Garbage" namespace de { namespace internal { @@ -49,11 +50,12 @@ EventLoop::EventLoop() : d(new Impl) EventLoop::~EventLoop() {} -int EventLoop::exec() +int EventLoop::exec(const std::function &postExec) { try { internal::StackPusher sp(this); + postExec(); for (;;) { // Wait until an event is posted. @@ -77,6 +79,7 @@ int EventLoop::exec() { warning("[EventLoop] Event loop terminating due to an uncaught exception"); er.warnPlainText(); + LOG_WARNING("Event loop stopped: %s") << er.asText(); return 0; } } diff --git a/doomsday/libs/core/src/core/textapp.cpp b/doomsday/libs/core/src/core/textapp.cpp index a16e7043d3..80cd9949cd 100644 --- a/doomsday/libs/core/src/core/textapp.cpp +++ b/doomsday/libs/core/src/core/textapp.cpp @@ -81,18 +81,20 @@ bool TextApp::notify(QObject *receiver, QEvent *event) } */ -int TextApp::execLoop() +int TextApp::exec(const std::function &postExec) { LOGDEV_NOTE("Starting TextApp event loop..."); - d->loop.start(); - int code = d->eventLoop.exec(); + int code = d->eventLoop.exec([this, postExec]() { + d->loop.start(); + postExec(); + }); LOGDEV_NOTE("TextApp event loop exited with code %i") << code; return code; } -void TextApp::stopLoop(int code) +void TextApp::quit(int code) { d->loop.stop(); d->eventLoop.quit(code); diff --git a/doomsday/libs/core/src/core/timer.cpp b/doomsday/libs/core/src/core/timer.cpp index a91af14719..98d76574b8 100644 --- a/doomsday/libs/core/src/core/timer.cpp +++ b/doomsday/libs/core/src/core/timer.cpp @@ -86,10 +86,12 @@ struct TimerScheduler : public Thread, public Lockable // Schedule the next trigger. if (pt.repeatDuration > 0.0) { - pending.push( - Pending{pt.nextAt + sc::microseconds(dint64(pt.repeatDuration * 1.0e6)), - pt.timer, - pt.repeatDuration}); + TimePoint nextAt = pt.nextAt + sc::microseconds(dint64(pt.repeatDuration * 1.0e6)); +#if defined (DE_DEBUG) + // Debugger may halt the process, don't bother spamming with timer events. + nextAt = de::max(nextAt, sc::system_clock::now()); +#endif + pending.push(Pending{nextAt, pt.timer, pt.repeatDuration}); } } else