Files
thehub/libs/utils/WaitUntilFinishedHelper.h
T

56 lines
1.8 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/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_WAIT_UNTIL_FINISHED_HELPER_H
#define FLOWEE_WAIT_UNTIL_FINISHED_HELPER_H
2019-08-24 13:20:37 +02:00
#include <functional>
2020-03-28 22:49:53 +01:00
#include <boost/asio/io_context_strand.hpp>
2019-08-24 13:20:37 +02:00
#include <mutex>
#include <atomic>
/**
* @brief Use the WaitUntilFinishedHelper class to start a method in a strand and wait until its done.
* Usage is simple, you create an instance and all the work is done in the constructor.
* After the constructor is finished, your method in the strand will have returned.
*/
class WaitUntilFinishedHelper
{
public:
2020-03-28 22:49:53 +01:00
WaitUntilFinishedHelper(const std::function<void()> &target, boost::asio::io_context::strand *strand);
2019-08-24 13:20:37 +02:00
WaitUntilFinishedHelper(const WaitUntilFinishedHelper &other);
~WaitUntilFinishedHelper();
void run();
private:
WaitUntilFinishedHelper *operator=(WaitUntilFinishedHelper&) = delete;
2019-08-24 13:20:37 +02:00
struct Private {
mutable std::mutex mutex;
std::function<void()> target;
std::atomic<int> ref;
2020-03-28 22:49:53 +01:00
boost::asio::io_context::strand *strand;
2019-08-24 13:20:37 +02:00
};
Private *d;
void handle();
};
2024-01-22 19:32:15 +01:00
#endif