]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Implement dummy thread creation and mutices to be able to use the same interfaces...
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 19 May 2000 15:54:06 +0000 (15:54 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 19 May 2000 15:54:06 +0000 (15:54 +0000)
git-svn-id: https://svn.dealii.org/trunk@2903 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/thread_management.h
deal.II/base/source/thread_management.cc

index 1e46b09c48f0d1b3c58380e48ce3d1d28ca70225..bc5b9a0ae3e7446c9f2e8cf0abf229a356e0b6b6 100644 (file)
 
 
 #ifdef DEAL_II_USE_MT
+#  include <base/exceptions.h>
+#  include <ace/Thread_Manager.h>
+#  include <ace/Synch.h>
+#endif
 
-#include <base/exceptions.h>
-#include <ace/Thread_Manager.h>
-#include <ace/Synch.h>
-
-#include <string>
 #include <utility>
 #include <vector>
 #include <iterator>
@@ -33,6 +32,114 @@ namespace Threads
                                   // forward declarations
   class FunDataBase;
 
+/**
+ * This class is used instead of a true lock class when not using
+ * multithreading. It allows to write programs such that they start
+ * new threads and/or lock objects in multithreading mode, and use
+ * dummy thread management and synchronisation classes instead when
+ * running in single-thread mode. Specifically, the #spawn# functions
+ * only call the function but wait for it to return instead of running
+ * in on another thread, and the mutices do nothing really. The only
+ * reason to provide such a function is that the program can be
+ * compiled both in MT and non-MT mode without difference.
+ *
+ * @author Wolfgang Bangerth, 2000
+ */
+  class DummyThreadMutex
+  {
+    public:
+                                      /**
+                                       * Simulate acquisition of the
+                                       * mutex. As this class does
+                                       * nothing really, this
+                                       * function does nothing as
+                                       * well.
+                                       */
+      inline void acquire () const {};
+
+                                      /**
+                                       * Simulate release of the
+                                       * mutex. As this class does
+                                       * nothing really, this
+                                       * function does nothing as
+                                       * well.
+                                       */
+      inline void release () const {};
+  };
+
+
+/**
+ * This class is used instead of a true thread manager class when not
+ * using multithreading. It allows to write programs such that they
+ * start new threads and/or lock objects in multithreading mode, and
+ * use dummy thread management and synchronisation classes instead
+ * when running in single-thread mode. Specifically, the #spawn#
+ * functions only call the function but wait for it to return instead
+ * of running in on another thread, and the mutices do nothing
+ * really. The only reason to provide such a function is that the
+ * program can be compiled both in MT and non-MT mode without
+ * difference.
+ *
+ * @author Wolfgang Bangerth, 2000
+ */
+  class DummyThreadManager 
+  {
+    public:
+                                      /**
+                                       * Typedef for a global
+                                       * function that might be
+                                       * called on a new thread.
+                                       */
+      typedef void * (*FunPtr) (void *);
+
+                                      /**
+                                       * Emulate spawning a new
+                                       * thread and calling the
+                                       * passed function
+                                       * thereon. Actually only call
+                                       * that function with the given
+                                       * parameters, and wait for it
+                                       * to return.
+                                       */
+      void spawn (const FunPtr fun_ptr,
+                 void *       fun_data,
+                 int          flags) const;
+
+                                      /**
+                                       * Emulate that we wait for
+                                       * other threads to
+                                       * return. Since no other
+                                       * threads have been started,
+                                       * the right thing, of course,
+                                       * is to return immediately.
+                                       */
+      inline void wait () const {};
+  };
+
+  
+#ifdef DEAL_II_USE_MT
+                                  /**
+                                   * In multithread mode, we alias
+                                   * the mutex and thread management
+                                   * classes to the respective
+                                   * classes of the ACE library.
+                                   */
+  typedef ACE_Thread_Mutex   ThreadMutex;
+  typedef ACE_Thread_Manager ThreadManager;
+#else
+                                  /**
+                                   * In non-multithread mode, the
+                                   * mutex and thread management
+                                   * classes are aliased to dummy
+                                   * classes that actually do
+                                   * nothing, in particular not lock
+                                   * objects or start new threads.
+                                   */
+  typedef DummyThreadMutex   ThreadMutex;
+  typedef ThreadManager      ThreadManager;
+#endif
+
+  
 
 /**
  * Class used to store a pointer temporarily and delete the object
@@ -151,7 +258,7 @@ namespace Threads
                                        * no data is deleted when it is
                                        * still in use.
                                        */
-      mutable ACE_Thread_Mutex lock;
+      mutable ThreadMutex lock;
     
     private:
                                       /**
@@ -169,8 +276,8 @@ namespace Threads
                                        * needs to have access to the
                                        * #thread_entry_point# variable.
                                        */
-      friend void spawn (ACE_Thread_Manager       &thread_manager,
-                        const FunEncapsulation   &fun_data);
+      friend void spawn (ThreadManager          &thread_manager,
+                        const FunEncapsulation &fun_data);
   };
 
 
@@ -1973,7 +2080,7 @@ namespace Threads
                                    * using the given thread manager
                                    * object.
                                    */
-  void spawn (ACE_Thread_Manager     &thread_manager,
+  void spawn (ThreadManager          &thread_manager,
              const FunEncapsulation &fun_data);
 
 
@@ -1982,7 +2089,7 @@ namespace Threads
                                    * using the same parameters and
                                    * calling the same function.
                                    */
-  void spawn_n (ACE_Thread_Manager     &thread_manager,
+  void spawn_n (ThreadManager          &thread_manager,
                const FunEncapsulation &fun_encapsulation,
                const unsigned int      n_threads);
 
@@ -2076,7 +2183,7 @@ namespace Threads
                                        * Exception
                                        */
       DeclException2 (ExcObjectsExist,
-                     string, int,
+                     char*, int,
                      << "There are still " << arg2 << " objects of type "
                      << arg1 << " alive. You probably have a memory "
                      << "leak somewhere.");
@@ -3554,7 +3661,6 @@ namespace Threads
 };   // end of implementation of namespace Threads
 
 
-#endif // DEAL_II_USE_MT
 
 
 //----------------------------   thread_management.h     ---------------------------
index e94b1eac67318ef8ee5c4b9e2fdf72f58dda95bc..bae4b33492ed166f99521aa3933326900837e7a5 100644 (file)
 
 namespace Threads 
 {
+  void DummyThreadManager::spawn (const FunPtr fun_ptr,
+                                 void *       fun_data,
+                                 int          /*flags*/) const
+  {
+    (*fun_ptr) (fun_data);
+  };
+  
+
+  
   FunDataCounter::FunDataCounter () :
                  n_fun_encapsulation_objects (0),
                  n_fun_data_base_objects (0)
@@ -135,7 +144,7 @@ namespace Threads
 
 
     
-  void spawn (ACE_Thread_Manager     &thread_manager,
+  void spawn (ThreadManager          &thread_manager,
              const FunEncapsulation &fun_data)
   {
                                     // lock the #fun_data_base# object
@@ -152,7 +161,7 @@ namespace Threads
 
 
   
-  void spawn_n (ACE_Thread_Manager     &thread_manager,
+  void spawn_n (ThreadManager          &thread_manager,
                const FunEncapsulation &fun_data,
                const unsigned int      n_threads)
   {

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.