1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright (c) 1999
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/STL_Extension/include/CGAL/Handle.h $
// $Id: include/CGAL/Handle.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Sylvain Pion
#ifndef CGAL_HANDLE_H
#define CGAL_HANDLE_H
#include <cstddef>
#include <cstdint>
#include <atomic>
#include <CGAL/Handle_for.h>
#include <CGAL/assertions.h>
namespace CGAL {
class Rep
{
friend class Handle;
protected:
Rep(int count = 1)
: count(count)
{}
virtual ~Rep() {}
std::atomic_int count;
};
class Handle
{
public:
typedef std::ptrdiff_t Id_type ;
Handle() noexcept
: PTR(static_cast<Rep*>(0)) {}
Handle(const Handle& x) noexcept(!(CGAL_PRECONDITIONS_ENABLED || CGAL_ASSERTIONS_ENABLED))
{
CGAL_precondition( x.PTR != static_cast<Rep*>(0) );
PTR = x.PTR;
//CGAL_assume (PTR->count > 0);
incref();
}
Handle(Handle&& x) noexcept : PTR(x.PTR) { x.PTR = 0; }
~Handle() { reset(); }
Handle&
operator=(const Handle& x) noexcept(!CGAL_PRECONDITIONS_ENABLED)
{
CGAL_precondition( x.PTR != static_cast<Rep*>(0) );
x.incref();
if(PTR) decref(); // not reset() in case this==&x
PTR = x.PTR;
return *this;
}
Handle&
operator=(Handle&& x) noexcept
{
swap(*this, x);
return *this;
}
friend void swap(Handle& a, Handle& b) noexcept { std::swap(a.PTR, b.PTR); }
private:
void incref() const noexcept
{
if (is_currently_single_threaded()) {
PTR->count.store(PTR->count.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
} else {
PTR->count.fetch_add(1, std::memory_order_relaxed);
}
}
void decref()
{
if (is_currently_single_threaded()) {
auto c = PTR->count.load(std::memory_order_relaxed);
if (c == 1)
delete PTR;
else
PTR->count.store(c - 1, std::memory_order_relaxed);
} else {
// TSAN does not support fences :-(
#if !defined __SANITIZE_THREAD__ && !__has_feature(thread_sanitizer)
if (PTR->count.load(std::memory_order_relaxed) == 1
|| PTR->count.fetch_sub(1, std::memory_order_release) == 1) {
std::atomic_thread_fence(std::memory_order_acquire);
#else
if (PTR->count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
#endif
delete PTR;
}
}
}
public:
void reset()
{
if (PTR)
{
decref();
PTR=0;
}
}
int
refs() const noexcept { return PTR->count.load(std::memory_order_relaxed); }
Id_type id() const noexcept { return static_cast<Id_type>(reinterpret_cast<std::intptr_t>(static_cast<void*>(PTR)) / sizeof(Rep)); }
bool identical(const Handle& h) const noexcept { return PTR == h.PTR; }
void * for_compact_container() const { return PTR; }
void for_compact_container(void* p) { PTR = static_cast<Rep*>(p); }
protected:
Rep* PTR;
};
//inline Handle::Id_type id(const Handle& x) { return x.id() ; }
inline bool identical(const Handle &h1, const Handle &h2) noexcept { return h1.identical(h2); }
} //namespace CGAL
#endif // CGAL_HANDLE_H
|