Commit 61276a38 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Do conditional compilation for MPMCPipeline using C++

Summary: [Folly] Do conditional compilation for `MPMCPipeline` using C++ via selecting a base class v.s. using the preprocessor.

Reviewed By: Orvid

Differential Revision: D9625568

fbshipit-source-id: 2e0d7195bb7fa4f44cf8b99ed3d7564b18393b73
parent 3acfb909
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/Portability.h>
#include <folly/detail/MPMCPipelineDetail.h> #include <folly/detail/MPMCPipelineDetail.h>
namespace folly { namespace folly {
...@@ -101,41 +102,53 @@ template <class In, class... Stages> class MPMCPipeline { ...@@ -101,41 +102,53 @@ template <class In, class... Stages> class MPMCPipeline {
static constexpr size_t kAmplification = static constexpr size_t kAmplification =
detail::AmplificationProduct<StageInfos>::value; detail::AmplificationProduct<StageInfos>::value;
class TicketBaseDebug {
public:
TicketBaseDebug() noexcept : owner_(nullptr), value_(0xdeadbeeffaceb00c) {}
TicketBaseDebug(TicketBaseDebug&& other) noexcept
: owner_(std::exchange(other.owner_, nullptr)),
value_(std::exchange(other.value_, 0xdeadbeeffaceb00c)) {}
explicit TicketBaseDebug(MPMCPipeline* owner, uint64_t value) noexcept
: owner_(owner), value_(value) {}
void check_owner(MPMCPipeline* owner) const {
CHECK(owner == owner_);
}
MPMCPipeline* owner_;
uint64_t value_;
};
class TicketBaseNDebug {
public:
TicketBaseNDebug() = default;
TicketBaseNDebug(TicketBaseNDebug&&) = default;
explicit TicketBaseNDebug(MPMCPipeline*, uint64_t value) noexcept
: value_(value) {}
void check_owner(MPMCPipeline*) const {}
uint64_t value_;
};
using TicketBase =
std::conditional_t<kIsDebug, TicketBaseDebug, TicketBaseNDebug>;
public: public:
/** /**
* Ticket, returned by blockingReadStage, must be given back to * Ticket, returned by blockingReadStage, must be given back to
* blockingWriteStage. Tickets are not thread-safe. * blockingWriteStage. Tickets are not thread-safe.
*/ */
template <size_t Stage> template <size_t Stage>
class Ticket { class Ticket : TicketBase {
public: public:
~Ticket() noexcept { ~Ticket() noexcept {
CHECK_EQ(remainingUses_, 0) << "All tickets must be completely used!"; CHECK_EQ(remainingUses_, 0) << "All tickets must be completely used!";
} }
#ifndef NDEBUG Ticket() noexcept : remainingUses_(0) {}
Ticket() noexcept
: owner_(nullptr),
remainingUses_(0),
value_(0xdeadbeeffaceb00c) {
}
#else
Ticket() noexcept : remainingUses_(0) { }
#endif
Ticket(Ticket&& other) noexcept Ticket(Ticket&& other) noexcept
: : TicketBase(static_cast<TicketBase&&>(other)),
#ifndef NDEBUG remainingUses_(std::exchange(other.remainingUses_, 0)) {}
owner_(other.owner_),
#endif
remainingUses_(other.remainingUses_),
value_(other.value_) {
other.remainingUses_ = 0;
#ifndef NDEBUG
other.owner_ = nullptr;
other.value_ = 0xdeadbeeffaceb00c;
#endif
}
Ticket& operator=(Ticket&& other) noexcept { Ticket& operator=(Ticket&& other) noexcept {
if (this != &other) { if (this != &other) {
...@@ -147,31 +160,16 @@ template <class In, class... Stages> class MPMCPipeline { ...@@ -147,31 +160,16 @@ template <class In, class... Stages> class MPMCPipeline {
private: private:
friend class MPMCPipeline; friend class MPMCPipeline;
#ifndef NDEBUG
MPMCPipeline* owner_;
#endif
size_t remainingUses_; size_t remainingUses_;
uint64_t value_;
Ticket(MPMCPipeline* owner, size_t amplification, uint64_t value) noexcept Ticket(MPMCPipeline* owner, size_t amplification, uint64_t value) noexcept
: : TicketBase(owner, value * amplification),
#ifndef NDEBUG remainingUses_(amplification) {}
owner_(owner),
#endif
remainingUses_(amplification),
value_(value * amplification) {
(void)owner; // -Wunused-parameter
}
uint64_t use(MPMCPipeline* owner) { uint64_t use(MPMCPipeline* owner) {
CHECK_GT(remainingUses_--, 0); CHECK_GT(remainingUses_--, 0);
#ifndef NDEBUG TicketBase::check_owner(owner);
CHECK(owner == owner_); return TicketBase::value_++;
#else
(void)owner; // -Wunused-parameter
#endif
return value_++;
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment