OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::_BBS_Backup_Manager Class Reference
Collaboration diagram for Slic3r::_BBS_Backup_Manager:

Classes

struct  Task
struct  timer
union  Task.__unnamed0__

Public Member Functions

void set_post_callback (std::function< void(int)> c)
void run_ui_tasks ()
void push_object_gaurd (ModelObject &object)
void pop_object_gaurd ()
void add_object_mesh (ModelObject &object)
void remove_object_mesh (ModelObject &object)
void backup_soon ()
void remove_backup (Model &model, bool removeAll)
void set_interval (long n)
void put_other_changes ()
void clear_other_changes (bool backup)
bool has_other_changes (bool backup)
void operator() ()
bool delay_task (Task &t, boost::unique_lock< boost::mutex > &lock)

Static Public Member Functions

static _BBS_Backup_Manager & get ()

Private Types

enum  TaskType {
  None , Backup , AddObject , RemoveObject ,
  RemoveBackup , Exit
}

Private Member Functions

 _BBS_Backup_Manager ()
 ~_BBS_Backup_Manager ()
void push_task (Task const &t)
void process_ui_task (Task &t, bool canceled=false)
void process_task (Task &t)

Private Attributes

boost::mutex m_mutex
boost::condition_variable m_cond
std::deque< Task > m_tasks
std::deque< Task > m_ui_tasks
size_t m_task_seq = 0
std::function< void(int)> m_post_callback
long m_interval = 1 * 60
boost::system_time m_next_backup
Model m_temp_model
bool m_other_changes = false
bool m_other_changes_backup = false
std::vector< std::pair< ModelObject *, size_t > > m_gaurd_objects
boost::thread m_thread

Detailed Description


Class Documentation

◆ Slic3r::_BBS_Backup_Manager::Task.__unnamed0__

union Slic3r::_BBS_Backup_Manager::Task.__unnamed0__
Class Members
size_t delay
bool removeAll
size_t sequence

Member Enumeration Documentation

◆ TaskType

Enumerator
None 
Backup 
AddObject 
RemoveObject 
RemoveBackup 
Exit 
9327 {
9328 None,
9329 Backup, // this task is working as response in ui thread
9330 AddObject,
9333 Exit
9334 };
@ AddObject
Definition bbs_3mf.cpp:9330
@ Exit
Definition bbs_3mf.cpp:9333
@ None
Definition bbs_3mf.cpp:9328
@ Backup
Definition bbs_3mf.cpp:9329
@ RemoveBackup
Definition bbs_3mf.cpp:9332
@ RemoveObject
Definition bbs_3mf.cpp:9331

Constructor & Destructor Documentation

◆ _BBS_Backup_Manager()

Slic3r::_BBS_Backup_Manager::_BBS_Backup_Manager ( )
inlineprivate
9380 {
9381 BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " inital and interval = " << m_interval;
9382 m_next_backup = boost::get_system_time() + boost::posix_time::seconds(m_interval);
9383 boost::unique_lock lock(m_mutex);
9384 m_thread = boost::thread(boost::ref(*this));
9385 }
long m_interval
Definition bbs_3mf.cpp:9558
boost::system_time m_next_backup
Definition bbs_3mf.cpp:9559
boost::mutex m_mutex
Definition bbs_3mf.cpp:9550
boost::thread m_thread
Definition bbs_3mf.cpp:9564
@ lock
Definition GLGizmoEmboss.cpp:209

References m_interval, m_mutex, m_next_backup, and m_thread.

Referenced by get().

Here is the caller graph for this function:

◆ ~_BBS_Backup_Manager()

Slic3r::_BBS_Backup_Manager::~_BBS_Backup_Manager ( )
inlineprivate
9387 {
9388 push_task({Exit});
9389 m_thread.join();
9390 }
void push_task(Task const &t)
Definition bbs_3mf.cpp:9392

References Exit, m_thread, and push_task().

Here is the call graph for this function:

Member Function Documentation

◆ add_object_mesh()

