mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-28 06:55:50 +00:00
General refactoring of beast framework classes
This commit is contained in:
@@ -86,6 +86,7 @@ public:
|
||||
instance = staticData.instance;
|
||||
if (instance == nullptr)
|
||||
{
|
||||
bassert (lifetime == SingletonLifetime::createOnDemand || ! staticData.destructorCalled);
|
||||
staticData.instance = &staticData.object;
|
||||
::new (staticData.instance) SharedSingleton (lifetime);
|
||||
instance = staticData.instance;
|
||||
@@ -107,7 +108,7 @@ private:
|
||||
{
|
||||
}
|
||||
|
||||
void performAtExit ()
|
||||
void onExit ()
|
||||
{
|
||||
if (m_lifetime == SingletonLifetime::persistAfterCreation)
|
||||
this->decReferenceCount ();
|
||||
@@ -132,6 +133,7 @@ private:
|
||||
{
|
||||
callDestructor = true;
|
||||
staticData.instance = nullptr;
|
||||
staticData.destructorCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,31 +147,16 @@ private:
|
||||
|
||||
typedef SpinLock LockType;
|
||||
|
||||
class ExitHook : public PerformedAtExit
|
||||
{
|
||||
public:
|
||||
explicit ExitHook (SharedSingleton* owner)
|
||||
: m_owner (owner)
|
||||
{
|
||||
}
|
||||
|
||||
void performAtExit ()
|
||||
{
|
||||
m_owner->performAtExit();
|
||||
}
|
||||
|
||||
private:
|
||||
SharedSingleton* m_owner;
|
||||
};
|
||||
|
||||
// This structure gets zero-filled at static initialization time.
|
||||
// No constructors are called.
|
||||
//
|
||||
struct StaticData
|
||||
class StaticData : public Uncopyable
|
||||
{
|
||||
public:
|
||||
LockType mutex;
|
||||
SharedSingleton* instance;
|
||||
SharedSingleton object;
|
||||
bool destructorCalled;
|
||||
|
||||
private:
|
||||
StaticData();
|
||||
@@ -183,9 +170,10 @@ private:
|
||||
}
|
||||
|
||||
friend class SharedPtr <SharedSingleton>;
|
||||
friend class AtExitMemberHook <SharedSingleton>;
|
||||
|
||||
SingletonLifetime::Lifetime m_lifetime;
|
||||
ExitHook m_exitHook;
|
||||
AtExitMemberHook <SharedSingleton> m_exitHook;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
34
modules/beast_core/memory/StaticObject.cpp
Normal file
34
modules/beast_core/memory/StaticObject.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
// This is here so we don't need the Thread class declaration
|
||||
void staticObjectWait (std::size_t n)
|
||||
{
|
||||
// Wait for initialization
|
||||
Thread::yield ();
|
||||
if (n > 10)
|
||||
Thread::sleep (1);
|
||||
else if (n > 100)
|
||||
Thread::sleep (10);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -112,61 +112,86 @@ private:
|
||||
template <class ObjectType, class Tag>
|
||||
char Storage <ObjectType, Tag>::s_storage [sizeof (ObjectType)];
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Provides a thread safe flag for indicating if and when
|
||||
// initialization is required for an object with static storage duration.
|
||||
//
|
||||
class Initializer
|
||||
namespace detail
|
||||
{
|
||||
|
||||
extern void staticObjectWait (std::size_t n);
|
||||
|
||||
}
|
||||
|
||||
/** Wrapper to produce an object with static storage duration.
|
||||
|
||||
The object is constructed in a thread-safe fashion when the get function
|
||||
is first called. Note that the destructor for Object is never called. To
|
||||
invoke the destructor, use the AtExitHook facility (with caution).
|
||||
|
||||
The Tag parameter allows multiple instances of the same Object type, by
|
||||
using different tags.
|
||||
|
||||
Object must meet these requirements:
|
||||
DefaultConstructible
|
||||
|
||||
@see AtExitHook
|
||||
*/
|
||||
template <class Object, typename Tag = void>
|
||||
class StaticObject
|
||||
{
|
||||
public:
|
||||
// If the condition is not initialized, the first caller will
|
||||
// receive true, while concurrent callers get blocked until
|
||||
// initialization completes.
|
||||
//
|
||||
bool beginConstruction ()
|
||||
static Object& get ()
|
||||
{
|
||||
bool needsInitialization = false;
|
||||
StaticData& staticData (StaticData::get());
|
||||
|
||||
if (m_state.get () != stateInitialized)
|
||||
if (staticData.state.get() != initialized)
|
||||
{
|
||||
if (m_state.compareAndSetBool (stateInitializing, stateUninitialized))
|
||||
if (staticData.state.compareAndSetBool (initializing, uninitialized))
|
||||
{
|
||||
needsInitialization = true;
|
||||
// Initialize the object.
|
||||
::new (&staticData.object) Object;
|
||||
staticData.state = initialized;
|
||||
}
|
||||
else
|
||||
{
|
||||
SpinDelay delay;
|
||||
|
||||
do
|
||||
for (std::size_t n = 0; staticData.state.get() != initialized; ++n)
|
||||
{
|
||||
delay.pause ();
|
||||
detail::staticObjectWait (n);
|
||||
}
|
||||
while (m_state.get () != stateInitialized);
|
||||
}
|
||||
}
|
||||
|
||||
return needsInitialization;
|
||||
}
|
||||
|
||||
// Called to signal that the initialization is complete
|
||||
//
|
||||
void endConstruction ()
|
||||
{
|
||||
m_state.set (stateInitialized);
|
||||
return staticData.object;
|
||||
}
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
stateUninitialized = 0, // must be zero
|
||||
stateInitializing,
|
||||
stateInitialized
|
||||
uninitialized = 0, // must be zero to function properly
|
||||
initializing,
|
||||
initialized
|
||||
};
|
||||
|
||||
Atomic <int> m_state;
|
||||
// This structure gets zero-filled at static initialization time.
|
||||
// No constructors are called.
|
||||
//
|
||||
class StaticData : public Uncopyable
|
||||
{
|
||||
public:
|
||||
Atomic <int> state;
|
||||
Object object;
|
||||
|
||||
static StaticData& get ()
|
||||
{
|
||||
static uint8 storage [sizeof (StaticData)];
|
||||
return *(reinterpret_cast <StaticData*> (&storage [0]));
|
||||
}
|
||||
|
||||
private:
|
||||
StaticData();
|
||||
~StaticData();
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user