]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Implement Mutex and ConditionVariable using boost or C++0x support.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 19 Jan 2009 00:01:03 +0000 (00:01 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 19 Jan 2009 00:01:03 +0000 (00:01 +0000)
git-svn-id: https://svn.dealii.org/trunk@18230 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/thread_management.h
deal.II/base/source/thread_management.cc
deal.II/doc/news/changes.h

index 2c0ddb3249fd4a4a3a18bebd92437552ca864fe3..a7d8a66de7924d813909005295945a2f7246ab1c 100644 (file)
@@ -23,6 +23,8 @@
 
 #if (DEAL_II_USE_MT == 1)
 #  include <base/std_cxx0x/thread.h>
+#  include <base/std_cxx0x/mutex.h>
+#  include <base/std_cxx0x/condition_variable.h>
 #endif
 
 #include <iterator>
 #include <utility>
 
 
-#if DEAL_II_USE_MT == 1
-#  if defined(DEAL_II_USE_MT_POSIX)
-#    include <pthread.h>
-#  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
-                                        * <tt>pthread_mutex_t</tt>
-                                        * 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 <tt>posix_cond_wait</tt> 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<std_cxx0x::mutex> 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
index 8166a37d664651edee4ce14f80b6b566743a779d..fa9ef7fa178d8f512cc6fc8722772384c922bbf0 100644 (file)
@@ -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,
index 9336435948ad18c922d2682792dac11f8a497240..3444e5ba004835c0069a6cd79cc8f1ae3f1ebbce 100644 (file)
@@ -351,6 +351,15 @@ inconvenience this causes.
 <h3>base</h3>
 
 <ol>
+  <li>
+  <p>
+  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.
+  <br> 
+  (Martin Kronbichler 2009/01/14)
+  </p>
+
   <li>
   <p>
   New: There is now a class TimerOutput that allows to neatly measure computing

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.