Files
thehub/libs/utils/WaitUntilFinishedHelper.cpp
T

56 lines
1.6 KiB
C++
Raw Permalink Normal View History

2019-08-24 13:20:37 +02:00
/*
* This file is part of the Flowee project
2021-06-20 22:44:44 +02:00
* Copyright (C) 2017-2018 Tom Zander <tom@flowee.org>
2019-08-24 13:20:37 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WaitUntilFinishedHelper.h"
#include "Logger.h"
2020-03-28 22:49:53 +01:00
WaitUntilFinishedHelper::WaitUntilFinishedHelper(const std::function<void ()> &target, boost::asio::io_context::strand *strand)
2019-08-24 13:20:37 +02:00
: d(new Private())
{
d->ref = 1;
d->target = target;
d->strand = strand;
}
WaitUntilFinishedHelper::WaitUntilFinishedHelper(const WaitUntilFinishedHelper &other)
: d(other.d) {
d->ref++;
}
WaitUntilFinishedHelper::~WaitUntilFinishedHelper() {
if (!--d->ref)
delete d;
}
void WaitUntilFinishedHelper::run()
{
d->mutex.lock();
2025-02-11 16:46:21 +01:00
d->strand->dispatch(std::bind(&WaitUntilFinishedHelper::handle, this), std::allocator<void>());
2019-08-24 13:20:37 +02:00
d->mutex.lock();
}
void WaitUntilFinishedHelper::handle() {
try {
d->target();
} catch (const std::exception &e) {
2025-01-13 23:37:51 +01:00
logFatal(Log::Flowee) << "Unhandled exception caught by WaitUntilFinishedHelper" << e;
2019-08-24 13:20:37 +02:00
}
d->mutex.unlock();
}