forked from supermerill/SuperSlicer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UIThreadWorker for debugging and profiling purposes
- Loading branch information
1 parent
a115702
commit 9892893
Showing
5 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters