OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::BoostThreadWorker Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Jobs/BoostThreadWorker.hpp>

Inheritance diagram for Slic3r::GUI::BoostThreadWorker:
Collaboration diagram for Slic3r::GUI::BoostThreadWorker:

Classes

struct  JobEntry
struct  StatusInfo
struct  MainThreadCallData
struct  EmptyMessage
class  WorkerMessage

Public Member Functions

 BoostThreadWorker (std::shared_ptr< ProgressIndicator > pri, boost::thread::attributes &attr, const char *name="")
 BoostThreadWorker (std::shared_ptr< ProgressIndicator > pri, boost::thread::attributes &&attr, const char *name="")
 BoostThreadWorker (std::shared_ptr< ProgressIndicator > pri, const char *name="")
 ~BoostThreadWorker ()
 BoostThreadWorker (const BoostThreadWorker &)=delete
 BoostThreadWorker (BoostThreadWorker &&)=delete
BoostThreadWorker & operator= (const BoostThreadWorker &)=delete
BoostThreadWorker & operator= (BoostThreadWorker &&)=delete
bool push (std::shared_ptr< Job > job) override
bool is_idle () const override
void cancel () override
void cancel_all () override
ProgressIndicator * get_pri ()
const ProgressIndicator * get_pri () const
void clear_percent () override
void show_error_info (const std::string &msg, int code, const std::string &description, const std::string &extra) override
void process_events () override
bool wait_for_current_job (unsigned timeout_ms=0) override
bool wait_for_idle (unsigned timeout_ms=0) override

Protected Member Functions

void update_status (int st, const std::string &msg="") override
bool was_canceled () const override
std::future< void > call_on_main_thread (std::function< void()> fn) override

Private Types

using JobQueue = ThreadSafeQueueSPSC<JobEntry>
using MessageQueue = ThreadSafeQueueSPSC<WorkerMessage>

Private Member Functions

void run ()
bool join (int timeout_ms=0)

Private Attributes

boost::thread m_thread
std::atomic< bool > m_running {false}
std::atomic< bool > m_canceled {false}
std::shared_ptr< ProgressIndicator > m_progress
JobQueue m_input_queue
MessageQueue m_output_queue
std::string m_name

Detailed Description


Class Documentation

◆ Slic3r::GUI::BoostThreadWorker::JobEntry

struct Slic3r::GUI::BoostThreadWorker::JobEntry
Collaboration diagram for Slic3r::GUI::BoostThreadWorker::JobEntry:
Class Members
bool canceled = false
exception_ptr eptr = nullptr
shared_ptr< Job > job

◆ Slic3r::GUI::BoostThreadWorker::StatusInfo

struct Slic3r::GUI::BoostThreadWorker::StatusInfo
Collaboration diagram for Slic3r::GUI::BoostThreadWorker::StatusInfo:
Class Members
string msg
int status

◆ Slic3r::GUI::BoostThreadWorker::MainThreadCallData

struct Slic3r::GUI::BoostThreadWorker::MainThreadCallData
Class Members
function< void()> fn
promise< void > promise

◆ Slic3r::GUI::BoostThreadWorker::EmptyMessage

struct Slic3r::GUI::BoostThreadWorker::EmptyMessage

Member Typedef Documentation

◆ JobQueue

◆ MessageQueue

Constructor & Destructor Documentation

◆ BoostThreadWorker() [1/5]

Slic3r::GUI::BoostThreadWorker::BoostThreadWorker ( std::shared_ptr< ProgressIndicator > pri,
boost::thread::attributes & attr,
const char * name = "" )
explicit
93 : m_progress(std::move(pri)), m_name{name}
94{
95 if (m_progress)
96 m_progress->set_cancel_callback([this](){ cancel(); });
97
98 m_thread = create_thread(attribs, [this] { this->run(); });
99
100 std::string nm{name};
101 if (!nm.empty()) set_thread_name(m_thread, name);
102}
std::shared_ptr< ProgressIndicator > m_progress
Definition BoostThreadWorker.hpp:82
std::string m_name
Definition BoostThreadWorker.hpp:85
void run()
Definition BoostThreadWorker.cpp:50
void cancel() override
Definition BoostThreadWorker.hpp:137
boost::thread m_thread
Definition BoostThreadWorker.hpp:80
boost::thread create_thread(boost::thread::attributes &attrs, Fn &&fn)
Definition Thread.hpp:48
bool set_thread_name(std::thread &thread, const char *thread_name)
Definition Thread.cpp:175

