Add UIThreadWorker for debugging and profiling purposes
This commit is contained in:
parent
a115702289
commit
9892893587
5 changed files with 150 additions and 14 deletions
114
src/slic3r/GUI/Jobs/UIThreadWorker.hpp
Normal file
114
src/slic3r/GUI/Jobs/UIThreadWorker.hpp
Normal file
|
@ -0,0 +1,114 @@
|
|||
#ifndef UITHREADWORKER_HPP
|
||||
#define UITHREADWORKER_HPP
|
||||
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
|
||||
#include "Worker.hpp"
|
||||
#include "ProgressIndicator.hpp"
|
||||
|
||||
namespace Slic3r { namespace GUI {
|
||||
|
||||
// Implementation of a worker which does not create any additional threads.
|
||||
class UIThreadWorker : public Worker, private Job::Ctl {
|
||||
std::queue<std::unique_ptr<Job>, std::deque<std::unique_ptr<Job>>> m_jobqueue;
|
||||
std::shared_ptr<ProgressIndicator> m_progress;
|
||||
bool m_running = false;
|
||||
bool m_canceled = false;
|
||||
|
||||
void process_front()
|
||||
{
|
||||
std::unique_ptr<Job> job;
|
||||
|
||||
if (!m_jobqueue.empty()) {
|
||||
job = std::move(m_jobqueue.front());
|
||||
m_jobqueue.pop();
|
||||
}
|
||||
|
||||
if (job) {
|
||||
std::exception_ptr eptr;
|
||||
m_running = true;
|
||||
|
||||
try {
|
||||
job->process(*this);
|
||||
} catch (...) {
|
||||
eptr= std::current_exception();
|
||||
}
|
||||
|
||||
m_running = false;
|
||||
|
||||
job->finalize(m_canceled, eptr);
|
||||
|
||||
m_canceled = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// Implement Job::Ctl interface:
|
||||
|
||||
void update_status(int st, const std::string &msg = "") override
|
||||
{
|
||||
if (m_progress) {
|
||||
m_progress->set_progress(st);
|
||||
m_progress->set_status_text(msg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool was_canceled() const override { return m_canceled; }
|
||||
|
||||
std::future<void> call_on_main_thread(std::function<void()> fn) override
|
||||
{
|
||||
return std::async(std::launch::deferred, [fn]{ fn(); });
|
||||
}
|
||||
|
||||
public:
|
||||
explicit UIThreadWorker(std::shared_ptr<ProgressIndicator> pri,
|
||||
const std::string & /*name*/ = "")
|
||||
: m_progress{pri}
|
||||
{}
|
||||
|
||||
UIThreadWorker() = default;
|
||||
|
||||
bool push(std::unique_ptr<Job> job) override
|
||||
{
|
||||
m_canceled = false;
|
||||
m_jobqueue.push(std::move(job));
|
||||
|
||||
return bool(job);
|
||||
}
|
||||
|
||||
bool is_idle() const override { return !m_running; }
|
||||
|
||||
void cancel() override { m_canceled = true; }
|
||||
|
||||
void cancel_all() override
|
||||
{
|
||||
m_canceled = true;
|
||||
process_front();
|
||||
while (!m_jobqueue.empty()) m_jobqueue.pop();
|
||||
}
|
||||
|
||||
void process_events() override {
|
||||
while (!m_jobqueue.empty())
|
||||
process_front();
|
||||
}
|
||||
|
||||
bool wait_for_current_job(unsigned /*timeout_ms*/ = 0) override {
|
||||
process_front();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wait_for_idle(unsigned /*timeout_ms*/ = 0) override {
|
||||
process_events();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ProgressIndicator * get_pri() { return m_progress.get(); }
|
||||
const ProgressIndicator * get_pri() const { return m_progress.get(); }
|
||||
};
|
||||
|
||||
}} // namespace Slic3r::GUI
|
||||
|
||||
#endif // UITHREADWORKER_HPP
|
Loading…
Add table
Add a link
Reference in a new issue