void Slic3r::_BBS_Backup_Manager::add_object_mesh ( ModelObject & object)
inline
9244 {
9245 for (auto& g : m_gaurd_objects) {
9246 if (g.first == &object) {
9247 ++g.second;
9248 return;
9249 }
9250 }
9251 // clone object
9252 auto model = object.get_model();
9253 auto o = m_temp_model.add_object(object);
9254 int backup_id = model->get_object_backup_id(object);
9255 push_task({ AddObject, (size_t) backup_id, object.get_model()->get_backup_path(), o, { 1 } });
9256 }
std::vector< std::pair< ModelObject *, size_t > > m_gaurd_objects
Definition bbs_3mf.cpp:9563
Model m_temp_model
Definition bbs_3mf.cpp:9560

References AddObject, m_gaurd_objects, m_temp_model, and push_task().

Referenced by pop_object_gaurd().

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

◆ backup_soon()

void Slic3r::_BBS_Backup_Manager::backup_soon ( )
inline
9262 {
9263 boost::lock_guard lock(m_mutex);
9265 m_tasks.push_back({ Backup, 0, std::string(), nullptr, { ++m_task_seq } });
9266 m_cond.notify_all();
9267 }
bool m_other_changes_backup
Definition bbs_3mf.cpp:9562
size_t m_task_seq
Definition bbs_3mf.cpp:9554
boost::condition_variable m_cond
Definition bbs_3mf.cpp:9551
std::deque< Task > m_tasks
Definition bbs_3mf.cpp:9552

References Backup, m_cond, m_mutex, m_other_changes_backup, m_task_seq, and m_tasks.

◆ clear_other_changes()

void Slic3r::_BBS_Backup_Manager::clear_other_changes ( bool backup)
inline
9314 {
9315 if (backup)
9316 m_other_changes_backup = false;
9317 else
9318 m_other_changes = false;
9319 }
bool m_other_changes
Definition bbs_3mf.cpp:9561

References m_other_changes, and m_other_changes_backup.

◆ delay_task()

bool Slic3r::_BBS_Backup_Manager::delay_task ( Task & t,
boost::unique_lock< boost::mutex > & lock )
inline
9524 {
9525 // delay last task for 3 seconds after last modify
9526 auto now = boost::get_system_time();
9527 auto delay_expire = now + boost::posix_time::seconds(10); // must not delay over this time
9528 auto wait = now + boost::posix_time::seconds(3);
9529 while (true) {
9530 m_cond.timed_wait(lock, wait);
9531 // Only delay when it's the only-one task
9532 if (m_tasks.size() != 1 || m_tasks.front().delay == t.delay)
9533 break;
9534 t.delay = m_tasks.front().delay;
9535 now = boost::get_system_time();
9536 if (now >= delay_expire)
9537 break;
9538 wait = now + boost::posix_time::seconds(3);
9539 if (wait > delay_expire)
9540 wait = delay_expire;
9541 };
9542 // task maybe canceled
9543 if (m_tasks.empty())
9544 return false;
9545 t = m_tasks.front();
9546 return true;
9547 }

References m_cond, and m_tasks.

Referenced by operator()().

Here is the caller graph for this function:

◆ get()

_BBS_Backup_Manager & Slic3r::_BBS_Backup_Manager::get ( )
inlinestatic
9211 {
9212 static _BBS_Backup_Manager m;
9213 return m;
9214 }
_BBS_Backup_Manager()
Definition bbs_3mf.cpp:9380

References _BBS_Backup_Manager().

Referenced by Slic3r::SaveObjectGaurd::SaveObjectGaurd(), Slic3r::SaveObjectGaurd::~SaveObjectGaurd(), Slic3r::backup_soon(), Slic3r::clear_other_changes(), Slic3r::has_other_changes(), Slic3r::put_other_changes(), Slic3r::remove_backup(), Slic3r::run_backup_ui_tasks(), Slic3r::save_object_mesh(), Slic3r::set_backup_callback(), and Slic3r::set_backup_interval().

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

◆ has_other_changes()

bool Slic3r::_BBS_Backup_Manager::has_other_changes ( bool backup)
inline
9322 {
9323 return backup ? m_other_changes_backup : m_other_changes;
9324 }

References m_other_changes, and m_other_changes_backup.

◆ operator()()

