mirror of
https://github.com/feather-wallet/feather.git
synced 2024-12-23 03:59:29 +00:00
92 lines
2.1 KiB
C++
92 lines
2.1 KiB
C++
// SPDX-License-Identifier: BSD-3-Clause
|
|
// SPDX-FileCopyrightText: 2014-2022 The Monero Project
|
|
|
|
#include "scheduler.h"
|
|
|
|
FutureScheduler::FutureScheduler(QObject *parent)
|
|
: QObject(parent), Alive(0), Stopping(false)
|
|
{
|
|
}
|
|
|
|
FutureScheduler::~FutureScheduler()
|
|
{
|
|
shutdownWaitForFinished();
|
|
}
|
|
|
|
void FutureScheduler::shutdownWaitForFinished() noexcept
|
|
{
|
|
QMutexLocker locker(&Mutex);
|
|
|
|
Stopping = true;
|
|
while (Alive > 0)
|
|
{
|
|
Condition.wait(&Mutex);
|
|
}
|
|
}
|
|
|
|
QPair<bool, QFuture<void>> FutureScheduler::run(std::function<void()> function) noexcept
|
|
{
|
|
return execute<void>([this, function](QFutureWatcher<void> *) {
|
|
return QtConcurrent::run([this, function] {
|
|
try
|
|
{
|
|
function();
|
|
}
|
|
catch (const std::exception &exception)
|
|
{
|
|
qWarning() << "Exception thrown from async function: " << exception.what();
|
|
}
|
|
done();
|
|
});
|
|
});
|
|
}
|
|
|
|
QPair<bool, QFuture<QVariantMap>> FutureScheduler::run(const std::function<QVariantMap()> &function, const std::function<void (QVariantMap)> &callback) noexcept
|
|
{
|
|
return execute<QVariantMap>([this, function, callback](QFutureWatcher<QVariantMap> *watcher) {
|
|
connect(watcher, &QFutureWatcher<QVariantMap>::finished, [watcher, callback] {
|
|
callback(watcher->future().result());
|
|
});
|
|
return QtConcurrent::run([this, function] {
|
|
QVariantMap result;
|
|
try
|
|
{
|
|
result = function();
|
|
}
|
|
catch (const std::exception &exception)
|
|
{
|
|
qWarning() << "Exception thrown from async function: " << exception.what();
|
|
}
|
|
done();
|
|
return result;
|
|
});
|
|
});
|
|
}
|
|
|
|
bool FutureScheduler::stopping() const noexcept
|
|
{
|
|
return Stopping;
|
|
}
|
|
|
|
bool FutureScheduler::add() noexcept
|
|
{
|
|
QMutexLocker locker(&Mutex);
|
|
|
|
if (Stopping)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
++Alive;
|
|
return true;
|
|
}
|
|
|
|
void FutureScheduler::done() noexcept
|
|
{
|
|
{
|
|
QMutexLocker locker(&Mutex);
|
|
--Alive;
|
|
}
|
|
|
|
Condition.wakeAll();
|
|
}
|