AERA
r_exec/object.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef r_exec_object_h
86 #define r_exec_object_h
87 
88 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
89 #include "../r_code/object.h"
90 #include "view.h"
91 #include "opcodes.h"
92 
93 #include <list>
94 
95 
96 namespace r_exec {
97 
98 static inline bool IsNotification(r_code::Code *object) {
99 
100  switch (object->code(0).getDescriptor()) {
101  case r_code::Atom::MARKER:
102  return object->code(0).asOpcode() == Opcodes::MkActChg ||
103  object->code(0).asOpcode() == Opcodes::MkHighAct ||
104  object->code(0).asOpcode() == Opcodes::MkHighSln ||
105  object->code(0).asOpcode() == Opcodes::MkLowAct ||
106  object->code(0).asOpcode() == Opcodes::MkLowRes ||
107  object->code(0).asOpcode() == Opcodes::MkLowSln ||
108  object->code(0).asOpcode() == Opcodes::MkNew ||
109  object->code(0).asOpcode() == Opcodes::MkRdx ||
110  object->code(0).asOpcode() == Opcodes::MkSlnChg;
111  default:
112  return false;
113  }
114 }
115 
116 // Shared resources:
117 // views: accessed by Mem::injectNow (via various sub calls) and Mem::update.
118 // psln_thr: accessed by reduction cores (via overlay mod/set).
119 // marker_set: accessed by Mem::injectNow ans Mem::_initiate_sln_propagation.
120 template<class C, class U> class Object :
121  public C {
122 private:
123  size_t hash_value_;
124 
125  volatile uint32 invalidated_; // must be aligned on 32 bits.
126 
127  CriticalSection psln_thrCS_;
128  CriticalSection viewsCS_;
129  CriticalSection markersCS_;
130 protected:
131  Object();
132  Object(r_code::Mem *mem);
133 public:
134  virtual ~Object(); // un-registers from the rMem's object_register.
135 
136  r_code::_View *build_view(r_code::SysView *source) override {
137 
138  return r_code::Code::build_view<r_exec::View>(source);
139  }
140 
141  bool is_invalidated() override;
142  bool invalidate() override; // return false when was not invalidated, true otherwise.
143 
144  void compute_hash_value();
145 
146  float32 get_psln_thr() override;
147 
148  void acq_views() override { viewsCS_.enter(); }
149  void rel_views() override { viewsCS_.leave(); }
150  void acq_markers() override { markersCS_.enter(); }
151  void rel_markers() override { markersCS_.leave(); }
152 
153  // Target psln_thr only.
154  void set(uint16 member_index, float32 value) override;
155  void mod(uint16 member_index, float32 value) override;
156 
157  r_code::_View *get_view(r_code::Code *group, bool lock) override; // returns the found view if any, NULL otherwise.
158 
159  class Hash {
160  public:
161  size_t operator ()(U *o) const {
162 
163  if (o->hash_value_ == 0)
164  o->compute_hash_value();
165  return o->hash_value_;
166  }
167  };
168 
169  class Equal {
170  public:
171  bool operator ()(const U *lhs, const U *rhs) const { // lhs and rhs have the same hash value_, i.e. same opcode, same code size and same reference size.
172 
173  if (lhs->code(0).asOpcode() == Opcodes::Ent || rhs->code(0).asOpcode() == Opcodes::Ent)
174  return lhs == rhs;
175 
176  uint16 i;
177  for (i = 0; i < lhs->references_size(); ++i)
178  if (lhs->get_reference(i) != rhs->get_reference(i))
179  return false;
180  for (i = 0; i < lhs->code_size(); ++i) {
181 
182  if (lhs->code(i) != rhs->code(i))
183  return false;
184  }
185  return true;
186  }
187  };
188 };
189 
190 // Local object.
191 // Used for r-code that does not travel across networks (groups and notifications) or when the rMem is not distributed.
192 // Markers are killed when at least one of their references dies (held by their views).
193 // Marker deletion is performed by registering pending delete operations in the groups they are projected onto.
194 class r_exec_dll LObject :
195  public Object<r_code::LocalObject, LObject> {
196 public:
197  static bool RequiresPacking() { return false; }
198  static LObject *Pack(r_code::Code *object, r_code::Mem* /* mem */) { return (LObject*)object; } // object is always a LObject (local operation).
201 
202  load(source);
203  }
204  virtual ~LObject() {}
205 };
206 }
207 
208 
209 #endif
r_exec::Object::Equal
Definition: r_exec/object.h:169
r_code::_View
Definition: r_code/object.h:164
r_code::SysView
Definition: r_code/object.h:119
r_code::SysObject
Definition: r_code/object.h:136
r_exec::LObject
Definition: r_exec/object.h:195
r_code::Code
Definition: r_code/object.h:224
core::CriticalSection
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:233
r_code::Mem
Definition: r_code/object.h:420
r_exec::Object::Hash
Definition: r_exec/object.h:159
r_exec::Object
Definition: r_exec/object.h:121