From 665e3dadfe5bb68a86d68ab5d67bca795e98a586 Mon Sep 17 00:00:00 2001 From: bangerth Date: Mon, 19 Jan 2009 00:01:03 +0000 Subject: [PATCH] Implement Mutex and ConditionVariable using boost or C++0x support. git-svn-id: https://svn.dealii.org/trunk@18230 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/thread_management.h | 119 +++++++----------- deal.II/base/source/thread_management.cc | 25 ---- deal.II/doc/news/changes.h | 9 ++ 3 files changed, 53 insertions(+), 100 deletions(-) diff --git a/deal.II/base/include/base/thread_management.h b/deal.II/base/include/base/thread_management.h index 2c0ddb3249..a7d8a66de7 100644 --- a/deal.II/base/include/base/thread_management.h +++ b/deal.II/base/include/base/thread_management.h @@ -23,6 +23,8 @@ #if (DEAL_II_USE_MT == 1) # include +# include +# include #endif #include @@ -31,14 +33,6 @@ #include -#if DEAL_II_USE_MT == 1 -# if defined(DEAL_II_USE_MT_POSIX) -# include -# endif -#endif - - - DEAL_II_NAMESPACE_OPEN @@ -268,15 +262,13 @@ namespace Threads #if DEAL_II_USE_MT == 1 -# if defined(DEAL_II_USE_MT_POSIX) /** - * Class implementing a Mutex with - * the help of POSIX functions. + * Class implementing a Mutex. * - * @author Wolfgang Bangerth, 2002, 2003 + * @author Wolfgang Bangerth, 2002, 2003, 2009 */ - class PosixThreadMutex + class Mutex { public: /** @@ -319,7 +311,7 @@ namespace Threads * Constructor. Lock the * mutex. */ - ScopedLock (PosixThreadMutex &m) : mutex(m) { mutex.acquire(); } + ScopedLock (Mutex &m) : mutex(m) { mutex.acquire(); } /** * Destructor. Unlock the @@ -334,85 +326,54 @@ namespace Threads * Store the address of the * mutex object. */ - PosixThreadMutex &mutex; + Mutex &mutex; }; - /** - * Constructor. Initialize the - * underlying POSIX mutex data - * structure. - */ - PosixThreadMutex (); - - /** - * Destructor. Release all - * resources. - */ - ~PosixThreadMutex (); - /** * Acquire a mutex. */ - inline void acquire () { pthread_mutex_lock(&mutex); } + inline void acquire () { mutex.lock(); } /** * Release the mutex again. */ - inline void release () { pthread_mutex_unlock(&mutex); } + inline void release () { mutex.unlock(); } private: /** * Data object storing the - * POSIX data which we need to - * call the POSIX functions. + * mutex data */ - pthread_mutex_t mutex; + std_cxx0x::mutex mutex; /** * Make the class implementing - * condition variables a - * friend, since it needs - * access to the - * pthread_mutex_t - * structure. + * condition variables a friend, since + * it needs to access the mutex. */ - friend class PosixThreadCondition; + friend class ConditionVariable; }; /** * Class implementing a condition - * variable with the help of POSIX - * functions. The semantics of this + * variable. The semantics of this * class and its member functions * are the same as those of the * POSIX functions. * * @author Wolfgang Bangerth, 2003 */ - class PosixThreadCondition + class ConditionVariable { public: - /** - * Constructor. Initialize the - * underlying POSIX condition - * variable data structure. - */ - PosixThreadCondition (); - - /** - * Destructor. Release all - * resources. - */ - ~PosixThreadCondition (); - /** * Signal to a single listener * that a condition has been * met, i.e. that some data * will now be available. */ - inline void signal () { pthread_cond_signal(&cond); } + inline void signal () { condition_variable.notify_one(); } /** * Signal to multiple listener @@ -420,7 +381,7 @@ namespace Threads * met, i.e. that some data * will now be available. */ - inline void broadcast () { pthread_cond_broadcast(&cond); } + inline void broadcast () { condition_variable.notify_all(); } /** * Wait for the condition to be @@ -432,17 +393,24 @@ namespace Threads * of posix_cond_wait for a * description of the * mechanisms. + * + * The mutex is assumed held at the + * entry to this function but is + * released upon exit. */ - inline void wait (PosixThreadMutex &mutex) - { pthread_cond_wait(&cond, &mutex.mutex); } + inline void wait (Mutex &mutex) + { + std_cxx0x::unique_lock lock(mutex.mutex, + std_cxx0x::adopt_lock); + condition_variable.wait (lock); + } private: /** * Data object storing the - * POSIX data which we need to - * call the POSIX functions. + * necessary data. */ - pthread_cond_t cond; + std_cxx0x::condition_variable condition_variable; }; @@ -521,20 +489,24 @@ namespace Threads /** - * If using POSIX functions, then - * alias the POSIX wrapper classes - * to the names we use throughout - * the library. + * Provide a backward compatible name (we + * used ThreadMutex up to release 6.1, but + * since it is already in a namespace + * Threads this seems redundant). + * + * @deprecated */ - typedef PosixThreadMutex ThreadMutex; + typedef Mutex ThreadMutex; /** - * If using POSIX functions, then - * alias the POSIX wrapper classes - * to the names we use throughout - * the library. + * Provide a backward compatible name (we + * used ThreadCondition up to release 6.1, + * but since it is already in a namespace + * Threads this seems redundant). + * + * @deprecated */ - typedef PosixThreadCondition ThreadCondition; + typedef ConditionVariable ThreadCondition; /** * If using POSIX functions, then @@ -544,9 +516,6 @@ namespace Threads */ typedef PosixThreadBarrier Barrier; -# else -# error Not Implemented -# endif #else /** * In non-multithread mode, the diff --git a/deal.II/base/source/thread_management.cc b/deal.II/base/source/thread_management.cc index 8166a37d66..fa9ef7fa17 100644 --- a/deal.II/base/source/thread_management.cc +++ b/deal.II/base/source/thread_management.cc @@ -143,31 +143,6 @@ namespace Threads #else # ifdef DEAL_II_USE_MT_POSIX - PosixThreadMutex::PosixThreadMutex () - { - pthread_mutex_init (&mutex, 0); - } - - - - PosixThreadMutex::~PosixThreadMutex () - { - pthread_mutex_destroy (&mutex); - } - - - PosixThreadCondition::PosixThreadCondition () - { - pthread_cond_init (&cond, 0); - } - - - - PosixThreadCondition::~PosixThreadCondition () - { - pthread_cond_destroy (&cond); - } - #ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS PosixThreadBarrier::PosixThreadBarrier (const unsigned int count, diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 9336435948..3444e5ba00 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -351,6 +351,15 @@ inconvenience this causes.

base

    +
  1. +

    + Changed: The classes Threads::ThreadMutex and Threads::ThreadCondition have + been renamed Threads::Mutex and Threads::ConditionVariable. The old names + were somewhat redundant but have been retained as typedefs for compatibility. +
    + (Martin Kronbichler 2009/01/14) +

    +
  2. New: There is now a class TimerOutput that allows to neatly measure computing -- 2.39.5