GetFEM  5.4.2
getfem_context.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2004-2020 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file getfem_context.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date June 17, 2004.
35  @brief Deal with interdependencies of objects (getfem::context_dependencies).
36 */
37 #ifndef GETFEM_CONTEXT_H__
38 #define GETFEM_CONTEXT_H__
39 
40 #include "getfem_config.h"
41 #include "getfem_omp.h"
42 #include <list>
43 
44 #include <atomic>
45 
46 namespace getfem {
47  /**Deal with interdependencies of objects.
48 
49  An object can be in three different states :
50  NORMAL : no change is necessary
51  CHANGED : something in the context has changed and the update function
52  of the object has to be called.
53  INVALID : one of the dependencies desappears, the object is invalid
54  and should no longer be used.
55 
56  add_dependency(ct) : add a dependency to the dependency list.
57 
58  touch() : significate to the dependent objects that something
59  has change in the object. This make the dependent
60  objects to be in the CHANGED state
61 
62  context_check() : check if the object has to be updated. if it is the
63  case it makes first a check to the dependency list
64  and call the update function of the object.
65  (the update function of the dependencies are called
66  before the update function of the current object).
67 
68  context_valid() : says if the object has still a valid context
69 
70  Remarks :
71 
72  - A protection against round dependencies exists. In this case, the
73  order of call of the update functions can be arbitrary
74 
75  - Detection of context changes is very fast (control the
76  state). the touch operation can cover the whole tree of dependent
77  object. But this is the case only for the first touch operation
78  because once a dependent object is in the CHANGED state it will not
79  be considered by next touch operations.
80  */
81  class APIDECL context_dependencies {
82 
83  protected :
84  enum context_state { CONTEXT_NORMAL, CONTEXT_CHANGED, CONTEXT_INVALID };
85  mutable context_state state;
86  mutable std::atomic_bool touched;
87  mutable std::vector<const context_dependencies *> dependencies;
88  mutable std::vector<const context_dependencies *> dependent;
89  typedef std::vector<const context_dependencies *>::iterator iterator_list;
90  getfem::lock_factory locks_;
91 
92  void sup_dependent_(const context_dependencies &cd) const;
93  void sup_dependency_(const context_dependencies &cd) const;
94  void invalid_context() const;
95  bool go_check() const;
96 
97  public :
98 
99  /** this function has to be defined and should update the object when
100  the context is modified. */
101  virtual void update_from_context() const = 0;
102 
103  void change_context() const
104  {
105  if (state == CONTEXT_NORMAL)
106  {
107  touch();
108  getfem::local_guard lock = locks_.get_lock();
109  state = CONTEXT_CHANGED;
110  }
111  }
112  void add_dependency(const context_dependencies &cd);
113 
114  void sup_dependency(const context_dependencies &cd)
115  {
116  cd.sup_dependent_(*this);
117  sup_dependency_(cd);
118  }
119 
120  void clear_dependencies();
121 
122 
123  bool is_context_valid() const { return (state != CONTEXT_INVALID); }
124  bool is_context_changed() const { return (state == CONTEXT_CHANGED); }
125  /** return true if update_from_context was called */
126  bool context_check() const
127  { if (state == CONTEXT_NORMAL) return false; return go_check(); }
128  void touch() const;
129  virtual ~context_dependencies();
130  context_dependencies() : state(CONTEXT_NORMAL), touched( ) {touched = false;}
131  context_dependencies(const context_dependencies& cd);
132  context_dependencies& operator=(const context_dependencies& cd);
133  };
134 
135 } /* end of namespace getfem. */
136 
137 
138 #endif /* GETFEM_CONTEXT_H__ */
getfem_config.h
defines and typedefs for namespace getfem
getfem
GEneric Tool for Finite Element Methods.
Definition: getfem_accumulated_distro.h:46
getfem_omp.h
Tools for multithreaded, OpenMP and Boost based parallelization.
getfem::context_dependencies
Deal with interdependencies of objects.
Definition: getfem_context.h:81
getfem::context_dependencies::context_check
bool context_check() const
return true if update_from_context was called
Definition: getfem_context.h:126