References Slic3r::create_thread(), m_name, m_progress, m_thread, run(), and Slic3r::set_thread_name().

Referenced by BoostThreadWorker(), BoostThreadWorker(), BoostThreadWorker(), BoostThreadWorker(), Slic3r::GUI::BoostThreadWorker::WorkerMessage::deliver(), operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BoostThreadWorker() [2/5]

Slic3r::GUI::BoostThreadWorker::BoostThreadWorker ( std::shared_ptr< ProgressIndicator > pri,
boost::thread::attributes && attr,
const char * name = "" )
inlineexplicit
108 : BoostThreadWorker{std::move(pri), attr, name}
109 {}
BoostThreadWorker(std::shared_ptr< ProgressIndicator > pri, boost::thread::attributes &attr, const char *name="")
Definition BoostThreadWorker.cpp:90

References BoostThreadWorker().

Here is the call graph for this function:

◆ BoostThreadWorker() [3/5]

Slic3r::GUI::BoostThreadWorker::BoostThreadWorker ( std::shared_ptr< ProgressIndicator > pri,
const char * name = "" )
inlineexplicit
113 : BoostThreadWorker{std::move(pri), {}, name}
114 {}

References BoostThreadWorker().

Here is the call graph for this function:

◆ ~BoostThreadWorker()

Slic3r::GUI::BoostThreadWorker::~BoostThreadWorker ( )
107{
108 bool joined = false;
109 try {
110 cancel_all();
112 m_input_queue.push(JobEntry{nullptr});
113 joined = join(ABORT_WAIT_MAX_MS);
114 } catch(...) {}
115
116 if (!joined)
117 BOOST_LOG_TRIVIAL(error)
118 << "Could not join worker thread '" << m_name << "'";
119}
bool join(int timeout_ms=0)
Definition BoostThreadWorker.cpp:121
bool wait_for_idle(unsigned timeout_ms=0) override
Definition BoostThreadWorker.cpp:170
JobQueue m_input_queue
Definition BoostThreadWorker.hpp:83
void cancel_all() override
Definition BoostThreadWorker.hpp:138
Definition BoostThreadWorker.hpp:37
constexpr int ABORT_WAIT_MAX_MS
Definition BoostThreadWorker.cpp:104

References Slic3r::GUI::ABORT_WAIT_MAX_MS, cancel_all(), join(), m_input_queue, m_name, and wait_for_idle().

Here is the call graph for this function:

◆ BoostThreadWorker() [4/5]

Slic3r::GUI::BoostThreadWorker::BoostThreadWorker ( const BoostThreadWorker & )
delete

References BoostThreadWorker().

Here is the call graph for this function:

◆ BoostThreadWorker() [5/5]

Slic3r::GUI::BoostThreadWorker::BoostThreadWorker ( BoostThreadWorker && )
delete

References BoostThreadWorker().

Here is the call graph for this function:

Member Function Documentation

◆ call_on_main_thread()

std::future< void > Slic3r::GUI::BoostThreadWorker::call_on_main_thread ( std::function< void()> fn)
overrideprotectedvirtual

Implements Slic3r::GUI::Job::Ctl.

81{
82 MainThreadCallData cbdata{std::move(fn), {}};
83 std::future<void> future = cbdata.promise.get_future();
84
85 m_output_queue.push(std::move(cbdata));
86
87 return future;
88}
MessageQueue m_output_queue
Definition BoostThreadWorker.hpp:84
std::promise< void > promise
Definition BoostThreadWorker.hpp:51
Definition BoostThreadWorker.hpp:49

References m_output_queue, and Slic3r::GUI::BoostThreadWorker::MainThreadCallData::promise.

◆ cancel()

void Slic3r::GUI::BoostThreadWorker::cancel ( )
inlineoverridevirtual

Implements Slic3r::GUI::Worker.

137{ m_canceled.store(true); }
std::atomic< bool > m_canceled
Definition BoostThreadWorker.hpp:81

