replicode
overlay.h
Go to the documentation of this file.
1 // overlay.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef overlay_h
32 #define overlay_h
33 
34 #include "r_code/object.h"
35 
36 #include "CoreLibrary/dll.h"
37 #include "CoreLibrary/base.h"
38 
39 #include <mutex>
40 
41 using namespace r_code;
42 
43 namespace r_exec {
44 
45 class View;
46 
47 // Upon invocation of take_input() the overlays older than tsc are killed, assuming stc>0; otherwise, overlays live unitl the ipgm dies.
48 // Controllers are built at loading time and at the view's injection time.
49 // Derived classes must expose a function: void reduce(r_code::View*input); (called by reduction jobs).
51  public _Object {
52 protected:
53  volatile uint64_t invalidated; // 32 bit alignment.
54  volatile uint64_t activated; // 32 bit alignment.
55 
57 
59 
60  std::mutex m_reductionMutex;
61 
62  virtual void take_input(r_exec::View *input) {}
63  template<class C> void __take_input(r_exec::View *input); // utility: to be called by sub-classes.
64 
65  Controller(r_code::View *view);
66 public:
67  virtual ~Controller();
68 
70  return tsc;
71  }
72 
73  virtual void invalidate() {
74  invalidated = 1;
75  }
76  bool is_invalidated() {
77  return invalidated == 1;
78  };
79  void activate(bool a) {
80  activated = (a ? 1 : 0);
81  }
82  bool is_activated() const {
83  return activated == 1;
84  }
85  bool is_alive() const {
86  return invalidated == 0 && activated == 1;
87  }
88 
89  virtual Code *get_core_object() const = 0;
90 
92  return view->object; // return the reduction object (e.g. ipgm, icpp_pgm, cst, mdl).
93  }
94  r_exec::View *getView() const {
95  return (r_exec::View *)view; // return the reduction object's view.
96  }
97 
98  void _take_input(r_exec::View *input); // called by the rMem at update time and at injection time.
99 
100  virtual void gain_activation() {
101  activate(true);
102  }
103  virtual void lose_activation() {
104  activate(false);
105  }
106 
107  void set_view(View *view);
108 
109  void debug(View *input) {}
110 };
111 
112 class _Context;
113 class IPGMContext;
114 class HLPContext;
115 
117  public _Object {
118  friend class _Context;
119  friend class IPGMContext;
120  friend class HLPContext;
121 protected:
123 
125 
126  r_code::vector<Atom> values; // value array: stores the results of computations.
127 // Copy of the pgm/hlp code. Will be patched during matching and evaluation:
128 // any area indexed by a vl_ptr will be overwritten with:
129 // the evaluation result if it fits in a single atom,
130 // a ptr to the value array if the result is larger than a single atom,
131 // a ptr to an input if the result is a pattern input.
133  uint16_t code_size;
134  std::vector<uint16_t> patch_indices; // indices where patches are applied; used for rollbacks.
135  uint16_t value_commit_index; // index of the last computed value+1; used for rollbacks.
136 
137  void load_code();
138  void patch_code(uint16_t index, Atom value);
139  uint16_t get_last_patch_index();
140  void unpatch_code(uint16_t patch_index);
141 
142  void rollback(); // reset the overlay to the last commited state: unpatch code and values.
143  void commit(); // empty the patch_indices and set value_commit_index to values.size().
144 
145  Code *get_core_object() const; // pgm, mdl, cst.
146 
147  Overlay();
148  Overlay(Controller *c, bool load_code = true);
149 public:
150  virtual ~Overlay();
151 
152  virtual void reset(); // reset to original state.
153  virtual Overlay *reduce(r_exec::View *input); // returns an offspring in case of a match.
154 
155  void invalidate() {
156  invalidated = 1;
157  }
158  virtual bool is_invalidated() {
159  return invalidated == 1;
160  }
161 
163  return ((Controller *)controller)->getObject();
164  }
166  return ((Controller *)controller)->getView();
167  }
168 
169  r_code::Code *build_object(Atom head) const;
170 };
171 
173  public Controller {
174 protected:
176 
177  OController(r_code::View *view);
178 public:
179  virtual ~OController();
180 };
181 }
182 
183 #endif
Definition: base.h:75
void invalidate()
Definition: overlay.h:155
virtual void lose_activation()
Definition: overlay.h:103
void debug(View *input)
Definition: overlay.h:109
Definition: _context.h:45
Definition: overlay.h:116
#define dll_export
Definition: dll.h:44
bool is_invalidated()
Definition: overlay.h:76
virtual void take_input(r_exec::View *input)
Definition: overlay.h:62
volatile uint64_t invalidated
Definition: overlay.h:53
r_exec::View * getView() const
Definition: overlay.h:94
Definition: _context.cpp:34
Definition: atom.cpp:36
volatile uint64_t invalidated
Definition: overlay.h:122
Definition: context.h:47
r_code::list< P< Overlay > > overlays
Definition: overlay.h:175
r_code::vector< Atom > values
Definition: overlay.h:126
bool is_alive() const
Definition: overlay.h:85
Atom * code
Definition: overlay.h:132
volatile uint64_t activated
Definition: overlay.h:54
Definition: hlp_context.h:42
Definition: overlay.h:172
bool is_activated() const
Definition: overlay.h:82
Definition: view.h:47
P< Code > object
Definition: object.h:108
virtual void gain_activation()
Definition: overlay.h:100
r_exec::View * getView() const
Definition: overlay.h:165
Definition: atom.h:45
r_code::View * view
Definition: overlay.h:58
virtual void invalidate()
Definition: overlay.h:73
uint64_t tsc
Definition: overlay.h:56
uint16_t code_size
Definition: overlay.h:133
Definition: object.h:100
Definition: list.h:42
Definition: object.h:172
Definition: overlay.h:50
Controller * controller
Definition: overlay.h:124
void reduce(const IPGMContext &context, const IPGMContext &input_set, const IPGMContext &section, std::vector< uint16_t > &input_indices, std::vector< uint16_t > &production_indices)
Definition: context.cpp:523
uint64_t get_tsc()
Definition: overlay.h:69
virtual bool is_invalidated()
Definition: overlay.h:158
r_code::Code * getObject() const
Definition: overlay.h:162
std::mutex m_reductionMutex
Definition: overlay.h:60
uint16_t value_commit_index
Definition: overlay.h:135
std::vector< uint16_t > patch_indices
Definition: overlay.h:134
void activate(bool a)
Definition: overlay.h:79
r_code::Code * getObject() const
Definition: overlay.h:91