Skip to content

Commit

Permalink
libcore: Running a function right after starting event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 1462eb0 commit f4da4e7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 47 deletions.
16 changes: 1 addition & 15 deletions doomsday/libs/core/include/de/core/app.h
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libs/core/include/de/core/eventloop.h
Expand Up @@ -54,7 +54,7 @@ class DE_PUBLIC EventLoop

virtual ~EventLoop();

int exec();
int exec(const std::function<void ()> &postExec = {});

void quit(int exitCode);

Expand Down
13 changes: 7 additions & 6 deletions doomsday/libs/core/include/de/core/textapp.h
Expand Up @@ -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<void()> &postExec = {});

int execLoop();
void stopLoop(int code);
void quit(int code);

Loop &loop();

Expand Down
16 changes: 2 additions & 14 deletions doomsday/libs/core/src/core/app.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion doomsday/libs/core/src/core/config.cpp
Expand Up @@ -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());
}
Expand Down
7 changes: 5 additions & 2 deletions doomsday/libs/core/src/core/eventloop.cpp
Expand Up @@ -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 {
Expand All @@ -49,11 +50,12 @@ EventLoop::EventLoop() : d(new Impl)
EventLoop::~EventLoop()
{}

int EventLoop::exec()
int EventLoop::exec(const std::function<void ()> &postExec)
{
try
{
internal::StackPusher sp(this);
postExec();
for (;;)
{
// Wait until an event is posted.
Expand All @@ -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;
}
}
Expand Down
10 changes: 6 additions & 4 deletions doomsday/libs/core/src/core/textapp.cpp
Expand Up @@ -81,18 +81,20 @@ bool TextApp::notify(QObject *receiver, QEvent *event)
}
*/

int TextApp::execLoop()
int TextApp::exec(const std::function<void()> &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);
Expand Down
10 changes: 6 additions & 4 deletions doomsday/libs/core/src/core/timer.cpp
Expand Up @@ -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
Expand Down

0 comments on commit f4da4e7

Please sign in to comment.