References m_canceled.

Referenced by cancel_all().

Here is the caller graph for this function:

◆ cancel_all()

void Slic3r::GUI::BoostThreadWorker::cancel_all ( )
inlineoverridevirtual

Implements Slic3r::GUI::Worker.

138{ m_input_queue.clear(); cancel(); }

References cancel(), and m_input_queue.

Referenced by ~BoostThreadWorker().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear_percent()

void Slic3r::GUI::BoostThreadWorker::clear_percent ( )
inlineoverridevirtual

Implements Slic3r::GUI::Job::Ctl.

144 {
145 if (m_progress) {
146 m_progress->clear_percent();
147 }
148 }

References m_progress.

◆ get_pri() [1/2]

ProgressIndicator * Slic3r::GUI::BoostThreadWorker::get_pri ( )
inline
140{ return m_progress.get(); }

References m_progress.

Referenced by Slic3r::GUI::BoostThreadWorker::WorkerMessage::deliver().

Here is the caller graph for this function:

◆ get_pri() [2/2]

const ProgressIndicator * Slic3r::GUI::BoostThreadWorker::get_pri ( ) const
inline
141{ return m_progress.get(); }

References m_progress.

◆ is_idle()

bool Slic3r::GUI::BoostThreadWorker::is_idle ( ) const
inlineoverridevirtual

Implements Slic3r::GUI::Worker.

126 {
127 // The assumption is that jobs can only be queued from a single main
128 // thread from which this method is also called. And the output
129 // messages are also processed only in this calling thread. In that
130 // case, if the input queue is empty, it will remain so during this
131 // function call. If the worker thread is also not running and the
132 // output queue is already processed, we can safely say that the
133 // worker is dormant.
134 return m_input_queue.empty() && !m_running.load() && m_output_queue.empty();
135 }
std::atomic< bool > m_running
Definition BoostThreadWorker.hpp:81

References m_input_queue, m_output_queue, and m_running.

Referenced by wait_for_current_job(), and wait_for_idle().

Here is the caller graph for this function:

◆ join()

bool Slic3r::GUI::BoostThreadWorker::join ( int timeout_ms = 0)
private
122{
123 if (!m_thread.joinable())
124 return true;
125
126 if (timeout_ms <= 0) {
127 m_thread.join();
128 }
129 else if (m_thread.try_join_for(boost::chrono::milliseconds(timeout_ms))) {
130 return true;
131 }
132 else
133 return false;
134
135 return true;
136}

References m_thread.

Referenced by ~BoostThreadWorker().

Here is the caller graph for this function:

◆ operator=() [1/2]

BoostThreadWorker & Slic3r::GUI::BoostThreadWorker::operator= ( BoostThreadWorker && )
delete

References BoostThreadWorker(), and push().

Here is the call graph for this function:

◆ operator=() [2/2]

BoostThreadWorker & Slic3r::GUI::BoostThreadWorker::operator= ( const BoostThreadWorker & )
delete

References BoostThreadWorker().

Here is the call graph for this function:

◆ process_events()

void Slic3r::GUI::BoostThreadWorker::process_events ( )
overridevirtual

Implements Slic3r::GUI::Worker.

139{
140 while (m_output_queue.consume_one([this](WorkerMessage &msg) {
141 msg.deliver(*this);
142 }));
143}
Definition BoostThreadWorker.hpp:57

References m_output_queue.

◆ push()

bool Slic3r::GUI::BoostThreadWorker::push ( std::shared_ptr< Job > job)
overridevirtual

Implements Slic3r::GUI::Worker.

185{
186 if (!job)
187 return false;
188
189 m_input_queue.push(JobEntry{std::move(job)});
190 return true;
191}

References m_input_queue.

Referenced by operator=().

Here is the caller graph for this function:

◆ run()

void Slic3r::GUI::BoostThreadWorker::run ( )
private
51{
52 bool stop = false;
53 while (!stop) {
55 .consume_one(BlockingWait{0, &m_running}, [this, &stop](JobEntry &e) {
56 if (!e.job)
57 stop = true;
58 else {
59 m_canceled.store(false);
60
61 try {
62 e.job->process(*this);
63 } catch (...) {
64 e.eptr = std::current_exception();
65 }
66
67 e.canceled = m_canceled.load();
68 m_output_queue.push(std::move(e)); // finalization message
69 }
70 m_running.store(false);
71 });
72 };
73}

