From: Ben Maurer Date: Mon, 28 Sep 2015 17:58:44 +0000 (-0700) Subject: Easy SIOF-proofing X-Git-Tag: deprecate-dynamic-initializer~375 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=739bdb1ca0e3e1218d4571e15ec72e1dcfd3eb36;p=folly.git Easy SIOF-proofing Summary: These classes are likely to be used as static variables and can easily be constructed with constexpr. One that we really ought to fix is SpinLock. Sadly we have a bunch of implementations of it some of which need initialization. Reviewed By: @meyering Differential Revision: D2459355 --- diff --git a/folly/Baton.h b/folly/Baton.h index 7922ffd0..6737efc5 100644 --- a/folly/Baton.h +++ b/folly/Baton.h @@ -19,7 +19,6 @@ #include #include -#include #include #include @@ -44,8 +43,11 @@ namespace folly { /// a much more restrictive lifecycle we can also add a bunch of assertions /// that can help to catch race conditions ahead of time. template class Atom = std::atomic> -struct Baton : boost::noncopyable { - Baton() : state_(INIT) {} +struct Baton { + constexpr Baton() : state_(INIT) {} + + Baton(Baton const&) = delete; + Baton& operator=(Baton const&) = delete; /// It is an error to destroy a Baton on which a thread is currently /// wait()ing. In practice this means that the waiter usually takes diff --git a/folly/LifoSem.h b/folly/LifoSem.h index 24b37c8f..b0af517c 100644 --- a/folly/LifoSem.h +++ b/folly/LifoSem.h @@ -313,12 +313,15 @@ class LifoSemHead { /// See LifoSemNode for more information on how to make your own. template class Atom = std::atomic> -struct LifoSemBase : boost::noncopyable { +struct LifoSemBase { /// Constructor - explicit LifoSemBase(uint32_t initialValue = 0) + constexpr explicit LifoSemBase(uint32_t initialValue = 0) : head_(LifoSemHead::fresh(initialValue)) {} + LifoSemBase(LifoSemBase const&) = delete; + LifoSemBase& operator=(LifoSemBase const&) = delete; + /// Silently saturates if value is already 2^32-1 void post() { auto idx = incrOrPop(1); @@ -591,7 +594,7 @@ struct LifoSemBase : boost::noncopyable { template class Atom, class BatonType> struct LifoSemImpl : public detail::LifoSemBase { - explicit LifoSemImpl(uint32_t v = 0) + constexpr explicit LifoSemImpl(uint32_t v = 0) : detail::LifoSemBase(v) {} }; diff --git a/folly/RWSpinLock.h b/folly/RWSpinLock.h index 09914e8a..a054d51b 100644 --- a/folly/RWSpinLock.h +++ b/folly/RWSpinLock.h @@ -141,7 +141,6 @@ pthread_rwlock_t Read 728698 24us 101ns 7.28ms 194us #include #include #include -#include #include #include @@ -165,10 +164,13 @@ namespace folly { * UpgradeLockable concepts except the TimedLockable related locking/unlocking * interfaces. */ -class RWSpinLock : boost::noncopyable { +class RWSpinLock { enum : int32_t { READER = 4, UPGRADED = 2, WRITER = 1 }; public: - RWSpinLock() : bits_(0) {} + constexpr RWSpinLock() : bits_(0) {} + + RWSpinLock(RWSpinLock const&) = delete; + RWSpinLock& operator=(RWSpinLock const&) = delete; // Lockable Concept void lock() { @@ -505,7 +507,7 @@ struct RWTicketIntTrait<32> { template -class RWTicketSpinLockT : boost::noncopyable { +class RWTicketSpinLockT { typedef detail::RWTicketIntTrait IntTraitType; typedef typename detail::RWTicketIntTrait::FullInt FullInt; typedef typename detail::RWTicketIntTrait::HalfInt HalfInt; @@ -513,6 +515,7 @@ class RWTicketSpinLockT : boost::noncopyable { QuarterInt; union RWTicket { + constexpr RWTicket() : whole(0) {} FullInt whole; HalfInt readWrite; __extension__ struct { @@ -537,9 +540,10 @@ class RWTicketSpinLockT : boost::noncopyable { public: - RWTicketSpinLockT() { - store_release(&ticket.whole, FullInt(0)); - } + constexpr RWTicketSpinLockT() {} + + RWTicketSpinLockT(RWTicketSpinLockT const&) = delete; + RWTicketSpinLockT& operator=(RWTicketSpinLockT const&) = delete; void lock() { if (kFavorWriter) { diff --git a/folly/SharedMutex.h b/folly/SharedMutex.h index 796323dd..ebe8d794 100644 --- a/folly/SharedMutex.h +++ b/folly/SharedMutex.h @@ -237,7 +237,7 @@ class SharedMutexImpl { class UpgradeHolder; class WriteHolder; - SharedMutexImpl() : state_(0) {} + constexpr SharedMutexImpl() : state_(0) {} SharedMutexImpl(const SharedMutexImpl&) = delete; SharedMutexImpl(SharedMutexImpl&&) = delete;