MADARA  3.4.1
LegacyBarrier.cpp
Go to the documentation of this file.
1 
2 #include <sstream>
3 
4 #include "LegacyBarrier.h"
7 
9  const KnowledgeUpdateSettings& settings)
10  : BaseContainer("", settings), context_(0), id_(0), participants_(1)
11 {
12  init_noharm();
13 }
14 
17  : BaseContainer(name, settings),
18  context_(&(knowledge.get_context())),
19  id_(0),
20  participants_(1)
21 {
22  init_noharm();
23  build_var();
25 }
26 
29  : BaseContainer(name, settings),
30  context_(knowledge.get_context()),
31  id_(0),
32  participants_(1)
33 {
34  init_noharm();
35  build_var();
37 }
38 
40  KnowledgeBase& knowledge, int id, int participants,
41  const KnowledgeUpdateSettings& settings)
42  : BaseContainer(name, settings),
43  context_(&(knowledge.get_context())),
44  id_(id),
45  participants_(participants)
46 {
47  init_noharm();
48  build_var();
50 }
51 
53  Variables& knowledge, int id, int participants,
54  const KnowledgeUpdateSettings& settings)
55  : BaseContainer(name, settings),
56  context_(knowledge.get_context()),
57  id_(id),
58  participants_(participants)
59 {
60  init_noharm();
61  build_var();
63 }
64 
66  : BaseContainer(rhs),
67  context_(rhs.context_),
68  variable_(rhs.variable_),
69  id_(rhs.id_),
70  participants_(rhs.participants_),
71 #ifndef _MADARA_NO_KARL_
72  aggregate_barrier_(rhs.aggregate_barrier_),
73 #endif
74  variable_name_(rhs.variable_name_)
75 {
76 }
77 
79 
81 {
82  if (this != &rhs)
83  {
84  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
85 
86  this->context_ = rhs.context_;
87  this->name_ = rhs.name_;
88  this->id_ = rhs.id_;
89  this->participants_ = rhs.participants_;
90  this->settings_ = rhs.settings_;
91  this->variable_ = rhs.variable_;
92 #ifndef _MADARA_NO_KARL_
93  this->aggregate_barrier_ = rhs.aggregate_barrier_;
94 #endif
95  this->variable_name_ = rhs.variable_name_;
96  }
97 }
98 
100 {
101  if (context_ && name_ != "")
102  {
103  ContextGuard context_guard(*context_);
104  MADARA_GUARD_TYPE guard(mutex_);
105 
106  std::stringstream buffer;
107  if (participants_ > 0)
108  {
109  // barrier logic is that everyone else is at least to your barrier
110  buffer << name_;
111  buffer << ".0 >= ";
112  buffer << name_;
113  buffer << ".";
114  buffer << id_;
115 
116  // add all other barrierer variables
117  for (size_t i = 1; i < participants_; ++i)
118  {
119  buffer << " && ";
120  buffer << name_;
121  buffer << ".";
122  buffer << i;
123  buffer << " >= ";
124  buffer << name_;
125  buffer << ".";
126  buffer << id_;
127  }
128  }
129 
130 #ifndef _MADARA_NO_KARL_
131  aggregate_barrier_ = context_->compile(buffer.str());
132 #endif
133 
135  "LegacyBarrier::build_aggregate_barrier: building barrier string of %s\n",
136  buffer.str().c_str());
137  }
138  else if (name_ == "")
139  {
140  context_->print("ERROR: containers::LegacyBarrier needs a name.\n", 0);
141  }
142  else if (!context_)
143  {
145  logger::LOG_ERROR, "ERROR: containers::LegacyBarrier needs a context.\n");
146  }
147 }
148 
150 {
151  if (context_ && name_ != "")
152  {
153  ContextGuard context_guard(*context_);
154  MADARA_GUARD_TYPE guard(mutex_);
155 
156  std::stringstream buffer;
157 
158  buffer << name_;
159  buffer << ".";
160  buffer << id_;
161 
162  variable_name_ = buffer.str();
163 
165  "LegacyBarrier::build_var: settings variable reference to %s\n",
166  variable_name_.c_str());
167 
168  variable_ = context_->get_ref(buffer.str(), no_harm);
169  }
170  else if (name_ == "")
171  {
172  context_->print("ERROR: Container::LegacyBarrier needs a name.\n", 0);
173  }
174  else if (!context_)
175  {
177  logger::LOG_ERROR, "ERROR: containers::LegacyBarrier needs a context.\n");
178  }
179 }
180 
182 {
183  no_harm.always_overwrite = false;
184  no_harm.delay_sending_modifieds = true;
185  no_harm.expand_variables = false;
186  no_harm.signal_changes = false;
187  no_harm.track_local_changes = false;
188  no_harm.treat_globals_as_locals = true;
189 }
190 
192 {
193  MADARA_GUARD_TYPE guard(mutex_);
194  return id_;
195 }
196 
198 {
199  MADARA_GUARD_TYPE guard(mutex_);
200  return participants_;
201 }
202 
204  const std::string& var_name, KnowledgeBase& knowledge, int id,
205  int participants)
206 {
207  KnowledgeUpdateSettings keep_local(true);
208  context_ = &(knowledge.get_context());
209 
210  ContextGuard context_guard(*context_);
211  MADARA_GUARD_TYPE guard(mutex_);
212 
213  name_ = var_name;
214  id_ = id;
215  participants_ = participants;
216 
217  this->build_var();
218  this->build_aggregate_barrier();
219 }
220 
222  const std::string& var_name, Variables& knowledge, int id, int participants)
223 {
224  KnowledgeUpdateSettings keep_local(true);
225  context_ = knowledge.get_context();
226 
227  ContextGuard context_guard(*context_);
228  MADARA_GUARD_TYPE guard(mutex_);
229 
230  name_ = var_name;
231  id_ = id;
232  participants_ = participants;
233 
234  this->build_var();
235  this->build_aggregate_barrier();
236 }
237 
239  const std::string& var_name, ThreadSafeContext& knowledge, int id,
240  int participants)
241 {
242  KnowledgeUpdateSettings keep_local(true);
243  context_ = &knowledge;
244 
245  ContextGuard context_guard(*context_);
246  MADARA_GUARD_TYPE guard(mutex_);
247 
248  name_ = var_name;
249  id_ = id;
250  participants_ = participants;
251 
252  this->build_var();
253  this->build_aggregate_barrier();
254 }
255 
257  size_t id, size_t participants)
258 {
259  if (context_)
260  {
261  ContextGuard context_guard(*context_);
262  MADARA_GUARD_TYPE guard(mutex_);
263 
264  id_ = id;
265  participants_ = participants;
266 
267  this->build_var();
268  this->build_aggregate_barrier();
269  }
270 }
271 
274 {
275  if (context_)
276  {
277  ContextGuard context_guard(*context_);
278  MADARA_GUARD_TYPE guard(mutex_);
279  context_->set(variable_, value, settings_);
280  }
281 
282  return value;
283 }
284 
287 {
289 
290  if (context_)
291  {
292  ContextGuard context_guard(*context_);
293  MADARA_GUARD_TYPE guard(mutex_);
294  result = context_->get(variable_, no_harm);
295  }
296 
297  return result;
298 }
299 
302 {
304 
305  if (context_)
306  {
307  ContextGuard context_guard(*context_);
308  MADARA_GUARD_TYPE guard(mutex_);
309  result = context_->get(variable_, no_harm).to_integer();
310  }
311 
312  return result;
313 }
314 
316 {
317  if (context_)
318  {
319  ContextGuard context_guard(*context_);
320  MADARA_GUARD_TYPE guard(mutex_);
321  context_->inc(variable_, settings_);
322  }
323 }
324 
326 {
327  bool result = false;
328 
329  if (context_ && name_ != "")
330  {
331  ContextGuard context_guard(*context_);
332  MADARA_GUARD_TYPE guard(mutex_);
333 
335  "LegacyBarrier::is_done: checking barrier result for done\n");
336 
337  result = barrier_result() == 1;
338 
339  if (!result)
340  {
342  "LegacyBarrier::is_true: barrier is not true, remarking barrier "
343  "variable\n");
344 
345  context_->mark_modified(variable_);
346 
348  }
349  else
350  {
352  "LegacyBarrier::is_true: barrier is true\n");
353  }
354  }
355 
356  return result;
357 }
358 
360 {
361  if (context_ && name_ != "")
362  {
363  context_->set(variable_, value, settings_);
364  }
365 }
366 
368 {
369  if (context_ && name_ != "")
370  {
371  context_->mark_modified(variable_);
372  }
373 }
374 
376 {
377  std::stringstream result;
378 
379  result << "LegacyBarrier: ";
380 
381  if (context_)
382  {
383  ContextGuard context_guard(*context_);
384  MADARA_GUARD_TYPE guard(mutex_);
385 
386  result << this->name_;
387  result << " = " << context_->get(variable_).to_string() << "\n";
388 
389  std::string prefix = name_ + ".";
390 
391  // add all other barrierer variables
392  for (size_t i = 0; i < participants_; ++i)
393  {
394  std::stringstream temp_buffer;
395  temp_buffer << prefix;
396  temp_buffer << i;
397 
398  std::string this_var = temp_buffer.str();
399 
400  result << " " << this_var;
401  result << "={";
402  result << this_var;
403  result << "}\n";
404  }
405  }
406 
407  return result.str();
408 }
409 
411 {
412  modify();
413 }
414 
416 {
417  return get_debug_info();
418 }
419 
422 {
423  return new LegacyBarrier(*this);
424 }
425 
427 {
428  double result(0.0);
429 
430  if (context_)
431  {
432  ContextGuard context_guard(*context_);
433  MADARA_GUARD_TYPE guard(mutex_);
434  result = context_->get(variable_, no_harm).to_double();
435  }
436 
437  return result;
438 }
439 
441 {
442  std::string result;
443 
444  if (context_)
445  {
446  ContextGuard context_guard(*context_);
447  MADARA_GUARD_TYPE guard(mutex_);
448  result = context_->get(variable_, no_harm).to_string();
449  }
450 
451  return result;
452 }
453 
455  uint32_t quality, const KnowledgeReferenceSettings& settings)
456 {
457  if (context_)
458  {
459  ContextGuard context_guard(*context_);
460  MADARA_GUARD_TYPE guard(mutex_);
461  context_->set_quality(name_, quality, true, settings);
462  }
463 }
464 
466 {
467  bool result(false);
468 
470  "LegacyBarrier::is_true: checking barrier result for truth\n");
471 
472  if (context_)
473  {
474  ContextGuard context_guard(*context_);
475  MADARA_GUARD_TYPE guard(mutex_);
476  result = barrier_result() == 1;
477  }
478 
480  "LegacyBarrier::is_true: final result is %d\n", (int)result);
481 
482  return result;
483 }
484 
486 {
487  return !is_true();
488 }
489 
491 {
492  return is_true();
493 }
494 
496 {
497  return is_false();
498 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:24
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
This class encapsulates an entry in a KnowledgeBase.
Settings for applying knowledge updates.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class is an abstract base class for all containers.
Definition: BaseContainer.h:34
KnowledgeUpdateSettings settings_
Settings for modifications.
std::string name_
Prefix of variable.
MADARA_LOCK_TYPE mutex_
guard for access and changes
This class stores an integer within a variable context.
Definition: LegacyBarrier.h:32
size_t get_participants(void) const
Returns the number of participants in the barrier ring.
ThreadSafeContext * context_
Variable context that we are modifying.
knowledge::KnowledgeRecord to_record(void) const
Returns the barrier round number as a knowledge::KnowledgeRecord.
void resize(size_t id=0, size_t participants=1)
Resizes the barrier, usually when number of participants change.
bool is_false(void) const
Determines if the barrier is false.
double to_double(void) const
Returns the barrier round number as a double.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
void build_aggregate_barrier(void)
Builds the aggregate barrier logic.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int id, int participants)
Sets the variable name that this refers to.
VariableReference variable_
Variable reference.
void build_var(void)
Builds the variable that is actually incremented.
size_t id_
id of this barrier in the barrier ring
virtual BaseContainer * clone(void) const
Clones this container.
bool is_true(void) const
Determines if the barrier is true.
void operator=(const LegacyBarrier &rhs)
Assignment operator.
size_t participants_
the number of participants in the barrier ring
LegacyBarrier(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
void modify(void)
Mark the value as modified.
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the barrier variables.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true.
std::string variable_name_
Holder for variable name to quickly refresh modified status.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: LegacyBarrier.h:35
void init_noharm(void)
Initialize the no harm eval settings.
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the barrier round number as an integer (same as *)
void next(void)
Goes to the next barrier round.
CompiledExpression aggregate_barrier_
Expression for aggregating barrier in one atomic operation.
void set(type value)
Sets the barrier to a specific round.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
size_t get_id(void) const
Returns the id of the barrier in the barrier ring.
std::string to_string(void) const
Returns the barrier round number as a string.
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
bool is_done(void)
Wait for all other participants to reach your barrier round.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger