replicode
object.h
Go to the documentation of this file.
1 // object.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 r_exec_object_h
32 #define r_exec_object_h
33 
34 #include "r_code/object.h"
35 #include "view.h"
36 #include "opcodes.h"
37 
38 #include <list>
39 
40 
41 namespace r_exec {
42 
43 dll_export bool IsNotification(Code *object);
44 
45 // Shared resources:
46 // views: accessed by Mem::injectNow (via various sub calls) and Mem::update.
47 // psln_thr: accessed by reduction cores (via overlay mod/set).
48 // marker_set: accessed by Mem::injectNow ans Mem::_initiate_sln_propagation.
49 template<class C, class U> class Object:
50  public C {
51 private:
52  size_t hash_value;
53 
54  volatile uint64_t invalidated; // must be aligned on 64 bits.
55 
56  std::mutex m_pslnThrMutex;
57  std::mutex m_viewsMutex;
58  std::mutex m_markersMutex;
59 protected:
60  Object();
61  Object(r_code::Mem *mem);
62 public:
63  virtual ~Object(); // un-registers from the rMem's object_register.
64 
66 
67  return Code::build_view<r_exec::View>(source);
68  }
69 
70  virtual bool is_invalidated();
71  virtual bool invalidate(); // return false when was not invalidated, true otherwise.
72 
73  void compute_hash_value();
74 
75  double get_psln_thr();
76 
77  void acq_views() {
78  m_viewsMutex.lock();
79  }
80  void rel_views() {
81  m_viewsMutex.unlock();
82  }
83  void acq_markers() {
84  m_markersMutex.lock();
85  }
86  void rel_markers() {
87  m_markersMutex.unlock();
88  }
89 
90 // Target psln_thr only.
91  void set(uint16_t member_index, double value);
92  void mod(uint16_t member_index, double value);
93 
94  View *get_view(Code *group, bool lock); // returns the found view if any, NULL otherwise.
95 
96  void kill();
97 
98  class Hash {
99  public:
100  size_t operator()(U *o) const {
101 
102  if (o->hash_value == 0)
103  o->compute_hash_value();
104  return o->hash_value;
105  }
106  };
107 
108  class Equal {
109  public:
110  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.
111 
112  if (lhs->code(0).asOpcode() == Opcodes::Ent || rhs->code(0).asOpcode() == Opcodes::Ent)
113  return lhs == rhs;
114 
115  uint16_t i;
116  for (i = 0; i < lhs->references_size(); ++i)
117  if (lhs->get_reference(i) != rhs->get_reference(i))
118  return false;
119  for (i = 0; i < lhs->code_size(); ++i) {
120 
121  if (lhs->code(i) != rhs->code(i))
122  return false;
123  }
124  return true;
125  }
126  };
127 };
128 
129 // Local object.
130 // Used for r-code that does not travel across networks (groups and notifications) or when the rMem is not distributed.
131 // Markers are killed when at least one of their references dies (held by their views).
132 // Marker deletion is performed by registering pending delete operations in the groups they are projected onto.
134  public Object<r_code::LObject, LObject> {
135 public:
136  static bool RequiresPacking() {
137  return false;
138  }
139  static LObject *Pack(Code *object, r_code::Mem *mem) {
140  return (LObject *)object; // object is always a LObject (local operation).
141  }
142  LObject(r_code::Mem *mem = NULL): Object<r_code::LObject, LObject>(mem) {}
144 
145  load(source);
146  }
147  virtual ~LObject() {}
148 };
149 }
150 
151 
152 #endif
size_t hash_value
Definition: object.h:52
static bool RequiresPacking()
Definition: object.h:136
View * get_view(Code *group, bool lock)
Definition: object.tpl.cpp:114
static LObject * Pack(Code *object, r_code::Mem *mem)
Definition: object.h:139
#define dll_export
Definition: dll.h:44
Definition: object.h:328
void set(uint16_t member_index, double value)
Definition: object.tpl.cpp:105
Definition: object.h:49
Definition: _context.cpp:34
Object()
Definition: object.tpl.cpp:37
Definition: atom.cpp:36
void mod(uint16_t member_index, double value)
Definition: object.tpl.cpp:91
virtual ~LObject()
Definition: object.h:147
r_code::View * build_view(SysView *source)
Definition: object.h:65
static uint16_t Ent
Definition: opcodes.h:47
bool IsNotification(Code *object)
Definition: object.cpp:38
Definition: view.h:47
size_t operator()(U *o) const
Definition: object.h:100
virtual bool invalidate()
Definition: object.tpl.cpp:55
void rel_views()
Definition: object.h:80
LObject(r_code::SysObject *source)
Definition: object.h:143
void acq_markers()
Definition: object.h:83
Definition: object.h:76
void acq_views()
Definition: object.h:77
double get_psln_thr()
Definition: object.tpl.cpp:84
Definition: object.h:100
LObject(r_code::Mem *mem=NULL)
Definition: object.h:142
void rel_markers()
Definition: object.h:86
Definition: object.h:172
virtual ~Object()
Definition: object.tpl.cpp:45
Definition: object.h:133
bool operator()(const U *lhs, const U *rhs) const
Definition: object.h:110
volatile uint64_t invalidated
Definition: object.h:54
Definition: object.h:62
virtual bool is_invalidated()
Definition: object.tpl.cpp:50
void compute_hash_value()
Definition: object.tpl.cpp:77
std::mutex m_viewsMutex
Definition: object.h:57
Definition: object.h:98
std::mutex m_pslnThrMutex
Definition: object.h:56
std::mutex m_markersMutex
Definition: object.h:58
Definition: object.h:108