Rename SharedSingleton and move files around

This commit is contained in:
Vinnie Falco
2013-06-30 03:08:15 -07:00
parent ca1eda2df1
commit 3fea8a4202
20 changed files with 121 additions and 76 deletions

View File

@@ -62,13 +62,6 @@ namespace beast
#include "threads/beast_ThreadGroup.cpp"
#include "threads/beast_ThreadWithCallQueue.cpp"
#if BEAST_WINDOWS
#include "native/beast_win32_FPUFlags.cpp"
#else
#include "native/beast_posix_FPUFlags.cpp"
#endif
}
#if BEAST_MSVC

View File

@@ -248,11 +248,9 @@ namespace beast
#include "functor/beast_Function.h"
#include "diagnostic/beast_CatchAny.h"
#include "events/beast_OncePerSecond.h"
#include "math/beast_Interval.h"
#include "math/beast_Math.h"
#include "math/beast_MurmurHash.h"
#include "memory/beast_AllocatedBy.h"
#include "memory/beast_RefCountedSingleton.h"
#include "memory/beast_PagedFreeStore.h"
#include "memory/beast_GlobalPagedFreeStore.h"
#include "memory/beast_FifoFreeStoreWithTLS.h"

View File

@@ -18,12 +18,12 @@
//==============================================================================
class OncePerSecond::TimerSingleton
: public RefCountedSingleton <OncePerSecond::TimerSingleton>
: public SharedSingleton <OncePerSecond::TimerSingleton>
, private InterruptibleThread::EntryPoint
{
private:
TimerSingleton ()
: RefCountedSingleton <OncePerSecond::TimerSingleton> (
: SharedSingleton <OncePerSecond::TimerSingleton> (
SingletonLifetime::persistAfterCreation)
, m_thread ("Once Per Second")
{

View File

@@ -1,387 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_INTERVAL_BEASTHEADER
#define BEAST_INTERVAL_BEASTHEADER
/** A half-open interval.
This represents the half-open interval [begin, end) over the scalar
type of template parameter `Ty`. It may also be considered as the
specification of a subset of a 1-dimensional Euclidean space.
@tparam Ty A scalar numerical type.
*/
template <class Ty>
class Interval
{
public:
typedef Ty value_type;
/** The empty interval.
*/
static const Interval none;
/** Create an uninitialized interval.
*/
Interval ()
{
}
/** Create an interval with the specified values.
*/
Interval (Ty begin, Ty end)
: m_begin (begin)
, m_end (end)
{
}
/** Create an interval from another interval.
*/
Interval (Interval const& other)
: m_begin (other.m_begin)
, m_end (other.m_end)
{
}
/** Assign from another interval.
@param other The interval to assign from.
@return A reference to this interval.
*/
Interval& operator= (const Interval& other)
{
m_begin = other.m_begin;
m_end = other.m_end;
return *this;
}
/** Compare an interval for equality.
Empty intervals are always equal to other empty intervals.
@param rhs The other interval to compare.
@return `true` if this interval is equal to the specified interval.
*/
bool operator== (Interval const& rhs) const
{
return (empty () && rhs.empty ()) ||
(m_begin == rhs.m_begin && m_end == rhs.m_end);
}
/** Compare an interval for inequality.
@param rhs The other interval to compare.
@return `true` if this interval is not equal to the specified interval.
*/
bool operator!= (Interval const& rhs) const
{
return !this->operator== (rhs);
}
/** Get the starting value of the interval.
@return The starting point of the interval.
*/
Ty begin () const
{
return m_begin;
}
/** Get the ending value of the interval.
@return The ending point of the interval.
*/
Ty end () const
{
return m_end;
}
/** Get the Lebesque measure.
@return The Lebesque measure.
*/
Ty length () const
{
return empty () ? Ty () : (end () - begin ());
}
//Ty count () const { return length (); } // sugar
//Ty distance () const { return length (); } // sugar
/** Determine if the interval is empty.
@return `true` if the interval is empty.
*/
bool empty () const
{
return m_begin >= m_end;
}
/** Determine if the interval is non-empty.
@return `true` if the interval is not empty.
*/
bool notEmpty () const
{
return m_begin < m_end;
}
/** Set the starting point of the interval.
@param v The starting point.
*/
void setBegin (Ty v)
{
m_begin = v;
}
/** Set the ending point of the interval.
@param v The ending point.
*/
void setEnd (Ty v)
{
m_end = v;
}
/** Set the ending point relative to the starting point.
@param v The length of the resulting interval.
*/
void setLength (Ty v)
{
m_end = m_begin + v;
}
/** Determine if a value is contained in the interval.
@param v The value to check.
@return `true` if this interval contains `v`.
*/
bool contains (Ty v) const
{
return notEmpty () && v >= m_begin && v < m_end;
}
/** Determine if this interval intersects another interval.
@param other The other interval.
@return `true` if the intervals intersect.
*/
template <class To>
bool intersects (Interval <To> const& other) const
{
return notEmpty () && other.notEmpty () &&
end () > other.begin () && begin () < other.end ();
}
/** Determine if this interval adjoins another interval.
An interval is adjoint to another interval if and only if the union of the
intervals is a single non-empty half-open subset.
@param other The other interval.
@return `true` if the intervals are adjoint.
*/
template <class To>
bool adjoins (Interval <To> const& other) const
{
return (empty () != other.empty ()) ||
(notEmpty () && end () >= other.begin ()
&& begin () <= other.end ());
}
/** Determine if this interval is disjoint from another interval.
@param other The other interval.
@return `true` if the intervals are disjoint.
*/
bool disjoint (Interval const& other) const
{
return !intersects (other);
}
/** Determine if this interval is a superset of another interval.
An interval A is a superset of interval B if B is empty or if A fully
contains B.
@param other The other interval.
@return `true` if this is a superset of `other`.
*/
template <class To>
bool superset_of (Interval <To> const& other) const
{
return other.empty () ||
(notEmpty () && begin () <= other.begin ()
&& end () >= other.end ());
}
/** Determine if this interval is a proper superset of another interval.
An interval A is a proper superset of interval B if A is a superset of
B and A is not equal to B.
@param other The other interval.
@return `true` if this interval is a proper superset of `other`.
*/
template <class To>
bool proper_superset_of (Interval <To> const& other) const
{
return this->superset_of (other) && this->operator != (other);
}
/** Determine if this interval is a subset of another interval.
@param other The other interval.
@return `true` if this interval is a subset of `other`.
*/
template <class To>
bool subset_of (Interval <To> const& other) const
{
return other.superset_of (*this);
}
/** Determine if this interval is a proper subset of another interval.
@param other The other interval.
@return `true` if this interval is a proper subset of `other`.
*/
template <class To>
bool proper_subset_of (Interval <To> const& other) const
{
return other.proper_superset_of (*this);
}
/** Return the intersection of this interval with another interval.
@param other The other interval.
@return The intersection of the intervals.
*/
template <class To>
Interval intersection (Interval <To> const& other) const
{
return Interval (std::max (begin (), other.begin ()),
std::min (end (), other.end ()));
}
/** Determine the smallest interval that contains both intervals.
@param other The other interval.
@return The simple union of the intervals.
*/
template <class To>
Interval simple_union (Interval <To> const& other) const
{
return Interval (
std::min (other.normalized ().begin (), normalized ().begin ()),
std::max (other.normalized ().end (), normalized ().end ()));
}
/** Calculate the single-interval union.
The result is empty if the union cannot be represented as a
single half-open interval.
@param other The other interval.
@return The simple union of the intervals.
*/
template <class To>
Interval single_union (Interval <To> const& other) const
{
if (empty ())
return other;
else if (other.empty ())
return *this;
else if (end () < other.begin () || begin () > other.end ())
return none;
else
return Interval (std::min (begin (), other.begin ()),
std::max (end (), other.end ()));
}
/** Determine if the interval is correctly ordered.
@return `true` if the interval is correctly ordered.
*/
bool normal () const
{
return end () >= begin ();
}
/** Return a normalized interval.
@return The normalized interval.
*/
Interval normalized () const
{
if (normal ())
return *this;
else
return Interval (end (), begin ());
}
/** Clamp a value to the interval.
@param v The value to clamp.
@return The clamped result.
*/
template <typename Tv>
Ty clamp (Tv v) const
{
// These conditionals are carefully ordered so
// that if m_begin == m_end, value is assigned m_begin.
if (v > end ())
v = end () - (std::numeric_limits <Tv>::is_integer ? 1 :
std::numeric_limits <Tv>::epsilon ());
if (v < begin ())
v = begin ();
return v;
}
private:
Ty m_begin;
Ty m_end;
};
template <typename Ty>
const Interval<Ty> Interval<Ty>::none = Interval<Ty> (Ty (), Ty ());
#endif

View File

@@ -22,7 +22,7 @@
// Original source code links in .cpp file
// This file depends on some Juce declarations and defines
// This file depends on some Beast declarations and defines
namespace Murmur
{

View File

@@ -27,7 +27,7 @@
@ingroup beast_concurrent
*/
template <class Tag>
class GlobalFifoFreeStore : public RefCountedSingleton <GlobalFifoFreeStore <Tag> >
class GlobalFifoFreeStore : public SharedSingleton <GlobalFifoFreeStore <Tag> >
{
public:
inline void* allocate (size_t bytes)
@@ -47,7 +47,7 @@ public:
private:
GlobalFifoFreeStore ()
: RefCountedSingleton <GlobalFifoFreeStore <Tag> >
: SharedSingleton <GlobalFifoFreeStore <Tag> >
(SingletonLifetime::persistAfterCreation)
{
}

View File

@@ -27,7 +27,7 @@ static const size_t globalPageBytes = 8 * 1024;
}
GlobalPagedFreeStore::GlobalPagedFreeStore ()
: RefCountedSingleton <GlobalPagedFreeStore> (SingletonLifetime::persistAfterCreation)
: SharedSingleton <GlobalPagedFreeStore> (SingletonLifetime::persistAfterCreation)
, m_allocator (globalPageBytes)
{
}

View File

@@ -27,7 +27,7 @@
@ingroup beast_concurrent
*/
class BEAST_API GlobalPagedFreeStore
: public RefCountedSingleton <GlobalPagedFreeStore>
: public SharedSingleton <GlobalPagedFreeStore>
, LeakChecked <GlobalPagedFreeStore>
{
private:

View File

@@ -1,200 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_REFERENCECOUNTEDSINGLETON_BEASTHEADER
#define BEAST_REFERENCECOUNTEDSINGLETON_BEASTHEADER
/** Thread-safe singleton which comes into existence on first use. Use this
instead of creating objects with static storage duration. These singletons
are automatically reference counted, so if you hold a pointer to it in every
object that depends on it, the order of destruction of objects is assured
to be correct.
class Object must provide the function `Object* Object::createInstance()`
@class RefCountedSingleton
@ingroup beast_core
*/
/** @{ */
class BEAST_API SingletonLifetime
{
public:
// It would be nice if we didn't have to qualify the enumeration but
// Argument Dependent Lookup is inapplicable here because:
//
// "Base classes dependent on a template parameter aren't part of lookup."
// - ville
//
/** Construction options for RefCountedSingleton
@ingroup beast_core
*/
enum Lifetime
{
/** Created on first use, destroyed when the last reference is removed.
*/
createOnDemand,
/** Like createOnDemand, but after the Singleton is destroyed an
exception will be thrown if an attempt is made to create it again.
*/
createOnDemandOnce,
/** The singleton is created on first use and persists until program exit.
*/
persistAfterCreation
};
};
//------------------------------------------------------------------------------
template <class Object>
class RefCountedSingleton
: public SingletonLifetime
, private PerformedAtExit
{
protected:
typedef SpinLock LockType;
/** Create the singleton.
@param lifetime The lifetime management option.
*/
explicit RefCountedSingleton (Lifetime const lifetime)
: m_lifetime (lifetime)
{
bassert (s_instance == nullptr);
if (m_lifetime == persistAfterCreation)
{
incReferenceCount ();
}
else if (m_lifetime == createOnDemandOnce && *s_created)
{
Throw (Error ().fail (__FILE__, __LINE__));
}
*s_created = true;
}
virtual ~RefCountedSingleton ()
{
bassert (s_instance == nullptr);
}
public:
typedef ReferenceCountedObjectPtr <Object> Ptr;
/** Retrieve a reference to the singleton.
*/
static Ptr getInstance ()
{
Ptr instance;
instance = s_instance;
if (instance == nullptr)
{
LockType::ScopedLockType lock (*s_mutex);
instance = s_instance;
if (instance == nullptr)
{
s_instance = Object::createInstance ();
instance = s_instance;
}
}
return instance;
}
inline void incReferenceCount () noexcept
{
m_refs.addref ();
}
inline void decReferenceCount () noexcept
{
if (m_refs.release ())
destroySingleton ();
}
// Caller must synchronize.
inline bool isBeingReferenced () const
{
return m_refs.isSignaled ();
}
private:
void performAtExit ()
{
if (m_lifetime == SingletonLifetime::persistAfterCreation)
decReferenceCount ();
}
void destroySingleton ()
{
bool destroy;
{
LockType::ScopedLockType lock (*s_mutex);
if (isBeingReferenced ())
{
destroy = false;
}
else
{
destroy = true;
s_instance = 0;
}
}
if (destroy)
{
delete this;
}
}
private:
Lifetime const m_lifetime;
AtomicCounter m_refs;
private:
static Object* s_instance;
static Static::Storage <LockType, RefCountedSingleton <Object> > s_mutex;
static Static::Storage <bool, RefCountedSingleton <Object> > s_created;
};
/** @{ */
template <class Object>
Object* RefCountedSingleton <Object>::s_instance;
template <class Object>
Static::Storage <typename RefCountedSingleton <Object>::LockType, RefCountedSingleton <Object> >
RefCountedSingleton <Object>::s_mutex;
template <class Object>
Static::Storage <bool, RefCountedSingleton <Object> >
RefCountedSingleton <Object>::s_created;
#endif

View File

@@ -1,29 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
//#pragma message(BEAST_FILEANDLINE_ "Missing platform-specific implementation")
FPUFlags FPUFlags::getCurrent ()
{
return FPUFlags ();
}
void FPUFlags::setCurrent (const FPUFlags& flags)
{
}

View File

@@ -1,176 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
FPUFlags FPUFlags::getCurrent ()
{
unsigned int currentControl;
const unsigned int newControl = 0;
const unsigned int mask = 0;
errno_t result = _controlfp_s (&currentControl, newControl, mask);
if (result != 0)
Throw (std::runtime_error ("error in _controlfp_s"));
FPUFlags flags;
flags.setMaskNaNs ((currentControl & _EM_INVALID) == _EM_INVALID);
flags.setMaskDenormals ((currentControl & _EM_DENORMAL) == _EM_DENORMAL);
flags.setMaskZeroDivides ((currentControl & _EM_ZERODIVIDE) == _EM_ZERODIVIDE);
flags.setMaskOverflows ((currentControl & _EM_OVERFLOW) == _EM_OVERFLOW);
flags.setMaskUnderflows ((currentControl & _EM_UNDERFLOW) == _EM_UNDERFLOW);
//flags.setMaskInexacts ((currentControl & _EM_INEXACT) == _EM_INEXACT);
flags.setFlushDenormals ((currentControl & _DN_FLUSH) == _DN_FLUSH);
flags.setInfinitySigned ((currentControl & _IC_AFFINE) == _IC_AFFINE);
Rounding rounding = roundDown;
switch (currentControl & _MCW_RC)
{
case _RC_CHOP:
rounding = roundChop;
break;
case _RC_UP:
rounding = roundUp;
break;
case _RC_DOWN:
rounding = roundDown;
break;
case _RC_NEAR:
rounding = roundNear;
break;
default:
Throw (std::runtime_error ("unknown rounding in _controlfp_s"));
};
flags.setRounding (rounding);
Precision precision = bits64;
switch (currentControl & _MCW_PC )
{
case _PC_64:
precision = bits64;
break;
case _PC_53:
precision = bits53;
break;
case _PC_24:
precision = bits24;
break;
default:
Throw (std::runtime_error ("unknown precision in _controlfp_s"));
};
flags.setPrecision (precision);
return flags;
}
static void setControl (const FPUFlags::Flag& flag,
unsigned int& newControl,
unsigned int& mask,
unsigned int constant)
{
if (flag.is_set ())
{
mask |= constant;
if (flag.value ())
newControl |= constant;
}
}
void FPUFlags::setCurrent (const FPUFlags& flags)
{
unsigned int newControl = 0;
unsigned int mask = 0;
setControl (flags.getMaskNaNs (), newControl, mask, _EM_INVALID);
setControl (flags.getMaskDenormals (), newControl, mask, _EM_DENORMAL);
setControl (flags.getMaskZeroDivides (), newControl, mask, _EM_ZERODIVIDE);
setControl (flags.getMaskOverflows (), newControl, mask, _EM_OVERFLOW);
setControl (flags.getMaskUnderflows (), newControl, mask, _EM_UNDERFLOW);
//setControl (flags.getMaskInexacts(), newControl, mask, _EM_INEXACT);
setControl (flags.getFlushDenormals (), newControl, mask, _DN_FLUSH);
setControl (flags.getInfinitySigned (), newControl, mask, _IC_AFFINE);
if (flags.getRounding ().is_set ())
{
Rounding rounding = flags.getRounding ().value ();
switch (rounding)
{
case roundChop:
mask |= _MCW_RC;
newControl |= _RC_CHOP;
break;
case roundUp:
mask |= _MCW_RC;
newControl |= _RC_UP;
break;
case roundDown:
mask |= _MCW_RC;
newControl |= _RC_DOWN;
break;
case roundNear:
mask |= _MCW_RC;
newControl |= _RC_NEAR;
break;
}
}
if (flags.getPrecision ().is_set ())
{
switch (flags.getPrecision ().value ())
{
case bits64:
mask |= _MCW_PC;
newControl |= _PC_64;
break;
case bits53:
mask |= _MCW_PC;
newControl |= _PC_53;
break;
case bits24:
mask |= _MCW_PC;
newControl |= _PC_24;
break;
}
}
unsigned int currentControl;
errno_t result = _controlfp_s (&currentControl, newControl, mask);
if (result != 0)
Throw (std::runtime_error ("error in _controlfp_s"));
}

View File

@@ -29,13 +29,13 @@
@ingroup beast_concurrent
*/
class BEAST_API GlobalThreadGroup : public ThreadGroup,
public RefCountedSingleton <GlobalThreadGroup>
public SharedSingleton <GlobalThreadGroup>
{
private:
friend class RefCountedSingleton <GlobalThreadGroup>;
friend class SharedSingleton <GlobalThreadGroup>;
GlobalThreadGroup ()
: RefCountedSingleton <GlobalThreadGroup> (
: SharedSingleton <GlobalThreadGroup> (
SingletonLifetime::persistAfterCreation)
{
}