Sets locales before any thread start participating in the GCode processing pipeline.

Locales should be set once per any participating threads in tbb::parallel_pipeline.
It should fix the issue with appearing comma instead of the decimal point in generated Gcode.
This commit is contained in:
Lukáš Hejl 2022-05-30 15:13:42 +02:00 committed by Lukas Matena
parent 81d43a2fd4
commit 2b87601239
3 changed files with 58 additions and 15 deletions
src/libslic3r

View file

@ -35,6 +35,8 @@
#include "SVG.hpp"
#include <tbb/parallel_for.h>
#include <tbb/task_scheduler_observer.h>
#include <tbb/enumerable_thread_specific.h>
// Intel redesigned some TBB interface considerably when merging TBB with their oneAPI set of libraries, see GH #7332.
// We are using quite an old TBB 2017 U7. Before we update our build servers, let's use the old API, which is deprecated in up to date TBB.
@ -1537,6 +1539,32 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
print.throw_if_canceled();
}
// For unknown reasons and in sporadic cases when GCode export is processing, some participating thread
// in tbb::parallel_pipeline has not set locales to "C", probably because this thread is newly spawned.
// So in this class method on_scheduler_entry is called for every thread before it starts participating
// in tbb::parallel_pipeline to ensure that locales are set correctly
// For tbb::parallel_pipeline, it seems that on_scheduler_entry is called for every layer and every filter.
// We ensure using thread-local storage that locales will be set to "C" just once for any participating thread.
class TBBLocalesSetter : public tbb::task_scheduler_observer
{
public:
TBBLocalesSetter() { this->observe(true); }
~TBBLocalesSetter() override = default;
void on_scheduler_entry(bool is_worker) override
{
if (bool &is_locales_sets = m_is_locales_sets.local(); !is_locales_sets) {
// Set locales of the worker thread to "C".
set_c_locales();
is_locales_sets = true;
}
}
private:
tbb::enumerable_thread_specific<bool, tbb::cache_aligned_allocator<bool>, tbb::ets_key_usage_type::ets_key_per_instance> m_is_locales_sets;
};
// Process all layers of all objects (non-sequential mode) with a parallel pipeline:
// Generate G-code, run the filters (vase mode, cooling buffer), run the G-code analyser
// and export G-code into file.
@ -1580,6 +1608,10 @@ void GCode::process_layers(
[&output_stream](std::string s) { output_stream.write(s); }
);
// It registers a handler that sets locales to "C" before any TBB thread starts participating in tbb::parallel_pipeline.
// Handler is unregistered when the destructor is called.
TBBLocalesSetter locales_setter;
// The pipeline elements are joined using const references, thus no copying is performed.
output_stream.find_replace_supress();
if (m_spiral_vase && m_find_replace)
@ -1633,6 +1665,10 @@ void GCode::process_layers(
[&output_stream](std::string s) { output_stream.write(s); }
);
// It registers a handler that sets locales to "C" before any TBB thread starts participating in tbb::parallel_pipeline.
// Handler is unregistered when the destructor is called.
TBBLocalesSetter locales_setter;
// The pipeline elements are joined using const references, thus no copying is performed.
output_stream.find_replace_supress();
if (m_spiral_vase && m_find_replace)