Skip to content

Commit 23526ba

Browse files
authored
Fix race condition in TreeSupportBaseCircle which may sometimes cause a crash (#2210)
2 parents 2630cc4 + 58589a8 commit 23526ba

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

include/TreeSupportBaseCircle.h

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,27 @@ class TreeSupportBaseCircle
1818
{
1919
protected:
2020
inline static Polygon base_circle;
21-
inline static bool circle_generated = false;
21+
inline static std::once_flag flag;
2222

2323
public:
2424
inline static constexpr int64_t base_radius = 50;
2525

2626
static Polygon getBaseCircle()
2727
{
28-
if (circle_generated)
29-
{
30-
return base_circle;
31-
}
32-
else
33-
{
34-
std::mutex critical_sections;
35-
std::lock_guard<std::mutex> critical_section_progress(critical_sections);
36-
if (circle_generated)
28+
std::call_once(
29+
flag,
30+
[&]()
3731
{
38-
return base_circle;
39-
}
40-
41-
constexpr auto support_tree_circle_resolution = 12; // The number of vertices in each circle.
42-
Polygon circle;
43-
for (const uint64_t i : ranges::views::iota(0, support_tree_circle_resolution))
44-
{
45-
const AngleRadians angle = static_cast<double>(i) / support_tree_circle_resolution * TAU;
46-
circle.emplace_back(static_cast<coord_t>(std::cos(angle) * base_radius), static_cast<coord_t>(std::sin(angle) * base_radius));
47-
}
48-
base_circle = Polygon(circle);
49-
circle_generated = true;
50-
return base_circle;
51-
}
32+
constexpr auto support_tree_circle_resolution = 12; // The number of vertices in each circle.
33+
Polygon circle;
34+
for (const uint64_t i : ranges::views::iota(0, support_tree_circle_resolution))
35+
{
36+
const AngleRadians angle = static_cast<double>(i) / support_tree_circle_resolution * TAU;
37+
circle.emplace_back(static_cast<coord_t>(std::cos(angle) * base_radius), static_cast<coord_t>(std::sin(angle) * base_radius));
38+
}
39+
base_circle = Polygon(circle);
40+
});
41+
return base_circle;
5242
}
5343
};
5444

0 commit comments

Comments
 (0)