Add Stoppable prepare and start interfaces

This commit is contained in:
Vinnie Falco
2013-09-28 15:00:35 -07:00
parent d37dd46f65
commit f07515eb88
18 changed files with 395 additions and 258 deletions

View File

@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup Label="UserMacros">
<RepoDir>..\..</RepoDir>
</PropertyGroup>
<PropertyGroup />
<ItemDefinitionGroup>
<ClCompile>
@@ -9,8 +11,12 @@
<PreprocessorDefinitions>BEAST_COMPILING_STATIC_LIBARARY=1;_CRTDBG_MAP_ALLOC;_WIN32_WINNT=0x0600;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(RepoDir)\config;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
<ItemGroup>
<BuildMacro Include="RepoDir">
<Value>$(RepoDir)</Value>
</BuildMacro>
</ItemGroup>
</Project>

View File

@@ -78,6 +78,7 @@
</None>
<None Include="..\..\modules\beast_core\zip\zlib\README" />
<None Include="..\..\README.md" />
<None Include="..\..\scripts\compile.sh" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\beast\Arithmetic.h" />
@@ -119,6 +120,7 @@
<ClInclude Include="..\..\beast\mpl\RemoveVolatile.h" />
<ClInclude Include="..\..\beast\Net.h" />
<ClInclude Include="..\..\beast\net\IPEndpoint.h" />
<ClInclude Include="..\..\beast\SafeBool.h" />
<ClInclude Include="..\..\beast\StaticAssert.h" />
<ClInclude Include="..\..\beast\Strings.h" />
<ClInclude Include="..\..\beast\strings\CharacterFunctions.h" />
@@ -143,9 +145,12 @@
<ClInclude Include="..\..\beast\type_traits\RemoveSigned.h" />
<ClInclude Include="..\..\beast\Uncopyable.h" />
<ClInclude Include="..\..\beast\Utility.h" />
<ClInclude Include="..\..\beast\utility\Debug.h" />
<ClInclude Include="..\..\beast\utility\EnableIf.h" />
<ClInclude Include="..\..\beast\utility\Error.h" />
<ClInclude Include="..\..\beast\utility\Journal.h" />
<ClInclude Include="..\..\beast\Version.h" />
<ClInclude Include="..\..\config\BeastConfig.h" />
<ClInclude Include="..\..\modules\beast_asio\async\AbstractHandler.h" />
<ClInclude Include="..\..\modules\beast_asio\async\AsyncObject.h" />
<ClInclude Include="..\..\modules\beast_asio\async\ComposedAsyncOperation.h" />
@@ -217,13 +222,10 @@
<ClInclude Include="..\..\modules\beast_core\containers\SparseSet.h" />
<ClInclude Include="..\..\modules\beast_core\containers\DynamicList.h" />
<ClInclude Include="..\..\modules\beast_core\containers\Variant.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\Debug.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\Error.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\FatalError.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\ProtectedCall.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\FPUFlags.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\LeakChecked.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\SafeBool.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\SemanticVersion.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\Throw.h" />
<ClInclude Include="..\..\modules\beast_core\diagnostic\UnitTest.h" />
@@ -392,7 +394,6 @@
<ClInclude Include="..\..\modules\beast_sqlite\beast_sqlite.h" />
<ClInclude Include="..\..\modules\beast_sqlite\sqlite\sqlite3.h" />
<ClInclude Include="..\..\modules\beast_sqlite\sqlite\sqlite3ext.h" />
<ClInclude Include="BeastConfig.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\beast\chrono\Chrono.cpp" />
@@ -490,6 +491,18 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\strings\Strings.cpp" />
<ClCompile Include="..\..\beast\utility\impl\Debug.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\utility\impl\Error.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\utility\impl\Journal.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -701,18 +714,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\diagnostic\Debug.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\diagnostic\Error.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\diagnostic\FatalError.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

View File

@@ -73,6 +73,9 @@
<None Include="..\..\beast\crypto\impl\sha2\sha2test.pl">
<Filter>beast\crypto\impl\sha2</Filter>
</None>
<None Include="..\..\scripts\compile.sh">
<Filter>scripts</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Filter Include="beast_core">
@@ -270,6 +273,9 @@
<Filter Include="beast\chrono\impl">
<UniqueIdentifier>{39886e0f-1607-4b7a-81cf-011d83dadee3}</UniqueIdentifier>
</Filter>
<Filter Include="scripts">
<UniqueIdentifier>{c65af439-8c23-46c3-9b95-7da15651e5f6}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
@@ -578,21 +584,12 @@
<ClInclude Include="..\..\modules\beast_core\containers\SortedLookupTable.h">
<Filter>beast_core\containers</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\diagnostic\Debug.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\diagnostic\Error.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\diagnostic\FPUFlags.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\diagnostic\LeakChecked.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\diagnostic\SafeBool.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\diagnostic\Throw.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
@@ -1178,7 +1175,6 @@
<ClInclude Include="..\..\beast\http\ParsedURL.h">
<Filter>beast\http</Filter>
</ClInclude>
<ClInclude Include="BeastConfig.h" />
<ClInclude Include="..\..\beast\FixedArray.h">
<Filter>beast</Filter>
</ClInclude>
@@ -1209,6 +1205,16 @@
<ClInclude Include="..\..\beast\chrono\RelativeTime.h">
<Filter>beast\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\SafeBool.h">
<Filter>beast</Filter>
</ClInclude>
<ClInclude Include="..\..\config\BeastConfig.h" />
<ClInclude Include="..\..\beast\utility\Error.h">
<Filter>beast\utility</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\utility\Debug.h">
<Filter>beast\utility</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
@@ -1451,12 +1457,6 @@
<ClCompile Include="..\..\modules\beast_core\native\bsd_Threads.cpp">
<Filter>beast_core\native</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\diagnostic\Debug.cpp">
<Filter>beast_core\diagnostic</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\diagnostic\Error.cpp">
<Filter>beast_core\diagnostic</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\diagnostic\FPUFlags.cpp">
<Filter>beast_core\diagnostic</Filter>
</ClCompile>
@@ -1781,6 +1781,12 @@
<ClCompile Include="..\..\beast\chrono\Chrono.cpp">
<Filter>beast\chrono</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\utility\impl\Debug.cpp">
<Filter>beast\utility\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\utility\impl\Error.cpp">
<Filter>beast\utility\impl</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt">

View File

@@ -34,7 +34,7 @@ namespace beast {
/** Contains static methods for converting the byte order between different
endiannesses.
*/
class BEAST_API ByteOrder : public Uncopyable
class ByteOrder : public Uncopyable
{
public:
//==============================================================================

View File

@@ -20,23 +20,11 @@
#ifndef BEAST_SAFEBOOL_H_INCLUDED
#define BEAST_SAFEBOOL_H_INCLUDED
/** Safe evaluation of class as `bool`.
namespace beast {
This allows a class to be safely evaluated as a bool without the usual
harmful side effects of the straightforward operator conversion approach.
To use it, derive your class from SafeBool and implement `asBoolean()` as:
namespace detail {
@code
bool asBoolean () const;
@endcode
Ideas from http://www.artima.com/cppsource/safebool.html
@class SafeBool
*/
class BEAST_API SafeBoolBase
class SafeBoolBase
{
private:
void disallowed () const { }
@@ -56,11 +44,29 @@ protected:
~SafeBoolBase () { }
};
}
/** Safe evaluation of class as `bool`.
This allows a class to be safely evaluated as a bool without the usual
harmful side effects of the straightforward operator conversion approach.
To use it, derive your class from SafeBool and implement `asBoolean()` as:
@code
bool asBoolean () const;
@endcode
Ideas from http://www.artima.com/cppsource/safebool.html
@class SafeBool
*/
template <typename T = void>
class SafeBool : public SafeBoolBase
class SafeBool : public detail::SafeBoolBase
{
public:
operator boolean_t () const
operator detail::SafeBoolBase::boolean_t () const
{
return (static_cast <T const*> (this))->asBoolean ()
? &SafeBoolBase::allowed : 0;
@@ -82,6 +88,8 @@ void operator!= (SafeBool <T> const& lhs, SafeBool <U> const& rhs)
lhs.disallowed ();
}
}
#endif

View File

@@ -20,7 +20,9 @@
#ifndef BEAST_UTILITY_H_INCLUDED
#define BEAST_UTILITY_H_INCLUDED
#include "utility/Debug.h"
#include "utility/EnableIf.h"
#include "utility/Error.h"
#include "utility/Journal.h"
#endif

View File

@@ -21,7 +21,7 @@
*/
//==============================================================================
#include "RelativeTime.h"
#include "../RelativeTime.h"
// VFALCO TODO Migrate the localizable strings interfaces for this file

View File

@@ -17,8 +17,12 @@
*/
//==============================================================================
#ifndef BEAST_DEBUG_H_INCLUDED
#define BEAST_DEBUG_H_INCLUDED
#ifndef BEAST_UTILITY_DEBUG_H_INCLUDED
#define BEAST_UTILITY_DEBUG_H_INCLUDED
#include "../strings/String.h"
namespace beast {
// Auxiliary outines for debugging
@@ -83,4 +87,6 @@ extern void checkHeap ();
}
}
#endif

