simplify ClassLock by use of Meyer's Singleton with zombie check

...and package the ZombieCheck as helper object.
Also rewrite the SyncClassLock_test to perform an
multithreaded contended test to prove the lock is shared and effective
This commit is contained in:
Fischlurch 2018-04-01 05:33:44 +02:00
parent 21fdce0dfc
commit bfbcc5de43
4 changed files with 76 additions and 144 deletions

View file

@ -93,6 +93,7 @@
#include "lib/nocopy.hpp"
#include "lib/nobug-init.hpp"
#include "lib/sync-classlock.hpp"
#include "lib/zombie-check.hpp"
#include "lib/meta/util.hpp"
#include <type_traits>
@ -125,25 +126,16 @@ namespace lib {
Creator creator_;
Deleter deleter_;
bool deceased_ =false;
public:
ZombieCheck zombieCheck;
DependencyFactory() = default;
~DependencyFactory()
{
deceased_ = true;
if (deleter_)
deleter_();
}
void
zombieCheck()
{
if (deceased_)
throw error::Fatal("DependencyFactory invoked out of order during Application shutdown. "
"Lumiera Policy violated: Dependencies must not be used from destructors."
,error::LUMIERA_ERROR_LIFECYCLE);
}
OBJ*
operator() ()
{

View file

@ -29,7 +29,7 @@
**
** @note simply using the ClassLock may cause a Monitor object (with a mutex) to be
** created at static initialisation and destroyed on application shutdown.
** @see singleton-factory.hpp usage example
** @see depend.hpp usage example
*/
@ -37,51 +37,12 @@
#define LIB_SYNC_CLASSLOCK_H
#include "lib/nobug-init.hpp"
#include "lib/zombie-check.hpp"
#include "lib/sync.hpp"
namespace lib {
namespace nifty { // implementation details
template<class X>
struct Holder
{
static uint accessed_;
static char content_[sizeof(X)];
Holder()
{
if (!accessed_)
new(content_) X();
++accessed_;
}
~Holder()
{
--accessed_;
if (0==accessed_)
get().~X();
}
X&
get()
{
X* obj = reinterpret_cast<X*> (&content_);
ASSERT (obj, "Logic of Schwartz counter broken.");
return *obj;
}
};
template<class X>
uint Holder<X>::accessed_;
template<class X>
char Holder<X>::content_[sizeof(X)];
} // (End) nifty implementation details
/**
* A synchronisation protection guard employing a lock scoped
@ -109,21 +70,15 @@ namespace lib {
Monitor&
getPerClassMonitor()
{
static nifty::Holder<PerClassMonitor> __used_here;
if (1 != use_count())
{
///////////////////////////////////////////////////////////////////////////TICKET #1133 investigate Problems with shutdown order
ERROR (progress, "AUA %d", use_count());
}
ASSERT (1==use_count(), "static init broken");
static PerClassMonitor classMonitor;
static ZombieCheck zombieCheck;
return __used_here.get();
zombieCheck();
return classMonitor;
}
public:
ClassLock() : Lock (getPerClassMonitor()) { }
uint use_count() { return nifty::Holder<PerClassMonitor>::accessed_; }
};

View file

@ -22,6 +22,14 @@
/** @file dependable-base.hpp
** Detector to set off alarm when (re)using deceased objects.
** When implementing services based on static fields or objects,
** an invocation after static shutdown can not be precluded -- be it by
** re-entrance, be through indirect reference to some dependency within a
** static function residing in another translation unit. Since typically the
** values in static storage are not overwritten after invoking the destructor,
** we may plant an automatic "zombie detector" to give a clear indication of
** such a policy violation (Lumiera forbids to use dependencies from dtors).
**
** @see sync-classlock.hpp
** @see depend.hpp
*/
@ -30,69 +38,43 @@
#ifndef LIB_ZOMBIE_CHECK_H
#define LIB_ZOMBIE_CHECK_H
#include "lib/del-stash.hpp"
#include "lib/nocopy.hpp"
#include "lib/error.hpp"
#include <iostream>
#include <utility>
namespace lib {
namespace nifty { // implementation details
template<class X>
struct Holder
{
char payload_[sizeof(X)];
//NOTE: deliberately no ctor/dtor
X&
access()
{
return *reinterpret_cast<X*> (&payload_);
}
};
template<class X>
uint Holder<X>::accessed_;
} // (End) nifty implementation details
namespace error = lumiera::error;
/**
* A dependable data container available with extended lifespan.
* Automatically plants a ref-count to ensure the data stays alive
* until the last static reference goes out of scope.
* Automatic lifecycle tracker, to produce an alarm when accessing objects after deletion.
*/
template<class X>
class DependableBase
: util::NonCopyable
class ZombieCheck
{
static nifty::Holder<X> storage_;
bool deceased_ = false;
public:
template<typename...ARGS>
explicit
DependableBase (ARGS&& ...args)
ZombieCheck() = default;
~ZombieCheck()
{
storage_.maybeInit (std::forward<ARGS> (args)...);
deceased_ = true;
}
operator X& () const
operator bool() const
{
return storage_.access();
return deceased_;
}
uint use_count() { return nifty::Holder<PerClassMonitor>::accessed_; }
void
operator() () const
{
if (deceased_)
throw error::Fatal("Already deceased object called out of order during Application shutdown. "
"Lumiera Policy violated: Dependencies must not be used from destructors."
,error::LUMIERA_ERROR_LIFECYCLE);
}
};
/** plant a static buffer to hold the payload X */
template<class X>
nifty::Holder<X> DependableBase<X>::storage_;
} // namespace lib
#endif /*LIB_ZOMBIE_CHECK_H*/

View file

@ -26,42 +26,22 @@
#include "lib/test/run.hpp"
#include "lib/error.hpp"
#include "lib/sync-classlock.hpp"
#include "lib/scoped-collection.hpp"
#include "backend/thread-wrapper.hpp"
using test::Test;
using backend::ThreadJoinable;
namespace lib {
namespace test {
namespace { // private test classes and data...
namespace { // Parameters for multithreaded contention test
const uint NUM_INSTANCES = 20; ///< number of probe instances to create
const uint NUM_THREADS = 20; ///< number of contending threads to create
const uint NUM_LOOP = 1000; ///< number of loop iterations per thread
/**
* Several instances of this probe class will be created.
* Each of them acquires the shared lock; but anyway, just
* by defining this class, the embedded Monitor got created.
*/
struct Probe
{
ClassLock<Probe> shared_lock_;
Probe() {}
~Probe() {}
};
} // (End) test classes and data....
}
@ -70,9 +50,10 @@ namespace test {
* @test check proper handling of class (not instance)-based Monitor locks.
* Because no instance is available in this case, a hidden storage for the
* Monitor object needs to be provided in a way safe for use even in the
* static startup/shutdown phase. This test validates the associated
* refcounting and object creation works as expected. It does \em not
* validate the locking functionality as such.
* static startup/shutdown phase. This can not directly validate this
* allocation of a shared monitor object behind the scenes, but it
* can verify the monitor is indeed shared by all ClassLock instances
* templated to a specific type.
*
* @see sync.hpp
*/
@ -82,15 +63,37 @@ namespace test {
virtual void
run (Arg)
{
{
Probe objs[NUM_INSTANCES];
CHECK (1 == objs[0].shared_lock_.use_count());
}
int contended = 0;
ClassLock<Probe> get_class_lock;
CHECK ( 1 == get_class_lock.use_count()); // embedded PerClassMonitor<Probe> got created exactly once
} // and stays alive until static dtors are called....
using Threads = lib::ScopedCollection<ThreadJoinable>;
// Start a bunch of threads with random access pattern
Threads threads{NUM_THREADS,
[&](Threads::ElementHolder& storage)
{
storage.create<ThreadJoinable> ("Sync-ClassLock stress test"
,[&]{
for (uint i=0; i<NUM_LOOP; ++i)
{
uint delay = rand() % 10;
usleep (delay);
{
ClassLock<void> guard;
++contended;
}
}
});
}
};
for (auto& thread : threads)
thread.join(); // block until thread terminates
CHECK (contended == NUM_THREADS * NUM_LOOP,
"ALARM: Lock failed, concurrent modification "
"during counter increment. Delta == %d"
,NUM_THREADS * NUM_LOOP - contended);
}
};