reworked sync.hpp using libraries locking functions
This commit is contained in:
parent
dbb3b2e2e7
commit
aea546554b
2 changed files with 172 additions and 88 deletions
|
|
@ -118,7 +118,7 @@ namespace backend {
|
||||||
void
|
void
|
||||||
join()
|
join()
|
||||||
{
|
{
|
||||||
accessMonitor().wait (*this, &JoinHandle::wakeupCheck);
|
accessMonitor().wait (&handle, *this, &JoinHandle::wakeupCheck);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
258
src/lib/sync.hpp
258
src/lib/sync.hpp
|
|
@ -91,71 +91,169 @@ namespace lib {
|
||||||
|
|
||||||
/** Helpers and building blocks for Monitor based synchronisation */
|
/** Helpers and building blocks for Monitor based synchronisation */
|
||||||
namespace sync {
|
namespace sync {
|
||||||
|
|
||||||
|
|
||||||
struct Wrapped_LumieraExcMutex
|
struct Wrapped_LumieraExcMutex
|
||||||
: public lumiera_mutex
|
|
||||||
{
|
{
|
||||||
|
lumiera_mutex self;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Wrapped_LumieraExcMutex() { lumiera_mutex_init (this, "Obj.Monitor ExclMutex", &NOBUG_FLAG(sync)); }
|
Wrapped_LumieraExcMutex() { lumiera_mutex_init (&self, "Obj.Monitor ExclMutex", &NOBUG_FLAG(sync)); }
|
||||||
~Wrapped_LumieraExcMutex() { lumiera_mutex_destroy (this, &NOBUG_FLAG(sync)); }
|
~Wrapped_LumieraExcMutex() { lumiera_mutex_destroy (&self, &NOBUG_FLAG(sync)); }
|
||||||
|
|
||||||
pthread_mutex_t* mutex() { return &(lumiera_mutex::mutex); }
|
bool lock (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_mutex_lock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------Resource-Tracking------
|
bool trylock (struct nobug_resource_user** handle)
|
||||||
void __may_block() { TODO ("Record we may block on mutex"); }
|
{
|
||||||
void __enter() { TODO ("Record we successfully acquired the mutex"); }
|
return !!lumiera_mutex_trylock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
void __left() { TODO ("Record we released the mutex"); }
|
}
|
||||||
|
|
||||||
|
bool timedlock (const struct timespec* timeout, struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_mutex_timedlock (&self, timeout, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
lumiera_mutex_unlock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
LumieraReccondition myreccond () {throw "bullshit"; return NULL;} // placeholder, brainstorm
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Wrapped_LumieraRecMutex
|
struct Wrapped_LumieraRecMutex
|
||||||
: public lumiera_recmutex
|
|
||||||
{
|
{
|
||||||
|
lumiera_recmutex self;
|
||||||
protected:
|
protected:
|
||||||
Wrapped_LumieraRecMutex() { lumiera_recmutex_init (this, "Obj.Monitor RecMutex", &NOBUG_FLAG(sync)); }
|
Wrapped_LumieraRecMutex() { lumiera_recmutex_init (&self, "Obj.Monitor RecMutex", &NOBUG_FLAG(sync)); }
|
||||||
~Wrapped_LumieraRecMutex() { lumiera_recmutex_destroy (this, &NOBUG_FLAG(sync)); }
|
~Wrapped_LumieraRecMutex() { lumiera_recmutex_destroy (&self, &NOBUG_FLAG(sync)); }
|
||||||
|
|
||||||
pthread_mutex_t* mutex() { return &recmutex; }
|
bool lock (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_recmutex_lock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------Resource-Tracking------
|
bool trylock (struct nobug_resource_user** handle)
|
||||||
void __may_block() { TODO ("Record we may block on mutex"); }
|
{
|
||||||
void __enter() { TODO ("Record we successfully acquired the mutex"); }
|
return !!lumiera_recmutex_trylock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
void __left() { TODO ("Record we are released the mutex"); }
|
}
|
||||||
|
|
||||||
|
bool timedlock (const struct timespec* timeout, struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_recmutex_timedlock (&self, timeout, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
lumiera_recmutex_unlock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
LumieraReccondition myreccond () {throw "bullshit"; return NULL;} // placeholder, brainstorm
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Wrapped_LumieraExcCond
|
struct Wrapped_LumieraExcCond
|
||||||
: public lumiera_condition
|
|
||||||
{
|
{
|
||||||
|
lumiera_condition self;
|
||||||
protected:
|
protected:
|
||||||
Wrapped_LumieraExcCond() { lumiera_condition_init (this, "Obj.Monitor ExclCondition", &NOBUG_FLAG(sync) ); }
|
Wrapped_LumieraExcCond() { lumiera_condition_init (&self, "Obj.Monitor ExclCondition", &NOBUG_FLAG(sync) ); }
|
||||||
~Wrapped_LumieraExcCond() { lumiera_condition_destroy (this, &NOBUG_FLAG(sync) ); }
|
~Wrapped_LumieraExcCond() { lumiera_condition_destroy (&self, &NOBUG_FLAG(sync) ); }
|
||||||
|
|
||||||
pthread_mutex_t* mutex() { return &cndmutex; }
|
bool lock (struct nobug_resource_user** handle)
|
||||||
pthread_cond_t* condv() { return &cond; }
|
{
|
||||||
|
return !!lumiera_condition_lock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------Resource-Tracking------
|
bool trylock (struct nobug_resource_user** handle)
|
||||||
void __may_block() { TODO ("Record we may block on mutex"); }
|
{
|
||||||
void __enter() { TODO ("Record we successfully acquired the mutex"); }
|
return !!lumiera_condition_trylock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
void __left() { TODO ("Record we released the mutex"); }
|
}
|
||||||
|
|
||||||
|
bool timedlock (const struct timespec* timeout, struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_condition_timedlock (&self, timeout, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wait (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_condition_wait (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool timedwait (const struct timespec* timeout, struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_condition_timedwait (&self, timeout, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void signal()
|
||||||
|
{
|
||||||
|
lumiera_condition_signal (&self, &NOBUG_FLAG(sync));
|
||||||
|
}
|
||||||
|
|
||||||
|
void broadcast()
|
||||||
|
{
|
||||||
|
lumiera_condition_broadcast (&self, &NOBUG_FLAG(sync));
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
lumiera_condition_unlock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
LumieraReccondition myreccond () {throw "bullshit"; return NULL;} // placeholder, brainstorm
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Wrapped_LumieraRecCond
|
struct Wrapped_LumieraRecCond
|
||||||
: public lumiera_reccondition
|
|
||||||
{
|
{
|
||||||
|
lumiera_reccondition self;
|
||||||
protected:
|
protected:
|
||||||
Wrapped_LumieraRecCond() { lumiera_reccondition_init (this, "Obj.Monitor RecCondition", &NOBUG_FLAG(sync) ); } ////////TODO
|
Wrapped_LumieraRecCond() { lumiera_reccondition_init (&self, "Obj.Monitor RecCondition", &NOBUG_FLAG(sync) ); }
|
||||||
~Wrapped_LumieraRecCond() { lumiera_reccondition_destroy (this, &NOBUG_FLAG(sync) ); }
|
~Wrapped_LumieraRecCond() { lumiera_reccondition_destroy (&self, &NOBUG_FLAG(sync) ); }
|
||||||
|
|
||||||
pthread_mutex_t* mutex() { return &reccndmutex; }
|
bool lock (struct nobug_resource_user** handle)
|
||||||
pthread_cond_t* condv() { return &cond; }
|
{
|
||||||
|
return !!lumiera_reccondition_lock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------Resource-Tracking------
|
bool trylock (struct nobug_resource_user** handle)
|
||||||
void __may_block() { TODO ("Record we may block on mutex"); }
|
{
|
||||||
void __enter() { TODO ("Record we successfully acquired the mutex"); }
|
return !!lumiera_reccondition_trylock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
void __left() { TODO ("Record we released the mutex"); }
|
}
|
||||||
|
|
||||||
|
bool timedlock (const struct timespec* timeout, struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_reccondition_timedlock (&self, timeout, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wait (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_reccondition_wait (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool timedwait (const struct timespec* timeout, struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
return !!lumiera_reccondition_timedwait (&self, timeout, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void signal()
|
||||||
|
{
|
||||||
|
lumiera_reccondition_signal (&self, &NOBUG_FLAG(sync));
|
||||||
|
}
|
||||||
|
|
||||||
|
void broadcast()
|
||||||
|
{
|
||||||
|
lumiera_reccondition_broadcast (&self, &NOBUG_FLAG(sync));
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock (struct nobug_resource_user** handle)
|
||||||
|
{
|
||||||
|
lumiera_reccondition_unlock (&self, &NOBUG_FLAG(sync), handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
LumieraReccondition myreccond () { return &self;} // placeholder, brainstorm
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -167,10 +265,6 @@ namespace lib {
|
||||||
: protected MTX
|
: protected MTX
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
using MTX::mutex;
|
|
||||||
using MTX::__may_block;
|
|
||||||
using MTX::__enter;
|
|
||||||
using MTX::__left;
|
|
||||||
|
|
||||||
~Mutex () { }
|
~Mutex () { }
|
||||||
Mutex () { }
|
Mutex () { }
|
||||||
|
|
@ -179,22 +273,15 @@ namespace lib {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void
|
void
|
||||||
acquire()
|
acquire(struct nobug_resource_user** handle)
|
||||||
{
|
{
|
||||||
__may_block();
|
MTX::lock(handle);
|
||||||
|
|
||||||
if (pthread_mutex_lock (mutex()))
|
|
||||||
throw lumiera::error::State("Mutex acquire failed."); ///////TODO capture the error-code
|
|
||||||
|
|
||||||
__enter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
release()
|
release(struct nobug_resource_user** handle)
|
||||||
{
|
{
|
||||||
pthread_mutex_unlock (mutex());
|
MTX::unlock(handle);
|
||||||
|
|
||||||
__left();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
@ -207,41 +294,38 @@ namespace lib {
|
||||||
class Condition
|
class Condition
|
||||||
: public Mutex<CDX>
|
: public Mutex<CDX>
|
||||||
{
|
{
|
||||||
protected:
|
|
||||||
using CDX::condv;
|
|
||||||
using CDX::mutex;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void
|
void
|
||||||
signal (bool wakeAll=false)
|
signal (bool wakeAll=false)
|
||||||
{
|
{
|
||||||
if (wakeAll)
|
if (wakeAll)
|
||||||
pthread_cond_broadcast (condv());
|
CDX::broadcast ();
|
||||||
else
|
else
|
||||||
pthread_cond_signal (condv());
|
CDX::signal ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class BF>
|
template<class BF>
|
||||||
bool
|
bool
|
||||||
wait (BF& predicate, Timeout& waitEndTime)
|
wait (struct nobug_resource_user** handle, BF& predicate, Timeout& waitEndTime)
|
||||||
{
|
{
|
||||||
int err=0;
|
bool ok = true;
|
||||||
while (!predicate() && !err)
|
while (ok && !predicate())
|
||||||
if (waitEndTime)
|
if (waitEndTime)
|
||||||
err = pthread_cond_timedwait (condv(), mutex(), &waitEndTime);
|
ok = CDX::timedwait (&waitEndTime, handle);
|
||||||
else
|
else
|
||||||
err = pthread_cond_wait (condv(), mutex());
|
ok = CDX::wait (handle);
|
||||||
|
|
||||||
if (!err) return true;
|
if (!ok && lumiera_error_expect(LUMIERA_ERROR_LOCK_TIMEOUT)) return false;
|
||||||
if (ETIMEDOUT==err) return false;
|
lumiera::throwOnError(); // any other error thows
|
||||||
|
|
||||||
throw lumiera::error::State ("Condition wait failed."); ///////////TODO extract error-code
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* helper for specifying an optional timeout for an timed wait.
|
* helper for specifying an optional timeout for an timed wait.
|
||||||
* Wrapping a timespec-struct, it allows for easy initialisation
|
* Wrapping a timespec-struct, it allows for easy initialisation
|
||||||
* by a given relative offset.
|
* by a given relative offset.
|
||||||
|
|
@ -320,30 +404,30 @@ namespace lib {
|
||||||
const Monitor& operator= (Monitor const& ref) { timeout_ = ref.timeout_; }
|
const Monitor& operator= (Monitor const& ref) { timeout_ = ref.timeout_; }
|
||||||
|
|
||||||
|
|
||||||
void acquireLock() { IMPL::acquire(); }
|
void acquireLock(struct nobug_resource_user** handle) { IMPL::acquire(handle); }
|
||||||
void releaseLock() { IMPL::release(); }
|
void releaseLock(struct nobug_resource_user** handle) { IMPL::release(handle); }
|
||||||
|
|
||||||
void signal(bool a){ IMPL::signal(a); }
|
void signal(bool a){ IMPL::signal(a); }
|
||||||
|
|
||||||
bool
|
bool
|
||||||
wait (Flag flag, ulong timedwait=0)
|
wait (struct nobug_resource_user** handle, Flag flag, ulong timedwait=0)
|
||||||
{
|
{
|
||||||
BoolFlagPredicate checkFlag(flag);
|
BoolFlagPredicate checkFlag(flag);
|
||||||
return IMPL::wait(checkFlag, timeout_.setOffset(timedwait));
|
return IMPL::wait(handle, checkFlag, timeout_.setOffset(timedwait));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class X>
|
template<class X>
|
||||||
bool
|
bool
|
||||||
wait (X& instance, bool (X::*method)(void), ulong timedwait=0)
|
wait (struct nobug_resource_user** handle, X& instance, bool (X::*method)(void), ulong timedwait=0)
|
||||||
{
|
{
|
||||||
BoolMethodPredicate<X> invokeMethod(instance, method);
|
BoolMethodPredicate<X> invokeMethod(instance, method);
|
||||||
return IMPL::wait(invokeMethod, timeout_.setOffset(timedwait));
|
return IMPL::wait(handle, invokeMethod, timeout_.setOffset(timedwait));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setTimeout(ulong relative) {timeout_.setOffset(relative);}
|
void setTimeout(ulong relative) {timeout_.setOffset(relative);}
|
||||||
bool isTimedWait() {return (timeout_);}
|
bool isTimedWait() {return (timeout_);}
|
||||||
|
|
||||||
LumieraReccondition accessCond(){return static_cast<LumieraReccondition> (this);}
|
LumieraReccondition accessCond(){return IMPL::myreccond();}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef Mutex<Wrapped_LumieraExcMutex> NonrecursiveLock_NoWait;
|
typedef Mutex<Wrapped_LumieraExcMutex> NonrecursiveLock_NoWait;
|
||||||
|
|
@ -357,9 +441,6 @@ namespace lib {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Interface to be used by client code:
|
/* Interface to be used by client code:
|
||||||
* Inherit from class Sync with a suitable Policy.
|
* Inherit from class Sync with a suitable Policy.
|
||||||
* Then use the embedded Lock class.
|
* Then use the embedded Lock class.
|
||||||
|
|
@ -374,14 +455,14 @@ namespace lib {
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Facility for monitor object based locking.
|
* Facility for monitor object based locking.
|
||||||
* To be attached either on a per class base or per object base.
|
* To be attached either on a per class base or per object base.
|
||||||
* Typically, the client class will inherit from this template (but it
|
* Typically, the client class will inherit from this template (but it
|
||||||
* is possible to use it stand-alone, if inheriting isn't an option).
|
* is possible to use it stand-alone, if inheriting isn't an option).
|
||||||
* The interface for clients to access the functionality is the embedded
|
* The interface for clients to access the functionality is the embedded
|
||||||
* Lock template, which should be instantiated as an automatic variable
|
* Lock template, which should be instantiated as an automatic variable
|
||||||
* within the scope to be protected.
|
* within the scope to be protected.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
template<class CONF = NonrecursiveLock_NoWait>
|
template<class CONF = NonrecursiveLock_NoWait>
|
||||||
class Sync
|
class Sync
|
||||||
|
|
@ -404,9 +485,11 @@ namespace lib {
|
||||||
Monitor& mon_;
|
Monitor& mon_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
RESOURCE_USER (handle); // temporary public until thread impl settles the locking
|
||||||
|
|
||||||
template<class X>
|
template<class X>
|
||||||
Lock(X* it) : mon_(getMonitor(it)){ mon_.acquireLock(); }
|
Lock(X* it) : mon_(getMonitor(it)){ RESOURCE_USER_INIT (handle); mon_.acquireLock(&handle); }
|
||||||
~Lock() { mon_.releaseLock(); }
|
~Lock() { mon_.releaseLock(&handle); }
|
||||||
|
|
||||||
void notify() { mon_.signal(false);}
|
void notify() { mon_.signal(false);}
|
||||||
void notifyAll() { mon_.signal(true); }
|
void notifyAll() { mon_.signal(true); }
|
||||||
|
|
@ -417,31 +500,32 @@ namespace lib {
|
||||||
bool
|
bool
|
||||||
wait (C& cond, ulong timeout=0)
|
wait (C& cond, ulong timeout=0)
|
||||||
{
|
{
|
||||||
return mon_.wait(cond,timeout);
|
return mon_.wait(&handle, cond, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename X>
|
template<typename X>
|
||||||
bool
|
bool
|
||||||
wait (X& instance, bool (X::*predicate)(void), ulong timeout=0)
|
wait (X& instance, bool (X::*predicate)(void), ulong timeout=0)
|
||||||
{
|
{
|
||||||
return mon_.wait(instance,predicate,timeout);
|
return mon_.wait(&handle, instance, predicate, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** convenience shortcut:
|
/** convenience shortcut:
|
||||||
* Locks and immediately enters wait state,
|
* Locks and immediately enters wait state,
|
||||||
* observing a condition defined as member function. */
|
* observing a condition defined as member function. */
|
||||||
template<class X>
|
template<class X>
|
||||||
Lock(X* it, bool (X::*method)(void))
|
Lock(X* it, bool (X::*method)(void))
|
||||||
: mon_(getMonitor(it))
|
: mon_(getMonitor(it))
|
||||||
{
|
{
|
||||||
mon_.acquireLock();
|
RESOURCE_USER_INIT (handle);
|
||||||
mon_.wait(*it,method);
|
mon_.acquireLock(&handle);
|
||||||
|
mon_.wait(&handle, *it, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** for creating a ClassLock */
|
/** for creating a ClassLock */
|
||||||
Lock(Monitor& m) : mon_(m)
|
Lock(Monitor& m) : mon_(m)
|
||||||
{ mon_.acquireLock(); }
|
{ RESOURCE_USER_INIT (handle); mon_.acquireLock(&handle); }
|
||||||
|
|
||||||
/** for controlled access to the
|
/** for controlled access to the
|
||||||
* underlying sync primitives */
|
* underlying sync primitives */
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue