Merge branch 'master' into second-tp-attempt

This commit is contained in:
Michael Ploujnikov 2010-01-14 19:32:07 -05:00
commit 9d4494aa90
7 changed files with 235 additions and 102 deletions

View file

@ -118,7 +118,7 @@ namespace backend {
void
join()
{
accessMonitor().wait (*this, &JoinHandle::wakeupCheck);
accessMonitor().wait (&handle, *this, &JoinHandle::wakeupCheck);
}
};

View file

@ -52,8 +52,10 @@
cnd, (lumiera_sectionlock_unlock_fn) lumiera_condition_unlock \
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
({ \
lumiera_lock_section_.lock = \
lumiera_condition_lock (cnd, &NOBUG_FLAG(nobugflag), &lumiera_lock_section_.rh); \
if (lumiera_lock_section_.lock) \
lumiera_lock_section_.lock = \
lumiera_condition_lock (cnd, &NOBUG_FLAG(nobugflag), &lumiera_lock_section_.rh); \
lumiera_lock_section_.lock; \
}); \
({ \
LUMIERA_CONDITION_SECTION_UNLOCK; \

View file

@ -134,3 +134,19 @@ lumiera_error_peek (void)
{
return lumiera_error_get ()->err;
}
int
lumiera_error_expect (lumiera_err expected)
{
LumieraErrorcontext self = lumiera_error_get ();
lumiera_err err = self->err;
if (err == expected)
{
if (err)
self->err = NULL;
return 1;
}
else
return 0;
}

View file

@ -160,6 +160,17 @@ lumiera_err
lumiera_error_peek (void);
/**
* Expect some error
* Check that the current error state matches some expectation, if true then the error state
* is cleared and 1 is returned, otherwise 0 is returned and the error state remains set.
* @param expected error which is expected
* @return 1 if the current error state equals the expected state, else 0, the error state is cleared when the expectation is met
*/
int
lumiera_error_expect (lumiera_err expected);
/*
predefined errors
*/

View file

@ -125,6 +125,7 @@ namespace lumiera {
LUMIERA_ERROR_DECLARE (WRONG_TYPE); ///< runtime type mismatch
LUMIERA_ERROR_DECLARE (ITER_EXHAUST); ///< end of sequence reached
LUMIERA_ERROR_DECLARE (BOTTOM_VALUE); ///< invalid or NIL value
LUMIERA_ERROR_DECLARE (RUNTIME); ///< generic runtime error, will be overridden by id
/** Macro for creating derived exception classes properly
@ -155,6 +156,7 @@ namespace lumiera {
LUMIERA_EXCEPTION_DECLARE (State, Error, LUMIERA_ERROR_STATE);
LUMIERA_EXCEPTION_DECLARE (Invalid, Error, LUMIERA_ERROR_INVALID);
LUMIERA_EXCEPTION_DECLARE (External, Error, LUMIERA_ERROR_EXTERNAL);
LUMIERA_EXCEPTION_DECLARE (Runtime, Error, LUMIERA_ERROR_RUNTIME);
/** install our own handler for undeclared exceptions. Will be
@ -163,6 +165,22 @@ namespace lumiera {
} // namespace error
/**
* Throw a 'Runtime' error which wraps an existing lumiera error
* no-op when no error is pending. Does not clear the error state.
*/
static inline void throwOnError()
{
lumiera_err err = lumiera_error_peek();
if (err)
{
const char* extra = lumiera_error_extra();
throw error::Runtime(extra?extra:"", err);
}
}
} // namespace lumiera

View file

@ -44,17 +44,19 @@
* @param nobugflag NoBug flag used to log actions on the condition
* @param cnd Condition variable to be locked
*/
#define LUMIERA_RECCONDITION_SECTION(nobugflag, cnd) \
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
lumiera_lock_section_ = { \
cnd, (lumiera_sectionlock_unlock_fn) lumiera_reccondition_unlock \
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
({ \
lumiera_lock_section_.lock = \
lumiera_reccondition_lock (cnd, &NOBUG_FLAG(nobugflag), &lumiera_lock_section_.rh); \
}); \
({ \
LUMIERA_RECCONDITION_SECTION_UNLOCK; \
#define LUMIERA_RECCONDITION_SECTION(nobugflag, cnd) \
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
lumiera_lock_section_ = { \
cnd, (lumiera_sectionlock_unlock_fn) lumiera_reccondition_unlock \
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
({ \
if (lumiera_lock_section_.lock) \
lumiera_lock_section_.lock = \
lumiera_reccondition_lock (cnd, &NOBUG_FLAG(nobugflag), &lumiera_lock_section_.rh); \
lumiera_lock_section_.lock; \
}); \
({ \
LUMIERA_RECCONDITION_SECTION_UNLOCK; \
}))
@ -92,7 +94,7 @@
do { \
REQUIRE (lumiera_lock_section_.lock, "Reccondition mutex not locked"); \
lumiera_reccondition_wait (lumiera_lock_section_.lock, \
NOBUG_FLAG_RAW(lumiera_lock_section_.flag), \
lumiera_lock_section_.flag, \
&lumiera_lock_section_.rh); \
} while (!(expr))

View file

@ -91,71 +91,169 @@ namespace lib {
/** Helpers and building blocks for Monitor based synchronisation */
namespace sync {
struct Wrapped_LumieraExcMutex
: public lumiera_mutex
{
lumiera_mutex self;
protected:
Wrapped_LumieraExcMutex() { lumiera_mutex_init (this, "Obj.Monitor ExclMutex", &NOBUG_FLAG(sync)); }
~Wrapped_LumieraExcMutex() { lumiera_mutex_destroy (this, &NOBUG_FLAG(sync)); }
Wrapped_LumieraExcMutex() { lumiera_mutex_init (&self, "Obj.Monitor ExclMutex", &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------
void __may_block() { TODO ("Record we may block on mutex"); }
void __enter() { TODO ("Record we successfully acquired the mutex"); }
void __left() { TODO ("Record we released the mutex"); }
bool trylock (struct nobug_resource_user** handle)
{
return !!lumiera_mutex_trylock (&self, &NOBUG_FLAG(sync), handle);
}
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
: public lumiera_recmutex
{
lumiera_recmutex self;
protected:
Wrapped_LumieraRecMutex() { lumiera_recmutex_init (this, "Obj.Monitor RecMutex", &NOBUG_FLAG(sync)); }
~Wrapped_LumieraRecMutex() { lumiera_recmutex_destroy (this, &NOBUG_FLAG(sync)); }
Wrapped_LumieraRecMutex() { lumiera_recmutex_init (&self, "Obj.Monitor RecMutex", &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------
void __may_block() { TODO ("Record we may block on mutex"); }
void __enter() { TODO ("Record we successfully acquired the mutex"); }
void __left() { TODO ("Record we are released the mutex"); }
bool trylock (struct nobug_resource_user** handle)
{
return !!lumiera_recmutex_trylock (&self, &NOBUG_FLAG(sync), handle);
}
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
: public lumiera_condition
{
lumiera_condition self;
protected:
Wrapped_LumieraExcCond() { lumiera_condition_init (this, "Obj.Monitor ExclCondition", &NOBUG_FLAG(sync) ); }
~Wrapped_LumieraExcCond() { lumiera_condition_destroy (this, &NOBUG_FLAG(sync) ); }
Wrapped_LumieraExcCond() { lumiera_condition_init (&self, "Obj.Monitor ExclCondition", &NOBUG_FLAG(sync) ); }
~Wrapped_LumieraExcCond() { lumiera_condition_destroy (&self, &NOBUG_FLAG(sync) ); }
pthread_mutex_t* mutex() { return &cndmutex; }
pthread_cond_t* condv() { return &cond; }
bool lock (struct nobug_resource_user** handle)
{
return !!lumiera_condition_lock (&self, &NOBUG_FLAG(sync), handle);
}
//------------------Resource-Tracking------
void __may_block() { TODO ("Record we may block on mutex"); }
void __enter() { TODO ("Record we successfully acquired the mutex"); }
void __left() { TODO ("Record we released the mutex"); }
bool trylock (struct nobug_resource_user** handle)
{
return !!lumiera_condition_trylock (&self, &NOBUG_FLAG(sync), handle);
}
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
: public lumiera_reccondition
{
lumiera_reccondition self;
protected:
Wrapped_LumieraRecCond() { lumiera_reccondition_init (this, "Obj.Monitor RecCondition", &NOBUG_FLAG(sync) ); } ////////TODO
~Wrapped_LumieraRecCond() { lumiera_reccondition_destroy (this, &NOBUG_FLAG(sync) ); }
Wrapped_LumieraRecCond() { lumiera_reccondition_init (&self, "Obj.Monitor RecCondition", &NOBUG_FLAG(sync) ); }
~Wrapped_LumieraRecCond() { lumiera_reccondition_destroy (&self, &NOBUG_FLAG(sync) ); }
pthread_mutex_t* mutex() { return &reccndmutex; }
pthread_cond_t* condv() { return &cond; }
bool lock (struct nobug_resource_user** handle)
{
return !!lumiera_reccondition_lock (&self, &NOBUG_FLAG(sync), handle);
}
//------------------Resource-Tracking------
void __may_block() { TODO ("Record we may block on mutex"); }
void __enter() { TODO ("Record we successfully acquired the mutex"); }
void __left() { TODO ("Record we released the mutex"); }
bool trylock (struct nobug_resource_user** handle)
{
return !!lumiera_reccondition_trylock (&self, &NOBUG_FLAG(sync), handle);
}
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:
using MTX::mutex;
using MTX::__may_block;
using MTX::__enter;
using MTX::__left;
~Mutex () { }
Mutex () { }
@ -179,22 +273,15 @@ namespace lib {
public:
void
acquire()
acquire(struct nobug_resource_user** handle)
{
__may_block();
if (pthread_mutex_lock (mutex()))
throw lumiera::error::State("Mutex acquire failed."); ///////TODO capture the error-code
__enter();
MTX::lock(handle);
}
void
release()
{
pthread_mutex_unlock (mutex());
__left();
release(struct nobug_resource_user** handle)
{
MTX::unlock(handle);
}
};
@ -207,41 +294,38 @@ namespace lib {
class Condition
: public Mutex<CDX>
{
protected:
using CDX::condv;
using CDX::mutex;
public:
void
signal (bool wakeAll=false)
{
if (wakeAll)
pthread_cond_broadcast (condv());
CDX::broadcast ();
else
pthread_cond_signal (condv());
CDX::signal ();
}
template<class BF>
bool
wait (BF& predicate, Timeout& waitEndTime)
wait (struct nobug_resource_user** handle, BF& predicate, Timeout& waitEndTime)
{
int err=0;
while (!predicate() && !err)
bool ok = true;
while (ok && !predicate())
if (waitEndTime)
err = pthread_cond_timedwait (condv(), mutex(), &waitEndTime);
ok = CDX::timedwait (&waitEndTime, handle);
else
err = pthread_cond_wait (condv(), mutex());
ok = CDX::wait (handle);
if (!err) return true;
if (ETIMEDOUT==err) return false;
if (!ok && lumiera_error_expect(LUMIERA_ERROR_LOCK_TIMEOUT)) 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.
* Wrapping a timespec-struct, it allows for easy initialisation
* by a given relative offset.
@ -320,30 +404,30 @@ namespace lib {
const Monitor& operator= (Monitor const& ref) { timeout_ = ref.timeout_; }
void acquireLock() { IMPL::acquire(); }
void releaseLock() { IMPL::release(); }
void acquireLock(struct nobug_resource_user** handle) { IMPL::acquire(handle); }
void releaseLock(struct nobug_resource_user** handle) { IMPL::release(handle); }
void signal(bool a){ IMPL::signal(a); }
bool
wait (Flag flag, ulong timedwait=0)
wait (struct nobug_resource_user** handle, Flag flag, ulong timedwait=0)
{
BoolFlagPredicate checkFlag(flag);
return IMPL::wait(checkFlag, timeout_.setOffset(timedwait));
return IMPL::wait(handle, checkFlag, timeout_.setOffset(timedwait));
}
template<class X>
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);
return IMPL::wait(invokeMethod, timeout_.setOffset(timedwait));
return IMPL::wait(handle, invokeMethod, timeout_.setOffset(timedwait));
}
void setTimeout(ulong relative) {timeout_.setOffset(relative);}
bool isTimedWait() {return (timeout_);}
LumieraReccondition accessCond(){return static_cast<LumieraReccondition> (this);}
LumieraReccondition accessCond(){return IMPL::myreccond();}
};
typedef Mutex<Wrapped_LumieraExcMutex> NonrecursiveLock_NoWait;
@ -357,9 +441,6 @@ namespace lib {
/* Interface to be used by client code:
* Inherit from class Sync with a suitable Policy.
* 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.
* Typically, the client class will inherit from this template (but it
* is possible to use it stand-alone, if inheriting isn't an option).
* The interface for clients to access the functionality is the embedded
* Lock template, which should be instantiated as an automatic variable
* within the scope to be protected.
*
*
*/
template<class CONF = NonrecursiveLock_NoWait>
class Sync
@ -404,9 +485,11 @@ namespace lib {
Monitor& mon_;
public:
RESOURCE_USER (handle); // temporary public until thread impl settles the locking
template<class X>
Lock(X* it) : mon_(getMonitor(it)){ mon_.acquireLock(); }
~Lock() { mon_.releaseLock(); }
Lock(X* it) : mon_(getMonitor(it)){ RESOURCE_USER_INIT (handle); mon_.acquireLock(&handle); }
~Lock() { mon_.releaseLock(&handle); }
void notify() { mon_.signal(false);}
void notifyAll() { mon_.signal(true); }
@ -417,31 +500,32 @@ namespace lib {
bool
wait (C& cond, ulong timeout=0)
{
return mon_.wait(cond,timeout);
return mon_.wait(&handle, cond, timeout);
}
template<typename X>
bool
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,
* observing a condition defined as member function. */
template<class X>
Lock(X* it, bool (X::*method)(void))
: mon_(getMonitor(it))
{
mon_.acquireLock();
mon_.wait(*it,method);
RESOURCE_USER_INIT (handle);
mon_.acquireLock(&handle);
mon_.wait(&handle, *it, method);
}
protected:
/** for creating a ClassLock */
Lock(Monitor& m) : mon_(m)
{ mon_.acquireLock(); }
{ RESOURCE_USER_INIT (handle); mon_.acquireLock(&handle); }
/** for controlled access to the
* underlying sync primitives */