diff options
author | Alessandro Ranellucci <aar@cpan.org> | 2016-11-26 18:07:36 +0300 |
---|---|---|
committer | bubnikv <bubnikv@gmail.com> | 2016-12-08 16:40:47 +0300 |
commit | 73ddd3b438cf4fa499e89705c30daa2fbfdf1c99 (patch) | |
tree | 1134a802394d8d12c34755e840ecfd775e8f4cc7 /xs/src/libslic3r/libslic3r.h | |
parent | 9fbd135f14ce6769786226f96f99ec9c30cfd00d (diff) |
Refactored parallelization code, cherry picked from @alexrj 36789774471a0bf9a66bb33cc3ab6984f8ede8c6
Diffstat (limited to 'xs/src/libslic3r/libslic3r.h')
-rw-r--r-- | xs/src/libslic3r/libslic3r.h | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/xs/src/libslic3r/libslic3r.h b/xs/src/libslic3r/libslic3r.h index 5aa7866b8..77d30489d 100644 --- a/xs/src/libslic3r/libslic3r.h +++ b/xs/src/libslic3r/libslic3r.h @@ -4,10 +4,13 @@ // this needs to be included early for MSVC (listing it in Build.PL is not enough) #include <ostream> #include <iostream> +#include <queue> #include <sstream> #include <cstdio> #include <stdint.h> #include <stdarg.h> +#include <vector> +#include <boost/thread.hpp> #define SLIC3R_FORK_NAME "Slic3r Prusa Edition" #define SLIC3R_VERSION "1.31.6" @@ -40,13 +43,6 @@ typedef long coord_t; typedef double coordf_t; -namespace Slic3r { - -enum Axis { X=0, Y, Z }; - -} -using namespace Slic3r; - /* Implementation of CONFESS("foo"): */ #ifdef _MSC_VER #define CONFESS(...) confess_at(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) @@ -91,4 +87,54 @@ inline std::string debug_out_path(const char *name, ...) // Write slices as SVG images into out directory during the 2D processing of the slices. // #define SLIC3R_DEBUG_SLICE_PROCESSING +namespace Slic3r { + +enum Axis { X=0, Y, Z }; + +template <class T> +inline void append_to(std::vector<T> &dst, const std::vector<T> &src) +{ + dst.insert(dst.end(), src.begin(), src.end()); +} + +template <class T> void +_parallelize_do(std::queue<T>* queue, boost::mutex* queue_mutex, boost::function<void(T)> func) +{ + //std::cout << "THREAD STARTED: " << boost::this_thread::get_id() << std::endl; + while (true) { + T i; + { + boost::lock_guard<boost::mutex> l(*queue_mutex); + if (queue->empty()) return; + i = queue->front(); + queue->pop(); + } + //std::cout << " Thread " << boost::this_thread::get_id() << " processing item " << i << std::endl; + func(i); + boost::this_thread::interruption_point(); + } +} + +template <class T> void +parallelize(std::queue<T> queue, boost::function<void(T)> func, + int threads_count = boost::thread::hardware_concurrency()) +{ + boost::mutex queue_mutex; + boost::thread_group workers; + for (int i = 0; i < threads_count; i++) + workers.add_thread(new boost::thread(&_parallelize_do<T>, &queue, &queue_mutex, func)); + workers.join_all(); +} + +template <class T> void +parallelize(T start, T end, boost::function<void(T)> func, + int threads_count = boost::thread::hardware_concurrency()) +{ + std::queue<T> queue; + for (T i = start; i <= end; ++i) queue.push(i); + parallelize(queue, func, threads_count); +} + +} // namespace Slic3r + #endif |