References m_canceled, m_input_queue, m_output_queue, and m_running.

Referenced by BoostThreadWorker().

Here is the caller graph for this function:

◆ show_error_info()

void Slic3r::GUI::BoostThreadWorker::show_error_info ( const std::string & msg,
int code,
const std::string & description,
const std::string & extra )
inlineoverridevirtual

Implements Slic3r::GUI::Job::Ctl.

151 {
152 if (m_progress) {
153 m_progress->show_error_info(from_u8(msg), code, from_u8(description), from_u8(extra));
154 }
155 }
wxString from_u8(const std::string &str)
Definition GUI.cpp:544

References Slic3r::GUI::from_u8(), and m_progress.

Here is the call graph for this function:

◆ update_status()

void Slic3r::GUI::BoostThreadWorker::update_status ( int st,
const std::string & msg = "" )
overrideprotectedvirtual

Implements Slic3r::GUI::Job::Ctl.

76{
77 m_output_queue.push(st, msg);
78}

References m_output_queue.

◆ wait_for_current_job()

bool Slic3r::GUI::BoostThreadWorker::wait_for_current_job ( unsigned timeout_ms = 0)
overridevirtual

Implements Slic3r::GUI::Worker.

146{
147 bool ret = true;
148
149 if (!is_idle()) {
150 bool was_finish = false;
151 bool timeout_reached = false;
152 while (!timeout_reached && !was_finish) {
153 timeout_reached =
154 !m_output_queue.consume_one(BlockingWait{timeout_ms},
155 [this, &was_finish](
156 WorkerMessage &msg) {
157 msg.deliver(*this);
158 if (msg.get_type() ==
160 was_finish = true;
161 });
162 }
163
164 ret = !timeout_reached;
165 }
166
167 return ret;
168}
@ Finalize
Definition BoostThreadWorker.hpp:59
bool is_idle() const override
Definition BoostThreadWorker.hpp:125

References Slic3r::GUI::BoostThreadWorker::WorkerMessage::Finalize, is_idle(), and m_output_queue.

Here is the call graph for this function:

◆ wait_for_idle()

bool Slic3r::GUI::BoostThreadWorker::wait_for_idle ( unsigned timeout_ms = 0)
overridevirtual

Implements Slic3r::GUI::Worker.

171{
172 bool timeout_reached = false;
173 while (!timeout_reached && !is_idle()) {
174 timeout_reached = !m_output_queue
175 .consume_one(BlockingWait{timeout_ms},
176 [this](WorkerMessage &msg) {
177 msg.deliver(*this);
178 });
179 }
180
181 return !timeout_reached;
182}

References is_idle(), and m_output_queue.

Referenced by ~BoostThreadWorker().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ was_canceled()

bool Slic3r::GUI::BoostThreadWorker::was_canceled ( ) const
inlineoverrideprotectedvirtual

Implements Slic3r::GUI::Job::Ctl.

96{ return m_canceled.load(); }

References m_canceled.

Member Data Documentation

◆ m_canceled

std::atomic<bool> Slic3r::GUI::BoostThreadWorker::m_canceled {false}
private
81{false}, m_canceled{false};

Referenced by cancel(), run(), and was_canceled().

◆ m_input_queue

JobQueue Slic3r::GUI::BoostThreadWorker::m_input_queue
private

◆ m_name

std::string Slic3r::GUI::BoostThreadWorker::m_name
private

◆ m_output_queue

MessageQueue Slic3r::GUI::BoostThreadWorker::m_output_queue
private

◆ m_progress

std::shared_ptr<ProgressIndicator> Slic3r::GUI::BoostThreadWorker::m_progress
private

◆ m_running

std::atomic<bool> Slic3r::GUI::BoostThreadWorker::m_running {false}
private
81{false}, m_canceled{false};

Referenced by is_idle(), and run().

◆ m_thread

boost::thread Slic3r::GUI::BoostThreadWorker::m_thread
private

Referenced by BoostThreadWorker(), and join().


The documentation for this class was generated from the following files: