getfem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Getfem-commits] r4660 - in /trunk/getfem/src: getfem/getfem_context.h g


From: andriy . andreykiv
Subject: [Getfem-commits] r4660 - in /trunk/getfem/src: getfem/getfem_context.h getfem_context.cc
Date: Thu, 22 May 2014 12:32:30 -0000

Author: andrico
Date: Thu May 22 14:32:30 2014
New Revision: 4660

URL: http://svn.gna.org/viewcvs/getfem?rev=4660&view=rev
Log:
"Thread safing" context_dependencies

Modified:
    trunk/getfem/src/getfem/getfem_context.h
    trunk/getfem/src/getfem_context.cc

Modified: trunk/getfem/src/getfem/getfem_context.h
URL: 
http://svn.gna.org/viewcvs/getfem/trunk/getfem/src/getfem/getfem_context.h?rev=4660&r1=4659&r2=4660&view=diff
==============================================================================
--- trunk/getfem/src/getfem/getfem_context.h    (original)
+++ trunk/getfem/src/getfem/getfem_context.h    Thu May 22 14:32:30 2014
@@ -38,7 +38,16 @@
 #define GETFEM_CONTEXT_H__
 
 #include "getfem_config.h"
+#include "getfem_omp.h"
 #include <list>
+
+#ifdef GETFEM_HAVE_OPENMP
+  #include <boost/atomic.hpp>
+  typedef boost::atomic_bool atomic_bool;
+#else
+  typedef bool  atomic_bool;
+#endif
+
 
 namespace getfem {
   /**Deal with interdependencies of objects.
@@ -80,10 +89,11 @@
   protected :
     enum context_state { CONTEXT_NORMAL, CONTEXT_CHANGED, CONTEXT_INVALID };
     mutable context_state state;
-    mutable bool touched;
+    mutable atomic_bool touched;
     mutable std::vector<const context_dependencies *> dependencies;
     mutable std::vector<const context_dependencies *> dependent;
     typedef std::vector<const context_dependencies *>::iterator iterator_list;
+    getfem::lock_factory locks_;
 
     void sup_dependent_(const context_dependencies &cd) const;
     void sup_dependency_(const context_dependencies &cd) const;
@@ -96,21 +106,34 @@
     virtual void update_from_context(void) const = 0;
 
     void change_context(void) const
-    { if (state == CONTEXT_NORMAL) { state = CONTEXT_CHANGED; touch(); } }
+    { 
+      if (state == CONTEXT_NORMAL) 
+      { 
+        touch(); 
+        getfem::local_guard lock = locks_.get_lock();
+        state = CONTEXT_CHANGED; 
+      } 
+    }
     void add_dependency(const context_dependencies &cd);
+    
     void sup_dependency(const context_dependencies &cd)
-    { cd.sup_dependent_(*this); sup_dependency_(cd); }
+    { 
+      cd.sup_dependent_(*this); 
+      sup_dependency_(cd); 
+    }
+
     bool is_context_valid(void) const { return (state != CONTEXT_INVALID); }
     bool is_context_changed() const { return (state == CONTEXT_CHANGED); }
     /** return true if update_from_context was called */
     bool context_check(void) const;
     void touch(void) const;
     virtual ~context_dependencies();
-    context_dependencies() : state(CONTEXT_NORMAL), touched(false) {}
+    context_dependencies() : state(CONTEXT_NORMAL), touched( ) {touched = 
false;}
+    context_dependencies(const context_dependencies& cd);
 
   };
 
 }  /* end of namespace getfem.                                             */
 
 
-#endif /* GETFEM_CONTEXT_H__  */
+#endif /* GETFEM_CONTEXT_H__  */

Modified: trunk/getfem/src/getfem_context.cc
URL: 
http://svn.gna.org/viewcvs/getfem/trunk/getfem/src/getfem_context.cc?rev=4660&r1=4659&r2=4660&view=diff
==============================================================================
--- trunk/getfem/src/getfem_context.cc  (original)
+++ trunk/getfem/src/getfem_context.cc  Thu May 22 14:32:30 2014
@@ -25,8 +25,17 @@
 
 namespace getfem {
 
+  context_dependencies::context_dependencies(const context_dependencies& cd) : 
+    state(cd.state),
+    touched(static_cast<bool>(cd.touched)),
+    dependencies(cd.dependencies),
+    dependent(cd.dependent),
+    locks_( )
+  {}
+
   void context_dependencies::sup_dependent_
   (const context_dependencies &cd) const {
+    getfem::local_guard lock = locks_.get_lock();
     size_type s = dependent.size();
     iterator_list it1 = dependent.begin(), it2 = it1, ite = dependent.end();
     for (; it1 != ite; ++it1)
@@ -36,6 +45,7 @@
   
   void context_dependencies::sup_dependency_
   (const context_dependencies &cd) const {
+    getfem::local_guard lock = locks_.get_lock();
     size_type s = dependencies.size();
     iterator_list it1=dependencies.begin(), it2=it1, ite=dependencies.end();
     for (; it1 != ite; ++it1)
@@ -44,27 +54,39 @@
   }
 
   void context_dependencies::invalid_context(void) const {
-    if (state != CONTEXT_INVALID) {
-      state = CONTEXT_INVALID;
+    if (state != CONTEXT_INVALID) 
+    {
       iterator_list it = dependent.begin(), ite = dependent.end();
       for (; it != ite; ++it) (*it)->invalid_context();
+      getfem::local_guard lock = locks_.get_lock();
+      state = CONTEXT_INVALID;
     }
   }
 
   void context_dependencies::add_dependency(const context_dependencies &cd) {
     cd.context_check(); cd.touched = false;
-    iterator_list it = dependencies.begin(), ite = dependencies.end();
-    for (; it != ite; ++it) if ((*it) == &cd) return;
-    dependencies.push_back(&cd);
+    {
+      getfem::local_guard lock = locks_.get_lock();
+      iterator_list it = dependencies.begin(), ite = dependencies.end();
+      for (; it != ite; ++it) if ((*it) == &cd) return;
+      dependencies.push_back(&cd);
+    }
+    getfem::local_guard lock = cd.locks_.get_lock();
     cd.dependent.push_back(this);
   }
   
-  bool context_dependencies::context_check(void) const {
-    if (state == CONTEXT_CHANGED) {
+  bool context_dependencies::context_check(void) const 
+  {
+    if (state == CONTEXT_CHANGED) 
+    {
+      iterator_list it = dependencies.begin(), ite = dependencies.end();
+      for (; it != ite; ++it) 
+      {
+        (*it)->context_check(); 
+        (*it)->touched = false;
+      }
+      getfem::local_guard lock = locks_.get_lock();
       state = CONTEXT_NORMAL;
-      iterator_list it = dependencies.begin(), ite = dependencies.end();
-      for (; it != ite; ++it)
-       { (*it)->context_check(); (*it)->touched = false; }
       update_from_context();
       return true;
     }
@@ -72,11 +94,13 @@
     return false;
   }
   
-  void context_dependencies::touch(void) const {
-    if (!touched) {
-      touched = true;
+  void context_dependencies::touch(void) const 
+  {
+    if (!touched) 
+    {
       iterator_list it = dependent.begin(), ite = dependent.end();
       for (; it != ite; ++it) (*it)->change_context();
+      touched = true;
     }
   }
  
@@ -88,4 +112,4 @@
     for (; it != ite; ++it) (*it)->sup_dependency_(*this);
   }
   
-}
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]