View File

@@ -17,8 +17,17 @@
*/
//==============================================================================
#ifndef BEAST_ERROR_H_INCLUDED
#define BEAST_ERROR_H_INCLUDED
#ifndef BEAST_UTILITY_ERROR_H_INCLUDED
#define BEAST_UTILITY_ERROR_H_INCLUDED
#include "../Config.h"
#include "../SafeBool.h"
#include "../strings/String.h"
#include <stdexcept>
namespace beast {
/** A concise error report.
@@ -32,7 +41,7 @@
@ingroup beast_core
*/
class BEAST_API Error
class Error
: public std::exception
, public SafeBool <Error>
{
@@ -119,4 +128,6 @@ private:
mutable char const* m_szWhat;
};
}
#endif

View File

@@ -19,6 +19,10 @@
#include "BeastConfig.h"
#include "../../modules/beast_core/beast_core.h"
#include "impl/Error.cpp"
// For Journal and Debug
#include "../../modules/beast_core/beast_core.h"
#include "impl/Journal.cpp"
#include "impl/Debug.cpp"

View File

@@ -17,8 +17,9 @@
*/
//==============================================================================
namespace Debug
{
namespace beast {
namespace Debug {
void breakPoint ()
{
@@ -358,3 +359,5 @@ public:
};
static DebugTests debugTests;
}

View File

@@ -17,6 +17,19 @@
*/
//==============================================================================
#include "../Error.h"
#include "../Debug.h"
#include <ostream>
// VFALCO TODO Localizable strings
#ifndef TRANS
#define TRANS(s) (s)
#define UNDEF_TRANS
#endif
namespace beast {
Error::Error ()
: m_code (success)
, m_lineNumber (0)
@@ -241,3 +254,11 @@ String const Error::getReasonTextForCode (Code code)
return s;
}
#ifdef UNDEF_TRANS
#undef TRANs
#undef UNDEF_TRANS
#endif
}

View File

@@ -138,8 +138,6 @@ namespace beast
#include "containers/DynamicList.cpp"
#include "containers/HashMap.cpp"
#include "diagnostic/Debug.cpp"
#include "diagnostic/Error.cpp"
#include "diagnostic/FatalError.cpp"
#include "diagnostic/FPUFlags.cpp"
#include "diagnostic/LeakChecked.cpp"

View File

@@ -54,6 +54,7 @@
#include "../../beast/Memory.h"
#include "../../beast/Intrusive.h"
#include "../../beast/Net.h"
#include "../../beast/SafeBool.h"
#include "../../beast/Strings.h"
#include "../../beast/TypeTraits.h"
#include "../../beast/Thread.h"
@@ -146,7 +147,6 @@ class FileOutputStream;
#include "memory/MemoryAlignment.h"
#include "memory/CacheLine.h"
#include "threads/ReadWriteMutex.h"
#include "diagnostic/SafeBool.h"
#include "threads/WaitableEvent.h"
#include "threads/Thread.h"
#include "threads/SpinLock.h"
@@ -154,8 +154,6 @@ class FileOutputStream;
#include "thread/MutexTraits.h"
#include "thread/TrackedMutex.h"
#include "diagnostic/FatalError.h"
#include "diagnostic/Error.h"
#include "diagnostic/Debug.h"
#include "text/LexicalCast.h"
#include "memory/ContainerDeletePolicy.h"
#include "maths/Math.h"

View File

@@ -17,84 +17,45 @@
*/
//==============================================================================
Stoppable::Stoppable (char const* name, Stoppable& parent)
Stoppable::Stoppable (char const* name, RootStoppable& root)
: m_name (name)
, m_root (false)
, m_root (root)
, m_child (this)
, m_calledStop (false)
, m_stopped (false)
, m_childrenStopped (false)
{
// must not have had stop called
}
Stoppable::Stoppable (char const* name, Stoppable& parent)
: m_name (name)
, m_root (parent.m_root)
, m_child (this)
, m_stopped (false)
, m_childrenStopped (false)
{
// Must not have stopping parent.
bassert (! parent.isStopping());
parent.m_children.push_front (&m_child);
}
Stoppable::Stoppable (char const* name, Stoppable* parent)
: m_name (name)
, m_root (parent == nullptr)
, m_child (this)
, m_calledStop (false)
, m_stopped (false)
, m_childrenStopped (false)
{
if (parent != nullptr)
{
// must not have had stop called
bassert (! parent->isStopping());
parent->m_children.push_front (&m_child);
}
}
Stoppable::~Stoppable ()
{
// must be stopped
bassert (m_stopped);
// children must be stopped
// Children must be stopped.
bassert (m_childrenStopped);
}
void Stoppable::stop (Journal::Stream stream)
bool Stoppable::isStopping() const
{
// may only be called once
if (m_calledStop)
return;
m_calledStop = true;
// must be called from a root stoppable
bassert (m_root);
// send the notification
stopAsync ();
// now block on the tree of Stoppable objects from the leaves up.
stopRecursive (stream);
return m_root.isStopping();
}
void Stoppable::stopAsync ()
{
// must be called from a root stoppable
bassert (m_root);
stopAsyncRecursive ();
}
bool Stoppable::isStopping ()
{
return m_calledStopAsync.get() != 0;
}
bool Stoppable::isStopped ()
bool Stoppable::isStopped () const
{
return m_stopped;
}
bool Stoppable::areChildrenStopped ()
bool Stoppable::areChildrenStopped () const
{
return m_childrenStopped;
}
@@ -104,75 +65,134 @@ void Stoppable::stopped ()
m_stoppedEvent.signal();
}
void Stoppable::onStop()
void Stoppable::onPrepare (Journal journal)
{
}
void Stoppable::onStart (Journal journal)
{
}
void Stoppable::onStop (Journal journal)
{
stopped();
}
void Stoppable::onChildrenStopped ()
void Stoppable::onChildrenStopped (Journal journal)
{
}
//------------------------------------------------------------------------------
void Stoppable::stopAsyncRecursive ()
void Stoppable::prepareRecursive (Journal journal)
{
// make sure we only do this once
if (m_root)
{
// if this fails, some other thread got to it first
if (! m_calledStopAsync.compareAndSetBool (1, 0))
return;
}
else
{
// can't possibly already be set
bassert (m_calledStopAsync.get() == 0);
m_calledStopAsync.set (1);
}
// notify this stoppable
onStop ();
// notify children
onPrepare (journal);
for (Children::const_iterator iter (m_children.cbegin ());
iter != m_children.cend(); ++iter)
{
iter->stoppable->stopAsyncRecursive();
}
iter->stoppable->prepareRecursive (journal);
}
void Stoppable::stopRecursive (Journal::Stream stream)
void Stoppable::startRecursive (Journal journal)
{
// Block on each child recursively. Thinking of the Stoppable
// hierarchy as a tree with the root at the top, we will block
// first on leaves, and then at each successivly higher level.
onStart (journal);
for (Children::const_iterator iter (m_children.cbegin ());
iter != m_children.cend(); ++iter)
iter->stoppable->startRecursive (journal);
}
void Stoppable::stopAsyncRecursive (Journal journal)
{
onStop (journal);
for (Children::const_iterator iter (m_children.cbegin ());
iter != m_children.cend(); ++iter)
iter->stoppable->stopAsyncRecursive (journal);
}
void Stoppable::stopRecursive (Journal journal)
{
// Block on each child from the bottom of the tree up.
//
for (Children::const_iterator iter (m_children.cbegin ());
iter != m_children.cend(); ++iter)
{
iter->stoppable->stopRecursive (stream);
}
iter->stoppable->stopRecursive (journal);
// Once we get here, we either have no children, or all of
// our children have stopped, so update state accordingly.
// if we get here then all children have stopped
//
memoryBarrier ();
m_childrenStopped = true;
onChildrenStopped (journal);
// Notify derived class that children have stopped.
onChildrenStopped ();
// Block until this stoppable stops. First we do a timed wait of 1 second, and
// if that times out we report to the Journal and then do an infinite wait.
// Now block on this Stoppable.
//
bool const timedOut (! m_stoppedEvent.wait (1 * 1000)); // milliseconds
if (timedOut)
{
stream << "Waiting for '" << m_name << "' to stop";
journal.warning << "Waiting for '" << m_name << "' to stop";
m_stoppedEvent.wait ();
}
// once we get here, we know the stoppable has stopped.
m_stopped = true;
}
//------------------------------------------------------------------------------
RootStoppable::RootStoppable (char const* name)
: Stoppable (name, *this)
{
}
RootStoppable::~RootStoppable ()
{
}
bool RootStoppable::isStopping() const
{
return m_calledStopAsync.get() != 0;
}
void RootStoppable::prepare (Journal journal)
{
if (! m_prepared.compareAndSetBool (1, 0))
{
journal.warning << "Stoppable::prepare called again";
return;
}
prepareRecursive (journal);
}
void RootStoppable::start (Journal journal)
{
// Courtesy call to prepare.
if (m_prepared.compareAndSetBool (1, 0))
prepareRecursive (journal);
if (! m_started.compareAndSetBool (1, 0))
{
journal.warning << "Stoppable::start called again";
return;
}
startRecursive (journal);
}
void RootStoppable::stop (Journal journal)
{
if (! m_calledStop.compareAndSetBool (1, 0))
{
journal.warning << "Stoppable::stop called again";
return;
}
stopAsync (journal);
stopRecursive (journal);
}
void RootStoppable::stopAsync (Journal journal)
{
if (! m_calledStopAsync.compareAndSetBool (1, 0))
return;
stopAsyncRecursive (journal);
}

