From c29ea07138eb1e204fb8f7131fa294817b277f3d Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Wed, 13 Jan 2010 17:41:26 +0100 Subject: [PATCH 1/4] lumiera_error_expect() lets one easily handle expected errors This is a convenience function which clears the error state when some expected error occurred and lets one branch on this. --- src/lib/error.c | 16 ++++++++++++++++ src/lib/error.h | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/lib/error.c b/src/lib/error.c index 840288c8d..601b84e47 100644 --- a/src/lib/error.c +++ b/src/lib/error.c @@ -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; +} diff --git a/src/lib/error.h b/src/lib/error.h index 308adf480..1449c4fa2 100644 --- a/src/lib/error.h +++ b/src/lib/error.h @@ -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 */ From 3e8d8590e90ed4f1d9ac8daa029f3b89938b5dcf Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Wed, 13 Jan 2010 20:30:10 +0100 Subject: [PATCH 2/4] C lumiera_error to C++ exception bridge * error::Runtime exception class which transports the C error code * lumiera::throwOnError() which throws when there is a pending lumiera error, otherwise it does nothing --- src/lib/error.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib/error.hpp b/src/lib/error.hpp index daf2cf242..412a422ac 100644 --- a/src/lib/error.hpp +++ b/src/lib/error.hpp @@ -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 From dbb3b2e2e755efee8a33ded087050fb601ce35ca Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Thu, 14 Jan 2010 13:22:19 +0100 Subject: [PATCH 3/4] FIX: uhm .. forgotten to fix some stuff --- src/lib/condition.h | 10 ++++++---- src/lib/reccondition.h | 26 ++++++++++++++------------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/lib/condition.h b/src/lib/condition.h index 293d0143e..51abfb61a 100644 --- a/src/lib/condition.h +++ b/src/lib/condition.h @@ -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; \ @@ -123,7 +125,7 @@ */ #define LUMIERA_CONDITION_SIGNAL \ do { \ - REQUIRE (lumiera_cond_section_.lock, "Condition mutex not locked"); \ + REQUIRE (lumiera_lock_section_.lock, "Condition mutex not locked"); \ lumiera_condition_signal (lumiera_lock_section_.lock, \ lumiera_lock_section_.flag); \ } while (0) @@ -136,7 +138,7 @@ */ #define LUMIERA_CONDITION_BROADCAST \ do { \ - REQUIRE (lumiera_cond_section_.lock, "Condition mutex not locked"); \ + REQUIRE (lumiera_lock_section_.lock, "Condition mutex not locked"); \ lumiera_condition_broadcast (lumiera_lock_section_.lock, \ lumiera_lock_section_.flag); \ } while (0) diff --git a/src/lib/reccondition.h b/src/lib/reccondition.h index fae19ea38..bbb321861 100644 --- a/src/lib/reccondition.h +++ b/src/lib/reccondition.h @@ -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)) From aea546554b095c796b50790a3832606f877b78ef Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Thu, 14 Jan 2010 21:15:13 +0100 Subject: [PATCH 4/4] reworked sync.hpp using libraries locking functions --- src/backend/thread-wrapper.hpp | 2 +- src/lib/sync.hpp | 258 ++++++++++++++++++++++----------- 2 files changed, 172 insertions(+), 88 deletions(-) diff --git a/src/backend/thread-wrapper.hpp b/src/backend/thread-wrapper.hpp index 730b4338b..cde254c5f 100644 --- a/src/backend/thread-wrapper.hpp +++ b/src/backend/thread-wrapper.hpp @@ -118,7 +118,7 @@ namespace backend { void join() { - accessMonitor().wait (*this, &JoinHandle::wakeupCheck); + accessMonitor().wait (&handle, *this, &JoinHandle::wakeupCheck); } }; diff --git a/src/lib/sync.hpp b/src/lib/sync.hpp index d62960a94..6cd0cd294 100644 --- a/src/lib/sync.hpp +++ b/src/lib/sync.hpp @@ -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 { - 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 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 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 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 (this);} + LumieraReccondition accessCond(){return IMPL::myreccond();} }; typedef Mutex 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 Sync @@ -404,9 +485,11 @@ namespace lib { Monitor& mon_; public: + RESOURCE_USER (handle); // temporary public until thread impl settles the locking + template - 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 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 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 */