void Slic3r::_BBS_Backup_Manager::operator() ( )
inline
9488 {
9489 boost::unique_lock lock(m_mutex);
9490 while (true)
9491 {
9492 while (m_tasks.empty()) {
9493 if (m_interval > 0)
9494 m_cond.timed_wait(lock, m_next_backup);
9495 else
9496 m_cond.wait(lock);
9497 if (m_interval > 0 && boost::get_system_time() > m_next_backup) {
9498 m_tasks.push_back({ Backup, 0, std::string(), nullptr, { ++m_task_seq } });
9499 m_next_backup += boost::posix_time::seconds(m_interval);
9500 // Maybe wakeup from power sleep
9501 if (m_next_backup < boost::get_system_time())
9502 m_next_backup = boost::get_system_time() + boost::posix_time::seconds(m_interval);
9503 }
9504 }
9505 Task t = m_tasks.front();
9506 if (t.type == Exit) break;
9507 if (t.object && t.delay) {
9508 if (!delay_task(t, lock))
9509 continue;
9510 }
9511 m_tasks.pop_front();
9512 auto callback = m_post_callback;
9513 lock.unlock();
9514 process_task(t);
9515 lock.lock();
9516 if (t.type > None) {
9517 m_ui_tasks.push_back(t);
9518 if (m_ui_tasks.size() == 1 && callback)
9519 callback(0);
9520 }
9521 }
9522 }
std::function< void(int)> m_post_callback
Definition bbs_3mf.cpp:9557
std::deque< Task > m_ui_tasks
Definition bbs_3mf.cpp:9553
void process_task(Task &t)
Definition bbs_3mf.cpp:9453
bool delay_task(Task &t, boost::unique_lock< boost::mutex > &lock)
Definition bbs_3mf.cpp:9524
Definition bbs_3mf.cpp:9335

References Backup, delay_task(), Exit, m_cond, m_interval, m_mutex, m_next_backup, m_post_callback, m_task_seq, m_tasks, m_ui_tasks, None, Slic3r::_BBS_Backup_Manager::Task::object, process_task(), and Slic3r::_BBS_Backup_Manager::Task::type.

Here is the call graph for this function:

◆ pop_object_gaurd()

void Slic3r::_BBS_Backup_Manager::pop_object_gaurd ( )
inline
9237 {
9238 auto object = m_gaurd_objects.back();
9239 m_gaurd_objects.pop_back();
9240 if (object.second)
9241 add_object_mesh(*object.first);
9242 }
void add_object_mesh(ModelObject &object)
Definition bbs_3mf.cpp:9244

References add_object_mesh(), and m_gaurd_objects.

Here is the call graph for this function:

◆ process_task()

void Slic3r::_BBS_Backup_Manager::process_task ( Task & t)
inlineprivate
9453 {
9454 BOOST_LOG_TRIVIAL(info) << "process_task" << t.to_string() << " and interval = " << m_interval;
9455 switch (t.type) {
9456 case Backup:
9457 // do it in response
9458 break;
9459 case AddObject: {
9460 {
9461 CNumericLocalesSetter locales_setter;
9462 _BBS_3MF_Exporter e;
9463 e.save_object_mesh(t.path, *t.object, (int) t.id);
9464 // response to delete cloned object
9465 }
9466 break;
9467 }
9468 case RemoveObject: {
9469 boost::system::error_code ec;
9470 boost::filesystem::remove(t.path + "/mesh_" + boost::lexical_cast<std::string>(t.id) + ".xml", ec);
9471 t.type = None;
9472 break;
9473 }
9474 case RemoveBackup: {
9475 try {
9476 boost::system::error_code ec;
9477 boost::filesystem::remove(t.path + "/.3mf", ec);
9478 // We Saved with SplitModel now, so we can safe delete these sub models.
9479 boost::filesystem::remove_all(t.path + "/3D/Objects");
9480 boost::filesystem::create_directory(t.path + "/3D/Objects");
9481 }
9482 catch (...) {}
9483 }
9484 }
9485 }

References AddObject, Backup, Slic3r::_BBS_Backup_Manager::Task::id, m_interval, None, Slic3r::_BBS_Backup_Manager::Task::object, Slic3r::_BBS_Backup_Manager::Task::path, RemoveBackup, RemoveObject, Slic3r::_BBS_3MF_Exporter::save_object_mesh(), Slic3r::_BBS_Backup_Manager::Task::to_string(), and Slic3r::_BBS_Backup_Manager::Task::type.

Referenced by operator()().

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

◆ process_ui_task()

