From 67e95884d5096a51fec4e0a8e8e6406bf445a8a2 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 22 Dec 2008 07:23:48 +0100 Subject: [PATCH] preliminary implementation using pthread primitives. Locking test pass. --- src/include/nobugcfg.h | 2 ++ src/lib/concurrency.hpp | 50 ++++++++++++++++++-------- tests/lib/concurrency-locking-test.cpp | 10 ++---- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/include/nobugcfg.h b/src/include/nobugcfg.h index cfcedd6b7..d8ea8ab26 100644 --- a/src/include/nobugcfg.h +++ b/src/include/nobugcfg.h @@ -88,6 +88,7 @@ namespace lumiera { NOBUG_DECLARE_FLAG (render); ///< logging channel focusing on the render engine's workings NOBUG_DECLARE_FLAG (config); ///< logging channel covering application and session configuration NOBUG_DECLARE_FLAG (memory); ///< logging channel covering memory management issues + NOBUG_DECLARE_FLAG (sync); ///< especially for tracing synchronisation NOBUG_DECLARE_FLAG (test); @@ -109,6 +110,7 @@ namespace lumiera { NOBUG_CPP_DEFINE_FLAG_PARENT (operate, lumiera); NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT (render, lumiera, LOG_WARNING); NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT (memory, lumiera, LOG_WARNING); + NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT (sync, memory, LOG_WARNING); NOBUG_CPP_DEFINE_FLAG_PARENT_LIMIT (test, all, LOG_ERR); diff --git a/src/lib/concurrency.hpp b/src/lib/concurrency.hpp index d03a4edb1..d54952988 100644 --- a/src/lib/concurrency.hpp +++ b/src/lib/concurrency.hpp @@ -47,14 +47,43 @@ extern "C" { #include -#include ///////////////////////////TODO -using std::cerr; namespace lib { using boost::scoped_ptr; + + + namespace sync { // Helpers for Monitor based synchronisation + class RecMutex + { + lumiera_mutex mtx_; + + public: + RecMutex() { lumiera_recmutex_init (&mtx_, "Obj.Monitor RecMutex", &NOBUG_FLAG (sync)); } + ~RecMutex() { lumiera_mutex_destroy (&mtx_, &NOBUG_FLAG (sync)); } + + void acquire() + { + TODO ("Record we may block on mutex"); + + if (pthread_mutex_lock (&mtx_.mutex)) + throw lumiera::error::State("Mutex acquire failed."); ///////TODO capture the error-code + + TODO ("Record we successfully acquired the mutex"); + } + void release() + { + TODO ("Record we are releasing the mutex"); + pthread_mutex_unlock (&mtx_.mutex); + } + }; + + } // namespace sync + + + /** * Facility for monitor object based locking. * To be attached either on a per class base or per object base. @@ -70,19 +99,12 @@ namespace lib { { struct Monitor { - lumiera_mutex mtx_; + sync::RecMutex mtx_; + Monitor() {} + ~Monitor() {} - Monitor() - { - lumiera_recmutex_init (&mtx_, "Monitor object", &NOBUG_FLAG (memory)); - } - ~Monitor() - { - lumiera_mutex_destroy (&mtx_, &NOBUG_FLAG (memory)); - } - - void acquireLock() { cerr << "acquire Thread Lock\n"; } - void releaseLock() { cerr << "release Thread Lock\n"; } + void acquireLock() { mtx_.acquire(); } + void releaseLock() { mtx_.release(); } }; Monitor objectMonitor_; diff --git a/tests/lib/concurrency-locking-test.cpp b/tests/lib/concurrency-locking-test.cpp index 86087e1c5..32f1eb744 100644 --- a/tests/lib/concurrency-locking-test.cpp +++ b/tests/lib/concurrency-locking-test.cpp @@ -22,21 +22,14 @@ #include "lib/test/run.hpp" -//#include "common/util.hpp" #include "include/error.hpp" #include "lib/concurrency.hpp" #include -//#include -//#include #include -//using boost::lexical_cast; -//using boost::format; -//using util::isnil; -//using std::string; using std::cout; using test::Test; @@ -178,7 +171,8 @@ namespace lib { class ConcurrencyLocking_test : public Test { - virtual void run(Arg arg) + virtual void + run (Arg) { if (!Glib::thread_supported()) Glib::thread_init();