Skip to content

Commit 0214c85

Browse files
committed
Add a mutex in <CGAL/Handle.h> as well
1 parent 6970e1c commit 0214c85

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

STL_Extension/include/CGAL/Handle.h

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ class Rep
3535
{
3636
friend class Handle;
3737
protected:
38-
Rep() { count = 1; }
38+
Rep() : count(1) {}
3939
virtual ~Rep() {}
4040

41-
int count;
41+
#if defined(CGAL_HANDLE_FOR_USE_ATOMIC) && ! defined(CGAL_NO_ATOMIC)
42+
CGAL::cpp11::atomic<unsigned int> count;
43+
#elif CGAL_HANDLE_FOR_USE_BOOST_ATOMIC_COUNTER
44+
boost::detail::atomic_count count;
45+
#else // no atomic
46+
unsigned int count;
47+
#endif // no atomic
48+
4249
};
4350

4451
class Handle
@@ -54,22 +61,50 @@ class Handle
5461
{
5562
CGAL_precondition( x.PTR != static_cast<Rep*>(0) );
5663
PTR = x.PTR;
57-
PTR->count++;
64+
#if defined(CGAL_HANDLE_FOR_USE_ATOMIC) || ! defined(CGAL_NO_ATOMIC)
65+
PTR->count.fetch_add(1, CGAL::cpp11::memory_order_relaxed);
66+
#else // not CGAL::cpp11::atomic
67+
++(PTR->count);
68+
#endif // not CGAL::cpp11::atomic
5869
}
5970

6071
~Handle()
6172
{
62-
if ( PTR && (--PTR->count == 0))
63-
delete PTR;
73+
if ( PTR ) {
74+
#if defined(CGAL_HANDLE_FOR_USE_ATOMIC) || ! defined(CGAL_NO_ATOMIC)
75+
if (PTR->count.fetch_sub(1, CGAL::cpp11::memory_order_release) == 1) {
76+
CGAL::cpp11::atomic_thread_fence(CGAL::cpp11::memory_order_acquire);
77+
delete PTR;
78+
}
79+
#else // not CGAL::cpp11::atomic
80+
if (--(PTR->count) == 0) {
81+
delete PTR;
82+
}
83+
#endif // not CGAL::cpp11::atomic
84+
}
6485
}
6586

6687
Handle&
6788
operator=(const Handle& x)
6889
{
6990
CGAL_precondition( x.PTR != static_cast<Rep*>(0) );
70-
x.PTR->count++;
71-
if ( PTR && (--PTR->count == 0))
72-
delete PTR;
91+
#if defined(CGAL_HANDLE_FOR_USE_ATOMIC) || ! defined(CGAL_NO_ATOMIC)
92+
x.PTR->count.fetch_add(1, CGAL::cpp11::memory_order_relaxed);
93+
#else // not CGAL::cpp11::atomic
94+
++(x.PTR->count);
95+
#endif // not CGAL::cpp11::atomic
96+
if ( PTR ) {
97+
#if defined(CGAL_HANDLE_FOR_USE_ATOMIC) || ! defined(CGAL_NO_ATOMIC)
98+
if (PTR->count.fetch_sub(1, CGAL::cpp11::memory_order_release) == 1) {
99+
CGAL::cpp11::atomic_thread_fence(CGAL::cpp11::memory_order_acquire);
100+
delete PTR;
101+
}
102+
#else // not CGAL::cpp11::atomic
103+
if (--(PTR->count) == 0) {
104+
delete PTR;
105+
}
106+
#endif // not CGAL::cpp11::atomic
107+
}
73108
PTR = x.PTR;
74109
return *this;
75110
}

0 commit comments

Comments
 (0)