void Slic3r::_BBS_Backup_Manager::process_ui_task ( Task & t,
bool canceled = false )
inlineprivate
9409 {
9410 BOOST_LOG_TRIVIAL(info) << "process_ui_task" << t.to_string() << " and interval = " << m_interval;
9411 switch (t.type) {
9412 case Backup: {
9413 if (canceled)
9414 break;
9415 std::function<void(int)> callback;
9416 boost::unique_lock lock(m_mutex);
9417 if (m_task_seq != t.sequence) {
9418 if (find(m_tasks.begin(), m_tasks.end(), Task{ Backup }) == m_tasks.end()) {
9419 t.sequence = ++m_task_seq; // may has pending tasks, retry later
9420 m_tasks.push_back(t);
9421 m_cond.notify_all();
9422 }
9423 break;
9424 }
9425 callback = m_post_callback;
9426 lock.unlock();
9427 {
9428 timer t("backup cost");
9429 try {
9430 if (callback) callback(1);
9431 } catch (...) {}
9432 }
9433 m_other_changes_backup = false;
9434 break;
9435 }
9436 case AddObject:
9437 m_temp_model.delete_object(t.object);
9438 break;
9439 case RemoveBackup:
9440 if (t.removeAll) {
9441 try {
9442 boost::filesystem::remove(t.path + "/lock.txt");
9443 boost::filesystem::remove_all(t.path);
9444 BOOST_LOG_TRIVIAL(info) << "process_ui_task: remove all of backup path " << t.path;
9445 } catch (std::exception &ex) {
9446 BOOST_LOG_TRIVIAL(error) << "process_ui_task: failed to remove backup path" << t.path << ": " << ex.what();
9447 }
9448 }
9449 break;
9450 }
9451 }
RaycastManager::TrItems::iterator find(RaycastManager::TrItems &items, const RaycastManager::TrKey &key)
Definition RaycastManager.cpp:298
Definition bbs_3mf.cpp:9363

References AddObject, Backup, m_cond, m_interval, m_mutex, m_other_changes_backup, m_post_callback, m_task_seq, m_tasks, m_temp_model, Slic3r::_BBS_Backup_Manager::Task::object, Slic3r::_BBS_Backup_Manager::Task::path, RemoveBackup, Slic3r::_BBS_Backup_Manager::Task::to_string(), and Slic3r::_BBS_Backup_Manager::Task::type.

Referenced by push_task(), remove_backup(), and run_ui_tasks().

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

◆ push_object_gaurd()

void Slic3r::_BBS_Backup_Manager::push_object_gaurd ( ModelObject & object)
inline
9233 {
9234 m_gaurd_objects.push_back(std::make_pair(&object, 0));
9235 }

References m_gaurd_objects.

◆ push_task()

void Slic3r::_BBS_Backup_Manager::push_task ( Task const & t)
inlineprivate
9392 {
9393 boost::unique_lock lock(m_mutex);
9394 if (t.delay && !m_tasks.empty() && m_tasks.back() == t) {
9395 auto t2 = m_tasks.back();
9396 m_tasks.back() = t;
9397 m_tasks.back().delay = t2.delay + 1;
9398 m_cond.notify_all();
9399 lock.unlock();
9400 process_ui_task(t2);
9401 }
9402 else {
9403 m_tasks.push_back(t);
9404 ++m_task_seq;
9405 m_cond.notify_all();
9406 }
9407 }
void process_ui_task(Task &t, bool canceled=false)
Definition bbs_3mf.cpp:9409

References m_cond, m_mutex, m_task_seq, m_tasks, and process_ui_task().

Referenced by ~_BBS_Backup_Manager(), add_object_mesh(), and remove_object_mesh().

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

◆ put_other_changes()

void Slic3r::_BBS_Backup_Manager::put_other_changes ( )
inline
9307 {
9308 BOOST_LOG_TRIVIAL(info) << "put_other_changes";
9309 m_other_changes = true;
9311 }

References m_other_changes, and m_other_changes_backup.

◆ remove_backup()

