add draft impl for Monitor storage; dummy impl running

todo: actually do any locking, improve handling of the forThis parameter
This commit is contained in:
Fischlurch 2008-12-15 06:18:38 +01:00
parent 2512f04f3f
commit c4df935113
2 changed files with 45 additions and 8 deletions

View file

@ -40,8 +40,15 @@
#include "include/nobugcfg.h"
#include "lib/util.hpp"
#include <boost/scoped_ptr.hpp>
#include <iostream> ///////////////////////////TODO
using std::cerr;
namespace lib {
using boost::scoped_ptr;
/**
* Facility for monitor object based locking.
@ -58,20 +65,38 @@ namespace lib {
{
struct Monitor
{
void acquireLock() { TODO ("acquire Thread Lock"); }
void releaseLock() { TODO ("release Thread Lock"); }
Monitor() { TODO ("maybe create mutex struct here?"); }
~Monitor() { TODO ("destroy mutex, assert it isn't locked!"); } /////TODO: Probably need to unlock it silently in case of a class-level monitor?
void acquireLock() { cerr << "acquire Thread Lock\n"; }
void releaseLock() { cerr << "release Thread Lock\n"; }
};
Monitor objectMonitor_;
/////////////////////////////////////////////////////////////////////////TODO: any better idea for where to put the template parameter? so we can get rid of it for the typical usage scenario?
/////////////////////////////////////////////////////////////////////////TODO: better solution for the storage? Implement the per-this locking without subclassing!
/////////////////////////////////////////////////////////////////////////TODO: factor out the recursive/non-recursive mutex case as policy...
template<class X>
static Monitor& getMonitor(X* forThis);
static inline Monitor& getMonitor();
static inline Monitor& getMonitor(Concurrency* forThis);
template<class X>
class Lock
{
Monitor& mon_;
public:
Lock(X* it=0)
: mon_(getMonitor<X> (it))
Lock(X* it)
: mon_(getMonitor(it))
{
mon_.acquireLock();
}
Lock()
: mon_(getMonitor<X>())
{
mon_.acquireLock();
}
@ -85,11 +110,22 @@ namespace lib {
Concurrency::Monitor&
Concurrency::getMonitor(Concurrency* forThis)
{
REQUIRE (forThis);
return forThis->objectMonitor_;
}
template<class X>
Concurrency::Monitor&
Concurrency::getMonitor(X* forThis)
Concurrency::getMonitor()
{
UNIMPLEMENTED ("get moni");
//TODO: guard this by a Mutex? consider double checked locking? (but then class Monitor needs to be volatile, thus is it worth the effort?)
static scoped_ptr<Monitor> classMonitor_ (0);
if (!classMonitor_) classMonitor_.reset (new Monitor ());
return *classMonitor_;
}

View file

@ -55,7 +55,7 @@ namespace lib {
class Victim
: Concurrency
: public Concurrency
{
volatile long cnt_[NUM_COUNTERS];
volatile uint step_; ///< @note stored as instance variable
@ -64,6 +64,7 @@ namespace lib {
pause ()
{
Lock<Victim> guard (this); // note recursive lock
for ( uint i=0, lim=(rand() % MAX_PAUSE); i<lim; ++i);
}