mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-15 15:56:05 +08:00
use random number as pid on linux
Reason: Flatpak might use same pid for several instances.
This commit is contained in:
parent
161095d593
commit
9c3829c879
@ -12,6 +12,7 @@
|
||||
#include <ctime>
|
||||
#include <cstdarg>
|
||||
#include <stdio.h>
|
||||
#include <random>
|
||||
|
||||
#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<unsigned> 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<unsigned> 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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user