Skip to content

Commit

Permalink
Rename start_next() to push
Browse files Browse the repository at this point in the history
PlaterJob refinements
  • Loading branch information
tamasmeszaros committed Jan 11, 2022
1 parent 7e070d3 commit 583c123
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/slic3r/GUI/Jobs/BoostThreadWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void BoostThreadWorker::process_events()
}));
}

bool BoostThreadWorker::start_next(std::unique_ptr<Job> job)
bool BoostThreadWorker::push(std::unique_ptr<Job> job)
{
if (job)
m_input_queue.push(JobEntry{std::move(job)});
Expand Down
2 changes: 1 addition & 1 deletion src/slic3r/GUI/Jobs/BoostThreadWorker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class BoostThreadWorker : public Worker, private Job::Ctl
BoostThreadWorker &operator=(const BoostThreadWorker &) = delete;
BoostThreadWorker &operator=(BoostThreadWorker &&) = delete;

bool start_next(std::unique_ptr<Job> job) override;
bool push(std::unique_ptr<Job> job) override;

bool is_idle() const override
{
Expand Down
51 changes: 34 additions & 17 deletions src/slic3r/GUI/Jobs/PlaterWorker.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef PLATERWORKER_HPP
#define PLATERWORKER_HPP

#include <map>

#include "Worker.hpp"
#include "BusyCursorJob.hpp"

#include "slic3r/GUI/GUI.hpp"
Expand All @@ -16,9 +19,21 @@ class Plater;
template<class WorkerSubclass>
class PlaterWorker: public Worker {
WorkerSubclass m_w;
Plater *m_plater;

class PlaterJob : public Job {
struct JobHolder : Job {
std::unique_ptr<Job> m_job;
void process(Ctl &ctl) override { m_job->process(ctl); };
void finalize(bool canceled, std::exception_ptr &e) override
{
m_job->finalize(canceled, e);
}
JobHolder(std::unique_ptr<Job> &&j) : m_job{std::move(j)} {}
};

template<class JobSubclass>
class PlaterJob : public Job {
JobSubclass m_job;
Plater *m_plater;

public:
Expand Down Expand Up @@ -49,25 +64,27 @@ class PlaterWorker: public Worker {
} wctl{c};

CursorSetterRAII busycursor{wctl};
m_job->process(wctl);
m_job.process(wctl);
}

void finalize(bool canceled, std::exception_ptr &eptr) override
{
m_job->finalize(canceled, eptr);
m_job.finalize(canceled, eptr);

if (eptr) try {
std::rethrow_exception(eptr);
} catch (std::exception &e) {
show_error(m_plater, _L("An unexpected error occured: ") + e.what());
eptr = nullptr;
}
std::rethrow_exception(eptr);
} catch (std::exception &e) {
show_error(m_plater, _L("An unexpected error occured: ") + e.what());
eptr = nullptr;
}
}

PlaterJob(std::unique_ptr<Job> j)
: m_job{std::move(j)}, m_plater{wxGetApp().plater()}
template<class...Args>
PlaterJob(Plater *p, Args&&...args)
: m_job{std::forward<Args>(args)...}, m_plater{p}
{
// TODO: decide if disabling slice button during UI job is what we want.
// TODO: decide if disabling slice button during UI job is what we
// want.
// if (m_plater)
// m_plater->sidebar().enable_buttons(false);
}
Expand All @@ -84,29 +101,29 @@ class PlaterWorker: public Worker {
}
};


public:

template<class... WorkerArgs>
PlaterWorker(Plater *plater, WorkerArgs &&...args)
: m_w{std::forward<WorkerArgs>(args)...}
: m_w{std::forward<WorkerArgs>(args)...}, m_plater{plater}
{
// Ensure that messages from the worker thread to the UI thread are
// processed continuously.
plater->Bind(wxEVT_IDLE, [this](wxIdleEvent &) {
m_w.process_events();
process_events();
});
}

// Always package the job argument into a PlaterJob
bool start_next(std::unique_ptr<Job> job) override
bool push(std::unique_ptr<Job> job) override
{
return m_w.start_next(std::make_unique<PlaterJob>(std::move(job)));
return m_w.push(std::make_unique<PlaterJob<JobHolder>>(m_plater, std::move(job)));
}

bool is_idle() const override { return m_w.is_idle(); }
void cancel() override { m_w.cancel(); }
void cancel_all() override { m_w.cancel_all(); }
void process_events() override {}
void process_events() override { m_w.process_events(); }
};

}} // namespace Slic3r::GUI
Expand Down
6 changes: 3 additions & 3 deletions src/slic3r/GUI/Jobs/Worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Worker {
public:
// Queue up a new job after the current one. This call does not block.
// Returns false if the job gets discarded.
virtual bool start_next(std::unique_ptr<Job> job) = 0;
virtual bool push(std::unique_ptr<Job> job) = 0;

// Returns true if no job is running and no job message is left to be processed.
// This means that nothing is left to finalize or take care of in the main thread.
Expand Down Expand Up @@ -63,7 +63,7 @@ bool queue_job(Worker &w, ProcessFn fn, FinishFn finishfn)
};

auto j = std::make_unique<LambdaJob>(std::move(fn), std::move(finishfn));
return w.start_next(std::move(j));
return w.push(std::move(j));
}

template<class ProcessFn, class = std::enable_if_t<IsProcessFn<ProcessFn>>>
Expand All @@ -74,7 +74,7 @@ bool queue_job(Worker &w, ProcessFn fn)

inline bool queue_job(Worker &w, std::unique_ptr<Job> j)
{
return w.start_next(std::move(j));
return w.push(std::move(j));
}

// Replace the current job queue with a new job. The signature is the same
Expand Down

0 comments on commit 583c123

Please sign in to comment.