Guard all uses of basic_random_generator for thread safety

This commit is contained in:
bgbsww
2024-06-04 10:02:13 -04:00
committed by Chris Hennes
parent 7e9496ecae
commit 42eb4fdda6
6 changed files with 46 additions and 0 deletions

View File

@@ -113,6 +113,8 @@
#include <BRep_Tool.hxx>
#include <TopoDS.hxx>
#include <memory>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "Geometry.h"
#include "ArcOfCirclePy.h"
@@ -455,8 +457,13 @@ void Geometry::deleteExtension(const std::string & name)
void Geometry::createNewTag()
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(static_cast<unsigned int>(std::time(nullptr)));

View File

@@ -30,6 +30,8 @@
#include <Base/Tools.h>
#include <Base/Writer.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "Constraint.h"
#include "ConstraintPy.h"
@@ -58,8 +60,13 @@ Constraint::Constraint()
, isActive(true)
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(QDateTime::currentMSecsSinceEpoch() & 0xffffffff);

View File

@@ -34,6 +34,8 @@
#include <BRepTools.hxx>
#include <Base/Console.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "CenterLine.h"
#include "DrawUtil.h"
@@ -1072,8 +1074,13 @@ std::string CenterLine::getTagAsString() const
void CenterLine::createNewTag()
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(static_cast<unsigned int>(std::time(nullptr)));

View File

@@ -27,6 +27,8 @@
# include <boost/uuid/uuid_generators.hpp>
# include <boost/uuid/uuid_io.hpp>
#endif
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <App/Application.h>
#include <Base/Vector3D.h>
@@ -312,8 +314,13 @@ std::string CosmeticEdge::getTagAsString() const
void CosmeticEdge::createNewTag()
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(static_cast<unsigned int>(std::time(nullptr)));
@@ -483,8 +490,13 @@ std::string GeomFormat::getTagAsString() const
void GeomFormat::createNewTag()
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(static_cast<unsigned int>(std::time(nullptr)));

View File

@@ -32,6 +32,8 @@
#include <App/Application.h>
#include <Base/Persistence.h>
#include <Base/Vector3D.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "CosmeticVertex.h"
#include "CosmeticVertexPy.h"
@@ -235,8 +237,13 @@ std::string CosmeticVertex::getTagAsString() const
void CosmeticVertex::createNewTag()
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(static_cast<unsigned int>(std::time(nullptr)));

View File

@@ -81,6 +81,8 @@
#include <Base/Reader.h>
#include <Base/Tools.h>
#include <Base/Writer.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <Mod/Part/App/Geometry.h>
#include <Mod/Part/App/TopoShape.h>
@@ -1393,9 +1395,13 @@ void Vertex::Restore(Base::XMLReader &reader)
void Vertex::createNewTag()
{
// Initialize a random number generator, to avoid Valgrind false positives.
// The random number generator is not threadsafe so we guard it. See
// https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes
static boost::mt19937 ran;
static bool seeded = false;
static boost::mutex random_number_mutex;
boost::lock_guard<boost::mutex> guard(random_number_mutex);
if (!seeded) {
ran.seed(static_cast<unsigned int>(std::time(nullptr)));
seeded = true;