View File

@@ -20,16 +20,66 @@
#ifndef BEAST_CORE_STOPPABLE_H_INCLUDED
#define BEAST_CORE_STOPPABLE_H_INCLUDED
/** Provides an interface for stopping.
class RootStoppable;
/** Provides an interface for starting and stopping.
A common method of structuring server or peer to peer code is to isolate
conceptual portions of functionality into individual classes, aggregated
into some larger "application" or "core" object which holds all the parts.
Frequently, these components are dependent on each other in unavoidably
complex ways. They also often use threads and perform asynchronous i/o
operations involving sockets or other operating system objects. The process
of starting and stopping such a system can be complex. This interface
provides a set of behaviors for ensuring that the start and stop of a
composite application-style object is well defined.
Upon the initialization of the composite object these steps are peformed:
1. Construct sub-components.
These are all typically derived from Stoppable. There can be a deep
hierarchy: Stoppable objects may themselves have Stoppable child
objects. This captures the relationship of dependencies.
2. prepare()
Because some components may depend on others, preparatory steps require
that all objects be first constructed. The prepare step calls all
Stoppable objects in the tree starting from the leaves and working up
to the root. In this stage we are guaranteed that all objects have been
constructed and are in a well-defined state.
3. onPrepare()
This override is called for all Stoppable objects in the hierarchy
during the prepare stage. Objects are called from the bottom up.
It is guaranteed that all child Stoppable objects have already been
prepared when this is called.
4. start()
At this point all sub-components have been constructed and prepared,
so it should be safe for them to be started. While some Stoppable
objects may do nothing in their start function, others will start
threads or call asynchronous i/o initiating functions like timers or
sockets.
5. onStart()
This override is called for all Stoppable objects in the hierarchy
during the start stage. Objects are called from the bottom up.
It is guaranteed that all child Stoppable objects have already been
started when this is called.
This is the sequence of events involved in stopping:
1. stopAsync() [optional]
6. stopAsync() [optional]
This notifies the root Stoppable and all its children that a stop is
requested.
2. stop()
7. stop()
This first calls stopAsync(), and then blocks on each child Stoppable in
the in the tree from the bottom up, until the Stoppable indicates it has
@@ -37,21 +87,21 @@
when some external signal indicates that the process should stop. For
example, an RPC 'stop' command, or a SIGINT POSIX signal.
3. onStop()
8. onStop()
This override is called for the root Stoppable and all its children when
stopAsync() is called. Derived classes should cancel pending I/O and
timers, signal that threads should exit, queue cleanup jobs, and perform
any other necessary final actions in preparation for exit.
4. onChildrenStopped()
9. onChildrenStopped()
This override is called when all the children have stopped. This informs
the Stoppable that there should not be any more dependents making calls
into its member functions. A Stoppable that has no children will still
have this function called.
5. stopped()
10. stopped()
The derived class calls this function to inform the Stoppable API that
it has completed the stop. This unblocks the caller of stop().
@@ -102,85 +152,44 @@
@note A Stoppable may not be restarted.
*/
/** @{ */
class Stoppable
{
public:
/** Create the stoppable.
A stoppable without a parent is a root stoppable.
@param name A name used in log output.
@param parent Optional parent of this stoppable.
*/
/** @{ */
Stoppable (char const* name, Stoppable& parent);
explicit Stoppable (char const* name, Stoppable* parent = nullptr);
/** @} */
protected:
Stoppable (char const* name, RootStoppable& root);
/** Destroy the stoppable.
Undefined behavior results if the object is not stopped first.
Stoppable objects should not be created and destroyed dynamically during
the process lifetime. Rather, the set of stoppables should be static and
well-defined after initialization. If the set of domain-specific objects
which need to stop is dynamic, use a single parent Stoppable to manage
those objects. For example, make an HTTP server implementation a
Stoppable, rather than each of its active connections.
*/
public:
/** Create the Stoppable. */
Stoppable (char const* name, Stoppable& parent);
/** Destroy the Stoppable. */
virtual ~Stoppable ();
/** Notify a root stoppable and children to stop, and block until stopped.
Has no effect if the stoppable was already notified.
This blocks until the stoppable and all of its children have stopped.
@param stream An optional Journal::Stream on which to log progress.
/** Returns `true` if the stoppable should stop. */
bool isStopping () const;
Thread safety:
Safe to call from any thread not associated with a Stoppable.
*/
void stop (Journal::Stream stream = Journal::Stream());
/** Returns `true` if the requested stop has completed. */
bool isStopped () const;
/** Notify a root stoppable and children to stop, without waiting.
Has no effect if the stoppable was already notified.
/** Returns `true` if all children have stopped. */
bool areChildrenStopped () const;
Thread safety:
Safe to call from any thread at any time.
*/
void stopAsync ();
/** Returns `true` if the stoppable should stop.
Call from the derived class to determine if a long-running operation
should be canceled. This is not appropriate for either threads, or
asynchronous I/O. For threads, use the thread-specific facilities
available to inform the thread that it should exit. For asynchronous
I/O, cancel all pending operations inside the onStop override.
@see onStop
Thread safety:
Safe to call from any thread at any time.
*/
bool isStopping ();
/** Returns `true` if the stoppable has completed its stop.
Thread safety:
Safe to call from any thread at any time.
*/
bool isStopped ();
/** Returns `true` if all children have stopped.
For stoppables without children, this returns `true` immediately
after a stop notification is received.
Thread safety:
Safe to call from any thread at any time.
*/
bool areChildrenStopped ();
/** Called by derived classes to indicate that the stoppable has stopped.
The derived class must call this either after isStopping returns `true`,
or when onStop is called, or else the call to stop will never unblock.
Thread safety:
Safe to call from any thread at any time.
*/
/** Called by derived classes to indicate that the stoppable has stopped. */
void stopped ();
/** Override called during preparation.
Since all other Stoppable objects in the tree have already been
constructed, this provides an opportunity to perform initialization which
depends on calling into other Stoppable objects.
This call is made on the same thread that called prepare().
The default implementation does nothing.
Guaranteed to only be called once.
*/
virtual void onPrepare (Journal journal);
/** Override called during start. */
virtual void onStart (Journal journal);
/** Override called when the stop notification is issued.
The call is made on an unspecified, implementation-specific thread.
@@ -202,7 +211,7 @@ public:
Guaranteed only to be called once.
Must be safe to call from any thread at any time.
*/
virtual void onStop ();
virtual void onStop (Journal journal);
/** Override called when all children have stopped.
@@ -222,9 +231,11 @@ public:
Guaranteed only to be called once.
Must be safe to call from any thread at any time.
*/
virtual void onChildrenStopped ();
virtual void onChildrenStopped (Journal journal);
private:
friend class RootStoppable;
struct Child;
typedef LockFreeStack <Child> Children;
@@ -237,28 +248,70 @@ private:
Stoppable* stoppable;
};
void stopAsyncRecursive ();
void stopRecursive (Journal::Stream stream);
void prepareRecursive (Journal journal);
void startRecursive (Journal journal);
void stopAsyncRecursive (Journal journal);
void stopRecursive (Journal journal);
protected:
char const* m_name;
bool m_root;
RootStoppable& m_root;
Child m_child;
Children m_children;
// Flag that we called stop. This is for diagnostics.
bool m_calledStop;
// Atomic flag to make sure we only call stopAsync once.
Atomic <int> m_calledStopAsync;
// Flag that this service stopped. Never goes back to false.
bool volatile m_stopped;
// Flag that all children have stopped (recursive). Never goes back to false.
bool volatile m_childrenStopped;
// stop() blocks on this event until stopped() is called.
Children m_children;
WaitableEvent m_stoppedEvent;
};
//------------------------------------------------------------------------------
class RootStoppable : public Stoppable
{
public:
explicit RootStoppable (char const* name);
~RootStoppable ();
bool isStopping() const;
/** Prepare all contained Stoppable objects.
This calls onPrepare for all Stoppable objects in the tree.
Calls made after the first have no effect.
Thread safety:
May be called from any thread.
*/
void prepare (Journal journal = Journal());
/** Start all contained Stoppable objects.
The default implementation does nothing.
Calls made after the first have no effect.
Thread safety:
May be called from any thread.
*/
void start (Journal journal = Journal());
/** Notify a root stoppable and children to stop, and block until stopped.
Has no effect if the stoppable was already notified.
This blocks until the stoppable and all of its children have stopped.
Thread safety:
Safe to call from any thread not associated with a Stoppable.
*/
void stop (Journal journal = Journal());
/** Notify a root stoppable and children to stop, without waiting.
Has no effect if the stoppable was already notified.
Thread safety:
Safe to call from any thread at any time.
*/
void stopAsync (Journal journal = Journal());
private:
Atomic <int> m_prepared;
Atomic <int> m_started;
Atomic <int> m_calledStop;
Atomic <int> m_calledStopAsync;
};
/** @} */
#endif

View File

@@ -23,7 +23,7 @@ for f in $1/*/*.cpp
do
{
echo "Compilng '$f'"
g++ -xc++ -I$1/../scripts/ "$f" -c -o /dev/null
g++ -xc++ -std=c++11 -I$1/../scripts/ "$f" -c -o /dev/null
g++ -xc++ -I$1/../config/ "$f" -c -o /dev/null
g++ -xc++ -std=c++11 -I$1/../config/ "$f" -c -o /dev/null
}
done