From 9c3829c8793d62e85bc57fae1d00a02623b48da1 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Thu, 16 Jan 2025 15:28:44 +0100 Subject: [PATCH] use random number as pid on linux Reason: Flatpak might use same pid for several instances. --- src/libslic3r/utils.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index cc7f448534..fac7870707 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "Platform.hpp" #include "Time.hpp" @@ -941,8 +942,25 @@ unsigned get_current_pid() { #ifdef WIN32 return GetCurrentProcessId(); -#else +#elif __APPLE__ return ::getpid(); +#else + // On flatpak getpid() might return same number for each concurent instances. + static std::atomic instance_uuid{0}; + if (instance_uuid == 0) { + unsigned generated_value; + { + // Use a thread-local random engine + thread_local std::random_device rd; + thread_local std::mt19937 generator(rd()); + std::uniform_int_distribution distribution; + generated_value = distribution(generator); + } + unsigned expected = 0; + // Atomically initialize the instance_uuid if it has not been set + instance_uuid.compare_exchange_strong(expected, generated_value); + } + return instance_uuid.load(); #endif }