void Slic3r::_BBS_Backup_Manager::remove_backup ( Model & model,
bool removeAll )
inline
9269 {
9270 BOOST_LOG_TRIVIAL(info)
9271 << "remove_backup " << model.get_backup_path() << ", " << removeAll;
9272 std::deque<Task> canceled_tasks;
9273 boost::unique_lock lock(m_mutex);
9274 if (removeAll && model.is_need_backup()) {
9275 // running task may not be canceled
9276 for (auto & t : m_ui_tasks)
9277 canceled_tasks.push_back(t);
9278 for (auto & t : m_tasks)
9279 canceled_tasks.push_back(t);
9280 m_ui_tasks.clear();
9281 m_tasks.clear();
9282 }
9283 m_tasks.push_back({ RemoveBackup, model.id().id, model.get_backup_path(), nullptr, { removeAll } });
9284 ++m_task_seq;
9285 if (model.is_need_backup()) {
9286 m_other_changes = false;
9287 m_other_changes_backup = false;
9288 }
9289 m_cond.notify_all();
9290 lock.unlock();
9291 for (auto& t : canceled_tasks) {
9292 process_ui_task(t, true);
9293 }
9294 }

References Slic3r::Model::get_backup_path(), Slic3r::ObjectBase::id(), Slic3r::ObjectID::id, Slic3r::Model::is_need_backup(), m_cond, m_mutex, m_other_changes, m_other_changes_backup, m_task_seq, m_tasks, m_ui_tasks, process_ui_task(), and RemoveBackup.

Here is the call graph for this function:

◆ remove_object_mesh()

void Slic3r::_BBS_Backup_Manager::remove_object_mesh ( ModelObject & object)
inline
9258 {
9259 push_task({ RemoveObject, object.id().id, object.get_model()->get_backup_path() });
9260 }

References push_task(), and RemoveObject.

Here is the call graph for this function:

◆ run_ui_tasks()

void Slic3r::_BBS_Backup_Manager::run_ui_tasks ( )
inline
9221 {
9222 std::deque<Task> tasks;
9223 {
9224 boost::lock_guard lock(m_mutex);
9225 std::swap(tasks, m_ui_tasks);
9226 }
9227 for (auto& t : tasks)
9228 {
9229 process_ui_task(t);
9230 }
9231 }

References m_mutex, m_ui_tasks, and process_ui_task().

Here is the call graph for this function:

◆ set_interval()

void Slic3r::_BBS_Backup_Manager::set_interval ( long n)
inline
9296 {
9297 boost::lock_guard lock(m_mutex);
9298 BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " entry, and last interval is: " << m_interval;
9299 m_next_backup -= boost::posix_time::seconds(m_interval);
9300 m_interval = n;
9301 m_next_backup += boost::posix_time::seconds(m_interval);
9302 m_cond.notify_all();
9303 BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " exit, and new interval is: " << m_interval;
9304 }

References m_cond, m_interval, m_mutex, and m_next_backup.

◆ set_post_callback()

void Slic3r::_BBS_Backup_Manager::set_post_callback ( std::function< void(int)> c)
inline
9216 {
9217 boost::lock_guard lock(m_mutex);
9218 m_post_callback = c;
9219 }

References m_mutex, and m_post_callback.

Member Data Documentation

◆ m_cond

boost::condition_variable Slic3r::_BBS_Backup_Manager::m_cond
private

◆ m_gaurd_objects

std::vector<std::pair<ModelObject*, size_t> > Slic3r::_BBS_Backup_Manager::m_gaurd_objects
private

◆ m_interval

long Slic3r::_BBS_Backup_Manager::m_interval = 1 * 60
private

◆ m_mutex

boost::mutex Slic3r::_BBS_Backup_Manager::m_mutex
private

◆ m_next_backup

boost::system_time Slic3r::_BBS_Backup_Manager::m_next_backup
private

◆ m_other_changes

bool Slic3r::_BBS_Backup_Manager::m_other_changes = false
private

◆ m_other_changes_backup

bool Slic3r::_BBS_Backup_Manager::m_other_changes_backup = false
private

◆ m_post_callback

std::function<void(int)> Slic3r::_BBS_Backup_Manager::m_post_callback
private

◆ m_task_seq

size_t Slic3r::_BBS_Backup_Manager::m_task_seq = 0
private

◆ m_tasks

std::deque<Task> Slic3r::_BBS_Backup_Manager::m_tasks
private

◆ m_temp_model

Model Slic3r::_BBS_Backup_Manager::m_temp_model
private

Referenced by add_object_mesh(), and process_ui_task().

◆ m_thread

boost::thread Slic3r::_BBS_Backup_Manager::m_thread
private

◆ m_ui_tasks

std::deque<Task> Slic3r::_BBS_Backup_Manager::m_ui_tasks
private

The documentation for this class was generated from the following file:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/Format/bbs_3mf.cpp