MADARA  3.1.8
ThreadSafeContext.inl
Go to the documentation of this file.
1 #ifndef _MADARA_THREADSAFECONTEXT_INL_
2 #define _MADARA_THREADSAFECONTEXT_INL_
3 
12 
13 #include <sstream>
14 
15 
16 inline int
18  const std::string & key,
19  const std::string & filename,
20  const KnowledgeUpdateSettings & settings)
21 {
22  VariableReference variable = get_ref (key, settings);
23  return read_file (variable, filename, settings);
24 }
25 
28  const std::string & key,
29  const KnowledgeReferenceSettings & settings) const
30 {
31  const KnowledgeRecord *ret = with (key, settings);
32  if (ret) {
33  return *ret;
34  }
35  return KnowledgeRecord();
36 }
37 
38 // return the value of a variable
41  const VariableReference & variable,
42  const KnowledgeReferenceSettings & settings) const
43 {
44  const KnowledgeRecord *ret = with (variable, settings);
45  if (ret) {
46  return *ret;
47  }
48  return KnowledgeRecord();
49 }
50 
53  const std::string & key,
54  const KnowledgeReferenceSettings & settings)
55 {
56  KnowledgeMap::iterator found;
57 
58  MADARA_GUARD_TYPE guard (mutex_);
59 
60  if (settings.expand_variables)
61  {
62  std::string cur_key = expand_statement (key);
63  found = map_.find (cur_key);
64  }
65  else
66  {
67  found = map_.find (key);
68  }
69 
70  if (found != map_.end ())
71  {
72  return &found->second;
73  }
74 
75  return nullptr;
76 }
77 
78 // return the value of a variable
81  const VariableReference & variable,
83 {
84  MADARA_GUARD_TYPE guard (mutex_);
85 
86  if (variable.record_)
87  return variable.record_;
88  else
89  return nullptr;
90 }
91 
94  const std::string & key,
95  const KnowledgeReferenceSettings & settings) const
96 {
97  KnowledgeMap::const_iterator found;
98 
99  MADARA_GUARD_TYPE guard (mutex_);
100 
101  if (settings.expand_variables)
102  {
103  std::string cur_key = expand_statement (key);
104  found = map_.find (cur_key);
105  }
106  else
107  {
108  found = map_.find (key);
109  }
110 
111  if (found != map_.end ())
112  {
113  return &found->second;
114  }
115 
116  return nullptr;
117 }
118 
119 // return the value of a variable
122  const VariableReference & variable,
123  const KnowledgeReferenceSettings &) const
124 {
125  MADARA_GUARD_TYPE guard (mutex_);
126 
127  if (variable.record_)
128  return variable.record_;
129  else
130  return nullptr;
131 }
132 
133 // return the value of a variable
134 inline bool
136  const VariableReference & variable,
137  const KnowledgeReferenceSettings &) const
138 {
139  MADARA_GUARD_TYPE guard (mutex_);
140 
141  if (variable.record_)
142  return variable.record_->exists ();
143  else
144  return false;
145 }
146 
147 // return the value of a variable
150  const VariableReference & variable,
151  size_t index,
153 {
154  MADARA_GUARD_TYPE guard (mutex_);
155 
156  if (variable.record_)
157  return variable.record_->retrieve_index (index);
158  else
159  return knowledge::KnowledgeRecord ();
160 }
161 
164  const std::string & key,
165  size_t index,
166  const KnowledgeReferenceSettings & settings)
167 {
168  VariableReference variable = get_ref (key, settings);
169  return retrieve_index (variable, index, settings);
170 }
171 
172 template<typename T>
173 inline int
175  const std::string & key,
176  T && value,
177  const KnowledgeUpdateSettings & settings)
178 {
179  VariableReference variable = get_ref (key, settings);
180  return set (variable, std::forward<T>(value), settings);
181 }
182 
183 template<typename T>
184 inline int
186  const VariableReference & variable,
187  T && value,
188  const KnowledgeUpdateSettings & settings)
189 {
190  MADARA_GUARD_TYPE guard (mutex_);
191  if (variable.record_)
192  return set_unsafe(variable, std::forward<T>(value), settings);
193  else
194  return -1;
195 }
196 
197 template<typename T>
198 inline int
200  const T * value,
201  uint32_t size,
202  const KnowledgeUpdateSettings & settings)
203 {
204  VariableReference variable = get_ref (key, settings);
205  return set (variable, value, size, settings);
206 }
207 
208 // set the value of a variable
209 template<typename T>
210 inline int
212  const VariableReference & variable,
213  const T * value,
214  uint32_t size,
215  const KnowledgeUpdateSettings & settings)
216 {
217  MADARA_GUARD_TYPE guard (mutex_);
218  if (variable.record_)
219  {
220  return set_unsafe_impl(variable, settings, value, size);
221  }
222  else
223  return -1;
224 
225  return 0;
226 }
227 
228 // set the value of a variable
229 template<typename... Args>
230 inline int
232  const VariableReference & variable,
233  const KnowledgeUpdateSettings & settings,
234  Args&&... args)
235 {
236  // check if we have the appropriate write quality
237  if (!settings.always_overwrite &&
238  variable.record_->write_quality < variable.record_->quality)
239  return -2;
240  else
241  variable.record_->quality = 0;
242 
243  variable.record_->set_value(std::forward<Args>(args)...);
244  variable.record_->quality = variable.record_->write_quality;
245 
246  return 0;
247 }
248 
249 template<typename T>
250 // set the value of a variable
251 inline int
253  const VariableReference & variable,
254  T && value,
255  const KnowledgeUpdateSettings & settings)
256 {
257  int ret = set_unsafe_impl(variable, settings, std::forward<T>(value));
258 
259  if (ret == 0)
260  mark_and_signal (variable.name_.get_ptr (), variable.record_, settings);
261 
262  return ret;
263 }
264 
265 template<typename T>
266 // set the value of a variable
267 inline int
269  const VariableReference & variable,
270  const T * value,
271  size_t size,
272  const KnowledgeUpdateSettings & settings)
273 {
274  int ret = set_unsafe_impl(variable, settings, value, size);
275 
276  if (ret == 0)
277  mark_and_signal (variable.name_.get_ptr (), variable.record_, settings);
278 
279  return ret;
280 }
281 
282 inline int
284  const std::string & key,
285  const char * value, size_t size,
286  const KnowledgeUpdateSettings & settings)
287 {
288  VariableReference variable = get_ref (key, settings);
289  return set_xml (variable, value, size, settings);
290 }
291 
292 inline int
294  const std::string & key,
295  const char * value, size_t size,
296  const KnowledgeUpdateSettings & settings)
297 {
298  VariableReference variable = get_ref (key, settings);
299  return set_text (variable, value, size, settings);
300 }
301 
302 inline int
304  const std::string & key,
305  const unsigned char * value, size_t size,
306  const KnowledgeUpdateSettings & settings)
307 {
308  VariableReference variable = get_ref (key, settings);
309  return set_jpeg (variable, value, size, settings);
310 }
311 
312 inline int
314  const std::string & key,
315  const unsigned char * value, size_t size,
316  const KnowledgeUpdateSettings & settings)
317 {
318  VariableReference variable = get_ref (key, settings);
319  return set_file (variable, value, size, settings);
320 }
321 
322 template<typename T>
323 inline int
325  const std::string & key,
326  size_t index, T&& value,
327  const KnowledgeUpdateSettings & settings)
328 {
329  VariableReference variable = get_ref (key, settings);
330  return set_index (variable, index, std::forward<T>(value), settings);
331 }
332 
333 template<typename T>
334 inline int
336  const VariableReference & variable, size_t index,
337  T&& value,
338  const KnowledgeUpdateSettings & settings)
339 {
340  MADARA_GUARD_TYPE guard (mutex_);
341  if (variable.record_)
342  return set_index_unsafe(variable, index, std::forward<T>(value), settings);
343  else
344  return -1;
345 }
346 
347 template<typename T>
348 inline int
350  const VariableReference & variable, size_t index,
351  T&& value, const KnowledgeUpdateSettings & settings)
352 {
353  // check if we have the appropriate write quality
354  if (!settings.always_overwrite &&
355  variable.record_->write_quality < variable.record_->quality)
356  return -2;
357  else
358  variable.record_->quality = 0;
359 
360  variable.record_->set_index (index, std::forward<T>(value));
361  variable.record_->quality = variable.record_->write_quality;
362 
363  return 0;
364 }
365 
366 template<typename T>
367 inline int
369  const VariableReference & variable, size_t index,
370  T&& value, const KnowledgeUpdateSettings & settings)
371 {
372  int ret = set_index_unsafe_impl(variable, index,
373  std::forward<T>(value), settings);
374 
375  if (ret == 0)
376  mark_and_signal (variable.name_.get_ptr (), variable.record_, settings);
377 
378  return ret;
379 }
380 
383  const std::string & key,
384  const KnowledgeUpdateSettings & settings)
385 {
386  VariableReference variable = get_ref (key, settings);
387  return inc (variable, settings);
388 }
389 
392  const std::string & key,
393  const KnowledgeUpdateSettings & settings)
394 {
395  VariableReference variable = get_ref (key, settings);
396  return dec (variable, settings);
397 }
398 
401  const VariableReference & variable,
402  const KnowledgeUpdateSettings & settings)
403 {
404  MADARA_GUARD_TYPE guard (mutex_);
405  if (variable.record_)
406  {
407  // check if we have the appropriate write quality
408  if (settings.always_overwrite ||
409  variable.record_->write_quality >= variable.record_->quality)
410  {
411  ++ (*variable.record_);
412 variable.record_->quality = variable.record_->write_quality;
413 
414 mark_and_signal (variable.name_.get_ptr (), variable.record_, settings);
415  }
416 
417  return *variable.record_;
418  }
419 
420  return knowledge::KnowledgeRecord ();
421 }
422 
423 #ifndef _MADARA_NO_KARL_
424 
425 // return whether or not the key exists
426 inline bool
428 const std::string & expression)
429 {
430  MADARA_GUARD_TYPE guard (mutex_);
431 
432  return interpreter_->delete_expression (expression);
433 }
434 
435 #endif // _MADARA_NO_KARL_
436 
437 inline bool
439 const std::string & key,
440 const KnowledgeReferenceSettings & settings)
441 {
442  // enter the mutex
443  bool found (false);
444  std::string key_actual;
445  const std::string * key_ptr;
446  MADARA_GUARD_TYPE guard (mutex_);
447 
448  if (settings.expand_variables)
449  {
450  key_actual = expand_statement (key);
451  key_ptr = &key_actual;
452  }
453  else
454  key_ptr = &key;
455 
456  // find the key and update found with result of find
457  KnowledgeMap::iterator record = map_.find (*key_ptr);
458  found = record != map_.end ();
459 
460  if (found)
461  {
462  record->second.clear_value ();
463  }
464 
465  return found;
466 }
467 
468 // return whether or not the key exists
469 inline bool
471 const std::string & key,
472 const KnowledgeReferenceSettings & settings)
473 {
474  // enter the mutex
475  std::string key_actual;
476  bool result (false);
477 
478  const std::string * key_ptr;
479  MADARA_GUARD_TYPE guard (mutex_);
480 
481  if (settings.expand_variables)
482  {
483  key_actual = expand_statement (key);
484  key_ptr = &key_actual;
485  }
486  else
487  key_ptr = &key;
488 
489  // erase the map
490  result = map_.erase (*key_ptr) == 1;
491 
492  // erase any changed or local changed map entries
493  changed_map_.erase (*key_ptr);
494  local_changed_map_.erase (*key_ptr);
495 
496  return result;
497 }
498 
499 // return whether or not the key exists
500 inline bool
502  const std::string & key,
503  const KnowledgeReferenceSettings & settings) const
504 {
505  // enter the mutex
506  std::string key_actual;
507  const std::string * key_ptr;
508  MADARA_GUARD_TYPE guard (mutex_);
509 
510  if (settings.expand_variables)
511  {
512  key_actual = expand_statement (key);
513  key_ptr = &key_actual;
514  }
515  else
516  key_ptr = &key;
517 
518  // if key is not null
519  if (*key_ptr != "")
520  {
521  // find the key in the knowledge base
522  KnowledgeMap::const_iterator found = map_.find (*key_ptr);
523 
524  // if it's found, then return the value
525  if (found != map_.end ())
526  return found->second.status () != knowledge::KnowledgeRecord::UNCREATED;
527  }
528 
529  // if no match, return empty (0)
530  return false;
531 }
532 
533 // Atomically decrement a stored value. Only reason we are inlining this function
534 // is because it is called by only one function, and we can save a bit of
535 // execution time via expansion into that function call.
538  const VariableReference & variable,
539  const KnowledgeUpdateSettings & settings)
540 {
541  MADARA_GUARD_TYPE guard (mutex_);
542  if (variable.record_)
543  {
544  // check if we have the appropriate write quality
545  if (settings.always_overwrite ||
546  variable.record_->write_quality >= variable.record_->quality)
547  {
548  -- (*variable.record_);
549  variable.record_->quality = variable.record_->write_quality;
550 
551  mark_and_signal (variable.name_.get_ptr (), variable.record_, settings);
552  }
553 
554  return *variable.record_;
555  }
556 
557  return knowledge::KnowledgeRecord ();
558 }
559 
562 inline uint64_t
564  uint64_t clock)
565 {
566  MADARA_GUARD_TYPE guard (mutex_);
567 
568  // clock_ is always increasing. We never reset it to a lower clock value
569  // user can check return value to see if the clock was set.
570  if (clock_ < clock)
571  clock_ = clock;
572 
573  return clock_;
574 }
575 
578 inline uint64_t
580  const std::string & key, uint64_t clock,
581  const KnowledgeReferenceSettings & settings)
582 {
583  // enter the mutex
584  std::string key_actual;
585  const std::string * key_ptr;
586  MADARA_GUARD_TYPE guard (mutex_);
587 
588  if (settings.expand_variables)
589  {
590  key_actual = expand_statement (key);
591  key_ptr = &key_actual;
592  }
593  else
594  key_ptr = &key;
595 
596  // check for null key
597  if (*key_ptr == "")
598  return 0;
599 
600  // create the key if it didn't exist
601  knowledge::KnowledgeRecord & record = map_[*key_ptr];
602 
603  // check for value already set
604  if (record.clock < clock)
605  {
606  record.clock = clock;
607 
608  // try to update the global clock as well
609  this->set_clock (clock);
610  }
611 
612  return record.clock;
613 }
614 
617 inline uint64_t
619  const std::string & key,
620  const KnowledgeUpdateSettings & settings)
621 {
622  // enter the mutex
623  std::string key_actual;
624  const std::string * key_ptr;
625  MADARA_GUARD_TYPE guard (mutex_);
626 
627  if (settings.expand_variables)
628  {
629  key_actual = expand_statement (key);
630  key_ptr = &key_actual;
631  }
632  else
633  key_ptr = &key;
634 
635  // check for null key
636  if (*key_ptr == "")
637  return 0;
638 
639  // create the key if it didn't exist
640  knowledge::KnowledgeRecord & record = map_[*key_ptr];
641 
642  return record.clock += settings.clock_increment;
643 }
644 
646 inline uint64_t
648  const KnowledgeUpdateSettings & settings)
649 {
650  MADARA_GUARD_TYPE guard (mutex_);
651  return clock_ += settings.clock_increment;
652 }
653 
656 inline uint64_t
658 {
659  MADARA_GUARD_TYPE guard (mutex_);
660  return clock_;
661 }
662 
663 inline madara::logger::Logger &
665 {
666  MADARA_GUARD_TYPE guard (mutex_);
667  return *logger_;
668 }
669 
670 inline void
672  logger::Logger & logger) const
673 {
674  MADARA_GUARD_TYPE guard (mutex_);
675  logger_ = &logger;
676 }
677 
679 inline uint64_t
681  const std::string & key,
682  const KnowledgeReferenceSettings & settings) const
683 {
684  // enter the mutex
685  std::string key_actual;
686  const std::string * key_ptr;
687  MADARA_GUARD_TYPE guard (mutex_);
688 
689  if (settings.expand_variables)
690  {
691  key_actual = expand_statement (key);
692  key_ptr = &key_actual;
693  }
694  else
695  key_ptr = &key;
696 
697  // check for null key
698  if (*key_ptr == "")
699  return 0;
700 
701  // find the key in the knowledge base
702  KnowledgeMap::const_iterator found = map_.find (*key_ptr);
703 
704  // if it's found, then compare the value
705  if (found != map_.end ())
706  {
707  return found->second.clock;
708  }
709  else
710  // key does not exist
711  return 0;
712 }
713 
716 inline void
718 {
719  mutex_.MADARA_LOCK_LOCK ();
720 }
721 
723 inline void
725 {
726  mutex_.MADARA_LOCK_UNLOCK ();
727 }
728 
731 inline void
733  const std::string & statement, unsigned int level) const
734 {
735  madara_logger_ptr_log (logger_, (int)level,
736  this->expand_statement (statement).c_str ());
737 }
738 
739 // clear all variables and their values
740 inline void
742 {
743  // enter the mutex
744  MADARA_GUARD_TYPE guard (mutex_);
745 
746  if (erase)
747  {
748  map_.clear ();
749  }
750 
751  else
752  {
753  for (madara::knowledge::KnowledgeMap::iterator i = map_.begin ();
754  i != map_.end (); ++i)
755  {
756  i->second.reset_value ();
757  }
758  }
759 
760  changed_map_.clear ();
761  changed_.MADARA_CONDITION_NOTIFY_ONE ();
762 }
763 
764 
767 inline void
769  bool extra_release)
770 {
771  // enter the mutex
772  MADARA_GUARD_TYPE guard (mutex_);
773 
774  // if the caller is relying on a recursive call (e.g. KnowlegeBase::wait),
775  // we'll need to call an extra release for this to work. Otherwise, the
776  // context would remain locked to the calling thread - even though it will
777  // now be put to sleep
778  if (extra_release)
779  mutex_.MADARA_LOCK_UNLOCK ();
780 
781  changed_.wait (mutex_);
782 
783  //if (extra_release)
784  // mutex_.MADARA_LOCK_LOCK ();
785 }
786 
787 inline void
789  const std::string & key,
790  const KnowledgeUpdateSettings & settings
791  )
792 {
793  VariableReference ref = get_ref (key, settings);
794  if (ref.is_valid ())
795  mark_to_send (ref, settings);
796 }
797 
798 inline void
800  const VariableReference & ref,
801  const KnowledgeUpdateSettings & settings
802  )
803 {
804  MADARA_GUARD_TYPE guard (mutex_);
805  mark_to_send_unsafe (std::string (ref.get_name ()), *ref.record_, settings);
806 }
807 
808 inline void
810  const std::string & key, madara::knowledge::KnowledgeRecord & record,
812  )
813 {
814  changed_map_[key] = &record;
815 
817  record.set_modified ();
818 }
819 
820 inline void
822  const std::string & key,
823  const KnowledgeUpdateSettings & settings
824  )
825 {
826  VariableReference ref = get_ref (key, settings);
827  if (ref.is_valid ())
828  mark_to_checkpoint (ref, settings);
829 }
830 
831 inline void
833  const VariableReference & ref,
834  const KnowledgeUpdateSettings & settings
835  )
836 {
837  MADARA_GUARD_TYPE guard (mutex_);
839  *ref.record_, settings);
840 }
841 
842 inline void
844  const std::string & key, madara::knowledge::KnowledgeRecord & record,
846  )
847 {
848  local_changed_map_[key] = &record;
849 
851  record.set_modified ();
852 }
853 
854 inline void
856  const char * name, knowledge::KnowledgeRecord * record,
857  const KnowledgeUpdateSettings & settings)
858 {
859  // otherwise set the value
860  if (name[0] != '.' || settings.treat_locals_as_globals)
861  {
862  if (!settings.treat_globals_as_locals)
863  {
864  mark_to_send_unsafe (name, *record);
865  }
866  }
867 
868  // track local changes now checkpoints all state, not just local
869  if (settings.track_local_changes)
870  {
871  mark_to_checkpoint_unsafe (name, *record);
872  }
873 
874  if (settings.signal_changes)
875  changed_.MADARA_CONDITION_NOTIFY_ALL ();
876 }
877 
878 inline void
880  const std::string & key,
881  const KnowledgeUpdateSettings & settings
882  )
883 {
884  VariableReference ref = get_ref (key, settings);
885  if (ref.is_valid ())
886  mark_modified (ref, settings);
887 }
888 
889 inline void
891  const VariableReference & ref,
892  const KnowledgeUpdateSettings & settings
893  )
894 {
895  MADARA_GUARD_TYPE guard (mutex_);
896  mark_and_signal (ref.get_name (), ref.record_, settings);
897 }
898 
899 inline std::string
901 {
902  MADARA_GUARD_TYPE guard (mutex_);
903  std::stringstream result;
904 
905  result << changed_map_.size () << " modifications ready to send:\n";
906 
907  for (KnowledgeRecords::const_iterator i = changed_map_.begin ();
908  i != changed_map_.end (); ++i)
909  {
910  if (i->second->is_binary_file_type ())
911  {
912  result << "File: ";
913  }
914  else if (i->second->type () == knowledge::KnowledgeRecord::DOUBLE)
915  {
916  result << "Double: ";
917  }
918  else if (i->second->type () == knowledge::KnowledgeRecord::DOUBLE_ARRAY)
919  {
920  result << "Double array: ";
921  }
922  else if (i->second->type () == knowledge::KnowledgeRecord::INTEGER)
923  {
924  result << "Integer: ";
925  }
926  else if (i->second->type () == knowledge::KnowledgeRecord::INTEGER_ARRAY)
927  {
928  result << "Integer array: ";
929  }
930  else if (i->second->is_string_type ())
931  {
932  result << "String: ";
933  }
934  else
935  {
936  result << "Unknown: ";
937  }
938 
939  result << i->first << " = " << i->second->to_string () << "\n";
940  }
941 
942  return result.str ();
943 }
944 
945 
949 {
950  MADARA_GUARD_TYPE guard (mutex_);
951 
952  return changed_map_;
953 }
954 
957 {
958  MADARA_GUARD_TYPE guard (mutex_);
959 
960  VariableReferences snapshot;
961  snapshot.resize (changed_map_.size ());
962  int cur = 0;
963 
965  "ThreadSafeContext::save_modifieds:" \
966  " changed_map.size=%d, snapshot.size=%d\n",
967  (int)changed_map_.size (), (int)snapshot.size ());
968 
969  for (KnowledgeRecords::const_iterator i = changed_map_.begin ();
970  i != changed_map_.end (); ++i, ++cur)
971  {
973  "ThreadSafeContext::save_modifieds:" \
974  " snapshot[%d].name=%s\n",
975  cur, i->first.c_str ());
976 
977  snapshot[cur].set_name (i->first);
978  snapshot[cur].record_ = i->second;
979  }
980 
981  return snapshot;
982 }
983 
984 inline void
986 const VariableReferences & modifieds) const
987 {
988  MADARA_GUARD_TYPE guard (mutex_);
989 
990  for (VariableReferences::const_iterator i = modifieds.begin ();
991  i != modifieds.end (); ++i)
992  {
993  changed_map_.insert (
994  KnowledgeRecords::value_type (i->name_.get_ptr (), i->record_));
995  }
996 }
997 
1001 {
1002  MADARA_GUARD_TYPE guard (mutex_);
1003 
1004  return local_changed_map_;
1005 }
1006 
1008 inline void
1010 {
1011  MADARA_GUARD_TYPE guard (mutex_);
1012 
1013  changed_map_.clear ();
1014 }
1015 
1016 
1018 inline void
1020 {
1021  MADARA_GUARD_TYPE guard (mutex_);
1022 
1023  // each synchronization counts as an event, since this is a
1024  // pretty important networking event
1025 
1026  //++this->clock_;
1027 
1028  for (madara::knowledge::KnowledgeMap::iterator i = map_.begin ();
1029  i != map_.end ();
1030  ++i)
1031  {
1032  if (i->first.size () > 0 && i->first[0] != '.')
1033  {
1034  // local or global doesn't matter. Clock and modification
1035  // aren't really a part of local variable checking anyway
1036  //i->second.status = madara::knowledge::KnowledgeRecord::MODIFIED;
1037 
1038  if (i->second.status () != knowledge::KnowledgeRecord::UNCREATED)
1039  mark_and_signal (i->first.c_str (), &i->second,
1041  else
1042  i->second.set_value (KnowledgeRecord::Integer (0));
1043 
1044  //i->second.clock = this->clock_;
1045  }
1046  }
1047 }
1048 
1050 inline void
1052  const std::string & variable)
1053 {
1054  MADARA_GUARD_TYPE guard (mutex_);
1055 
1056  changed_map_.erase (variable);
1057 }
1058 
1059 inline void
1061 {
1062  MADARA_GUARD_TYPE guard (mutex_);
1063 
1064  local_changed_map_.clear ();
1065 }
1066 
1067 
1069 inline void
1071 {
1072  if (lock)
1073  {
1074  MADARA_GUARD_TYPE guard (mutex_);
1075  changed_.MADARA_CONDITION_NOTIFY_ONE ();
1076  }
1077  else
1078  changed_.MADARA_CONDITION_NOTIFY_ONE ();
1079 }
1080 
1081 inline void
1083  const std::string & filename)
1084 {
1085  logger_->add_file (filename);
1086 }
1087 
1088 inline int
1090 {
1091  return logger_->get_level ();
1092 }
1093 
1094 inline void
1096 {
1097  logger_->set_level (level);
1098 }
1099 
1100 
1101 
1102 #endif // _MADARA_THREADSAFECONTEXT_INL_
This class encapsulates an entry in a KnowledgeBase.
bool expand_variables
Toggle for always attempting to expand variables (true) or never expanding variables (false) ...
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
madara::knowledge::KnowledgeMap map_
Hash table containing variable names and values.
int set_index_unsafe(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
NON-Atomically sets the value of an array index to a value.
KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a value at a specified index within a knowledge array.
uint64_t set_clock(uint64_t clock)
Atomically sets the lamport clock.
utility::ScopedArray< const char > name_
potential string value of the node (size int)
int set_index_unsafe_impl(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
uint32_t quality
priority of the update
int set_file(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
madara::knowledge::KnowledgeRecord KnowledgeRecord
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to a JPEG image.
const knowledge::KnowledgeRecords & get_modifieds(void) const
Retrieves a list of modified variables.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
void lock(void) const
Locks the mutex on this context.
bool signal_changes
Toggle whether to signal changes have happened.
bool delete_expression(const std::string &expression)
Deletes the expression from the interpreter cache.
int set_unsafe(const VariableReference &variable, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
NON-Atomically sets the value of a variable to the specific value.
void set_level(int level)
Sets the maximum logging detail level.
Definition: Logger.inl:44
void signal(bool lock=true) const
Signals that this thread is done with the context.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:11
void mark_to_checkpoint(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable reference to modified at current clock for the purposes of checkpointing (even if it...
int get_log_level(void)
Gets the log level.
Optimized reference to a variable within the knowledge base.
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
void apply_modified(void)
Changes all global variables to modified at current clock.
int get_level(void)
Gets the maximum logging detail level.
Definition: Logger.inl:68
int status(void) const
returns the status of the record.
const knowledge::KnowledgeRecords & get_local_modified(void) const
Retrieves a list of modified local variables.
int set_xml(const std::string &key, const char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an XML string.
T * get_ptr(void)
get the underlying pointer
Definition: ScopedArray.inl:68
uint64_t clock_increment
Default clock increment.
void add_logger(const std::string &filename)
Adds a file to the logger.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
void unlock(void) const
Unlocks the mutex on this context.
madara::knowledge::KnowledgeRecord * with(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
int set_unsafe_impl(const VariableReference &variable, const KnowledgeUpdateSettings &settings, Args &&...args)
::std::map< std::string, KnowledgeRecord * > KnowledgeRecords
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
knowledge::KnowledgeRecords local_changed_map_
void add_file(const std::string &filename)
Adds a file to the logger.
Definition: Logger.inl:14
void set_index(size_t index, T value)
sets the value at the index to the specified value.
void set_modified(void)
sets the status to modified
static constexpr struct madara::knowledge::tags::string_t string
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
uint64_t inc_clock(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the Lamport clock and returns the new clock time (intended for sending knowledg...
bool always_overwrite
Toggle for always overwriting records, regardless of quality, clock values, etc.
std::vector< VariableReference > VariableReferences
a vector of variable references
int set_text(const std::string &key, const char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an XML string.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
void mark_to_checkpoint_unsafe(const std::string &key, madara::knowledge::KnowledgeRecord &record, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock for the purposes of checkpointing.
knowledge::KnowledgeRecords changed_map_
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
logger::Logger * logger_
Logger for printing.
const char * get_name(void) const
Returns the name of the variable.
uint64_t get_clock(void) const
Atomically gets the Lamport clock.
uint32_t write_quality
write priority for any local updates
void mark_to_send(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable reference to modified at current clock, and queues it to send, even if it is a local...
knowledge::KnowledgeRecord * record_
Reference to knowledge record.
void reset_checkpoint(void) const
Reset all checkpoint variables in the modified lists.
void mark_to_send_unsafe(const std::string &key, madara::knowledge::KnowledgeRecord &record, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock, and queues it to send, even if it is a local that woul...
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
bool treat_globals_as_locals
Toggle whether updates to global variables are treated as local variables and not marked as modified ...
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
bool track_local_changes
Toggle for checkpointing support.
bool delete_expression(const std::string &expression)
Attempts to delete an expression from cache.
Definition: Interpreter.inl:75
Settings for applying knowledge updates.
void mark_and_signal(const char *name, knowledge::KnowledgeRecord *record, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
method for marking a record modified and signaling changes
madara::expression::Interpreter * interpreter_
KaRL interpreter.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
bool treat_locals_as_globals
Toggle whether updates to local variables are treated as global variables that should be sent over th...
uint64_t clock
last modification time
void set_log_level(int level)
Sets the log level.
void reset_modified(void)
Reset all variables to be unmodified.