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

@@ -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