Clean StopableJob and use inheritance instead of set lambda

This commit is contained in:
Filip Sykala 2021-11-24 14:04:00 +01:00
parent 86a60863bf
commit 456952325f
3 changed files with 129 additions and 83 deletions

View File

@ -14,18 +14,12 @@ using namespace Slic3r;
using namespace GUI; using namespace GUI;
namespace Priv { namespace Priv {
static void process(std::unique_ptr<EmbossData> input, StopCondition is_stop);
static void finalize(const EmbossData &input, const indexed_triangle_set &result); static void finalize(const EmbossData &input, const indexed_triangle_set &result);
// TODO: May be move to objec list utils
// TODO: move to objec list utils
static void select_volume(ModelVolume *volume); static void select_volume(ModelVolume *volume);
} // namespace Priv } // namespace Priv
EmbossJob::EmbossJob() : StopableJob<EmbossData>(Priv::process) {} void EmbossJob::process(std::unique_ptr<EmbossData> input, StopCondition is_stop)
void Priv::process(std::unique_ptr<EmbossData> input, StopCondition is_stop)
{ {
// Changing cursor to busy // Changing cursor to busy
wxBeginBusyCursor(); wxBeginBusyCursor();

View File

@ -26,8 +26,8 @@ struct EmbossData
class EmbossJob : public StopableJob<EmbossData> class EmbossJob : public StopableJob<EmbossData>
{ {
public: protected:
EmbossJob(); void process(std::unique_ptr<EmbossData> input, StopCondition is_stop) override;
}; };
} // namespace Slic3r::GUI } // namespace Slic3r::GUI

View File

@ -22,23 +22,69 @@ template<typename TIn> class StopableJob
bool m_running = false; bool m_running = false;
// faster interupt inside func, developer must add StopCondifion call // faster interupt inside func, developer must add StopCondifion call
bool m_stop = false; bool m_stop = false;
using Func = std::function<void(std::unique_ptr<TIn>, StopCondition)>;
Func m_func;
public: public:
StopableJob(Func func) : m_func(func) {} /// <summary>
virtual ~StopableJob() { /// Stop and join thread
/// </summary>
virtual ~StopableJob();
/// <summary>
/// Restart processing of input
/// </summary>
/// <param name="input">Data needed to process</param>
void run(std::unique_ptr<TIn> input);
/// <summary>
/// Check if job processing input now
/// </summary>
/// <returns>True when run now otherwise False</returns>
bool is_running(); // const; -- mutex
/// <summary>
/// Check if actual job is stopping now
/// </summary>
/// <returns>True when at stop process otherwise False</returns>
bool is_stoping(); // const; -- mutex
/// <summary>
/// set flag to stop processing
/// </summary>
void stop();
/// <summary>
/// Free thread resources by join thread
/// Be Carefull, it is blocking until join
/// Suggest to call stop() before join
/// </summary>
void join();
protected:
/// <summary>
/// Thread job of processing input data
/// </summary>
/// <param name="input">input data to process</param>
/// <param name="stop_condition">When lambda is true, quit processing,
/// keep in mind check is under mutex so do it occasionally</param>
virtual void process(std::unique_ptr<TIn> input, StopCondition stop_condition) = 0;
};
//////
// Implementation
//////
template<typename TIn>
StopableJob<TIn>::~StopableJob()
{
stop(); stop();
try { try {
// thread join could throw exception // thread join could throw exception
// https://en.cppreference.com/w/cpp/thread/thread/join // https://en.cppreference.com/w/cpp/thread/thread/join
join(); join();
} catch (std::system_error err) {} } catch (std::system_error err) {}
} }
void run(std::unique_ptr<TIn> input) template<typename TIn>
{ void StopableJob<TIn>::run(std::unique_ptr<TIn> input)
{
if (input == nullptr) return; if (input == nullptr) return;
{ {
// keep access to next input // keep access to next input
@ -59,7 +105,7 @@ public:
m_thread = std::thread( m_thread = std::thread(
[this](std::unique_ptr<TIn> input) { [this](std::unique_ptr<TIn> input) {
do { do {
m_func(std::move(input), [this]() { return is_stoping(); }); process(std::move(input), [this]() { return is_stoping(); });
std::lock_guard lg(m_mutex); std::lock_guard lg(m_mutex);
m_stop = false; m_stop = false;
@ -74,32 +120,38 @@ public:
}, },
std::move(input)); std::move(input));
} catch (std::exception &) {} } catch (std::exception &) {}
} }
bool is_running() template<typename TIn>
{ bool StopableJob<TIn>::is_running()
{
std::lock_guard lg(m_mutex); std::lock_guard lg(m_mutex);
return m_running; return m_running;
} }
bool is_stoping()
{
template<typename TIn>
bool StopableJob<TIn>::is_stoping()
{
std::lock_guard lg(m_mutex); std::lock_guard lg(m_mutex);
return m_stop; return m_stop;
} }
void stop() {
template<typename TIn>
void StopableJob<TIn>::stop()
{
std::lock_guard lg(m_mutex); std::lock_guard lg(m_mutex);
if (!m_running) return; if (!m_running) return;
m_input_next = nullptr; m_input_next = nullptr;
m_stop = true; m_stop = true;
} }
// Be Carefull, blocking until join
// call stop when you not sure template<typename TIn>
void join(int timeout_ms = 0) void StopableJob<TIn>::join()
{ {
if (m_thread.joinable()) m_thread.join(); if (m_thread.joinable()) m_thread.join();
assert(!m_running); assert(!m_running);
} }
};
} // namespace Slic3r::GUI } // namespace Slic3r::GUI