Skip to content

Commit

Permalink
Use std::tuple for empty base class optmiziation in rcu_obj_base
Browse files Browse the repository at this point in the history
This is what GCC's implementation of unique_ptr does. It makes template
specialization for deleter unnecessary.

Signed-off-by: Lance Roy <[email protected]>
  • Loading branch information
ldr709 committed Aug 2, 2017
1 parent c82ce9e commit 09a5eca
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions Test/paulmck/rcu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,26 @@
#include <cstddef>
#include <memory>
#include <mutex>
#include <tuple>
#include <utility>

// Derived-type approach. All RCU-protected data structures using this
// approach must derive from std::rcu_obj_base, which in turn derives
// from std::rcu_head. No idea what happens in case of multiple inheritance.

namespace std {
template<typename T, typename D = default_delete<T>, bool E = is_empty<D>::value>
class rcu_obj_base: private rcu_head {
D deleter;
template<typename T, typename D = default_delete<T>>
class rcu_obj_base: private rcu_head, private std::tuple<D> {
public:
void retire(D d = {})
{
deleter = std::move(d);
::call_rcu(
static_cast<rcu_head *>(this),
[](rcu_head *rhp) {
auto rhdp = static_cast<rcu_obj_base *>(rhp);
auto obj = static_cast<T *>(rhdp);
rhdp->deleter(obj);
}
);
}
};


// Specialization for when D is an empty type.

template<typename T, typename D>
class rcu_obj_base<T,D,true>: private rcu_head {
public:
void retire(D = {})
{
std::get<0>(*this) = std::move(d);
::call_rcu(
static_cast<rcu_head *>(this),
[](rcu_head *rhp) {
auto rhdp = static_cast<rcu_obj_base *>(rhp);
auto obj = static_cast<T *>(rhdp);
D()(obj);
std::get<0>(*rhdp)(obj);
}
);
}
Expand Down

0 comments on commit 09a5eca

Please sign in to comment.