1 #ifndef _MADARA_THREADSAFECONTEXT_INL_
2 #define _MADARA_THREADSAFECONTEXT_INL_
24 return read_file(variable, filename, settings);
35 std::stringstream buffer;
36 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
37 buffer << key <<
" is uninitialized";
54 std::stringstream buffer;
55 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
56 buffer << key <<
" is uninitialized";
71 std::stringstream buffer;
72 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
73 buffer << variable.
get_name() <<
" is uninitialized";
90 std::stringstream buffer;
91 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
92 buffer << variable.
get_name() <<
" is uninitialized";
107 std::stringstream buffer;
108 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
109 buffer << key <<
" is uninitialized";
127 std::stringstream buffer;
128 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
129 buffer << variable.
get_name() <<
" is uninitialized";
141 KnowledgeMap::iterator found;
143 MADARA_GUARD_TYPE guard(
mutex_);
148 found =
map_.find(cur_key);
152 found =
map_.find(key);
155 if (found !=
map_.end())
157 return &found->second;
168 MADARA_GUARD_TYPE guard(
mutex_);
174 std::stringstream buffer;
175 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
176 buffer << variable.
get_name() <<
" is uninitialized";
186 KnowledgeMap::const_iterator found;
188 MADARA_GUARD_TYPE guard(
mutex_);
193 found =
map_.find(cur_key);
197 found =
map_.find(key);
200 if (found !=
map_.end())
204 std::stringstream buffer;
205 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
206 buffer << found->first <<
" is uninitialized";
210 return &found->second;
221 MADARA_GUARD_TYPE guard(
mutex_);
227 std::stringstream buffer;
228 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
229 buffer << variable.
get_name() <<
" is uninitialized";
241 MADARA_GUARD_TYPE guard(
mutex_);
247 std::stringstream buffer;
248 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
249 buffer << variable.
get_name() <<
" is uninitialized";
253 return ret && ret->exists();
261 MADARA_GUARD_TYPE guard(
mutex_);
267 std::stringstream buffer;
268 buffer <<
"ERROR: settings do not allow reads of unset vars and ";
269 buffer << variable.
get_name() <<
" is uninitialized";
274 return record->retrieve_index(index);
292 return set(variable, std::forward<T>(value), settings);
299 MADARA_GUARD_TYPE guard(
mutex_);
302 return set_unsafe(variable, std::forward<T>(value), settings);
312 return set(variable, value, size, settings);
320 MADARA_GUARD_TYPE guard(
mutex_);
329 template<
typename... Args>
341 record->set_value(std::forward<Args>(args)...);
342 record->quality = record->write_quality;
378 return set_xml(variable, value, size, settings);
385 return set_text(variable, value, size, settings);
389 const unsigned char* value,
size_t size,
393 return set_jpeg(variable, value, size, settings);
397 const unsigned char* value,
size_t size,
401 return set_file(variable, value, size, settings);
409 return set_index(variable, index, std::forward<T>(value), settings);
416 MADARA_GUARD_TYPE guard(
mutex_);
435 record->set_index(index, std::forward<T>(value));
436 record->quality = record->write_quality;
461 return inc(variable, settings);
468 return dec(variable, settings);
474 MADARA_GUARD_TYPE guard(
mutex_);
482 record->
quality = record->write_quality;
494 #ifndef _MADARA_NO_KARL_
499 MADARA_GUARD_TYPE guard(
mutex_);
513 MADARA_GUARD_TYPE guard(
mutex_);
518 key_ptr = &key_actual;
524 KnowledgeMap::iterator record =
map_.find(*key_ptr);
525 found = record !=
map_.end();
529 record->second.clear_value();
539 MADARA_GUARD_TYPE guard(
mutex_);
545 variable.
entry_->second.clear_value();
564 MADARA_GUARD_TYPE guard(
mutex_);
569 key_ptr = &key_actual;
579 result =
map_.erase(*key_ptr) == 1;
589 MADARA_GUARD_TYPE guard(
mutex_);
596 return map_.erase(var.
entry_->first.c_str()) == 1;
602 for (
auto cur = begin; cur != end; ++cur)
607 map_.erase(begin, end);
617 MADARA_GUARD_TYPE guard(
mutex_);
622 key_ptr = &key_actual;
631 KnowledgeMap::const_iterator found =
map_.find(*key_ptr);
634 if (found !=
map_.end())
648 MADARA_GUARD_TYPE guard(
mutex_);
656 record->
quality = record->write_quality;
673 MADARA_GUARD_TYPE guard(
mutex_);
691 MADARA_GUARD_TYPE guard(
mutex_);
696 key_ptr = &key_actual;
709 if (record.
clock < clock)
711 record.
clock = clock;
728 MADARA_GUARD_TYPE guard(
mutex_);
733 key_ptr = &key_actual;
752 MADARA_GUARD_TYPE guard(
mutex_);
760 MADARA_GUARD_TYPE guard(
mutex_);
766 MADARA_GUARD_TYPE guard(
mutex_);
772 MADARA_GUARD_TYPE guard(
mutex_);
783 MADARA_GUARD_TYPE guard(
mutex_);
788 key_ptr = &key_actual;
798 KnowledgeMap::const_iterator found =
map_.find(*key_ptr);
801 if (found !=
map_.end())
803 return found->second.clock;
831 const std::string& statement,
unsigned int level)
const
841 MADARA_GUARD_TYPE guard(
mutex_);
852 for (KnowledgeMap::iterator i =
map_.begin(); i !=
map_.end(); ++i)
854 i->second.reset_value();
858 changed_.MADARA_CONDITION_NOTIFY_ONE();
866 MADARA_GUARD_TYPE guard(
mutex_);
873 mutex_.MADARA_LOCK_UNLOCK();
892 MADARA_GUARD_TYPE guard(
mutex_);
920 MADARA_GUARD_TYPE guard(
mutex_);
958 if (rec_ptr->has_history())
967 changed_.MADARA_CONDITION_NOTIFY_ALL();
981 MADARA_GUARD_TYPE guard(
mutex_);
993 MADARA_GUARD_TYPE guard(
mutex_);
994 std::stringstream result;
996 result <<
changed_map_.size() <<
" modifications ready to send:\n";
1007 result <<
"Double: ";
1011 result <<
"Double array: ";
1015 result <<
"Integer: ";
1019 result <<
"Integer array: ";
1023 result <<
"String: ";
1027 result <<
"Unknown: ";
1030 result << entry.first <<
" = " << record.
to_string() <<
"\n";
1033 return result.str();
1039 MADARA_GUARD_TYPE guard(
mutex_);
1045 const std::map<std::string, bool> & send_list,
bool reset)
1047 MADARA_GUARD_TYPE guard(
mutex_);
1052 if (send_list.size() == 0)
1057 map.emplace_hint (map.end(),
1058 i->first, *i->second.get_record_unsafe());
1072 for (
auto var : send_list)
1079 map.end(), found->first, *found->second.get_record_unsafe());
1089 if (send_list.find (i->first) != send_list.end())
1091 map.emplace_hint (map.end(),
1092 i->first, *i->second.get_record_unsafe());
1108 MADARA_GUARD_TYPE guard(
mutex_);
1115 "ThreadSafeContext::save_modifieds:"
1116 " changed_map.size=%d, snapshot.size=%d\n",
1122 "ThreadSafeContext::save_modifieds:"
1123 " snapshot[%d].name=%s\n",
1126 snapshot.emplace_back(entry.second);
1135 MADARA_GUARD_TYPE guard(
mutex_);
1137 for (
auto& entry : modifieds)
1139 changed_map_.insert(std::make_pair(entry.get_name(), entry));
1147 MADARA_GUARD_TYPE guard(
mutex_);
1155 MADARA_GUARD_TYPE guard(
mutex_);
1163 MADARA_GUARD_TYPE guard(
mutex_);
1170 for (KnowledgeMap::iterator i =
map_.begin(); i !=
map_.end(); ++i)
1171 for (
auto& entry :
map_)
1173 if (entry.first.size() > 0 && entry.first[0] !=
'.')
1196 MADARA_GUARD_TYPE guard(
mutex_);
1203 MADARA_GUARD_TYPE guard(
mutex_);
1213 MADARA_GUARD_TYPE guard(
mutex_);
1214 changed_.MADARA_CONDITION_NOTIFY_ONE();
1217 changed_.MADARA_CONDITION_NOTIFY_ONE();
#define madara_logger_checked_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
madara::knowledge::KnowledgeRecord KnowledgeRecord
An exception for attempting to access an invalid context1.
bool delete_expression(const std::string &expression)
Attempts to delete an expression from cache.
This class encapsulates an entry in a KnowledgeBase.
bool has_history() const
Return true if this record has a circular buffer history.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
uint32_t type(void) const
returns the type of the value
KnowledgeRecord & ref_newest()
uint64_t clock
last modification lamport clock time
size_t get_newest(OutputIterator out, size_t count) const
Copy the count newest stored history entries of this record to the given output iterator,...
int status(void) const
returns the status of the record.
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
bool is_binary_file_type(void) const
returns true if the knowledge record has a binary file type
bool is_string_type(void) const
returns true if the record is a string type (STRING, XML, TEXT_FILE)
void set_modified(void)
sets the status to modified
uint32_t quality
priority of the update
Settings for applying knowledge updates.
bool expand_variables
Toggle for always attempting to expand variables (true) or never expanding variables (false)
bool exception_on_unitialized
throw an exception if reference is on uninitialized variable
Settings for applying knowledge updates.
uint64_t clock_increment
Default clock increment.
bool signal_changes
Toggle whether to signal changes have happened.
bool treat_locals_as_globals
Toggle whether updates to local variables are treated as global variables that should be sent over th...
bool always_overwrite
Toggle for always overwriting records, regardless of quality, clock values, etc.
bool stream_changes
Toggle for streaming support.
bool treat_globals_as_locals
Toggle whether updates to global variables are treated as local variables and not marked as modified ...
bool track_local_changes
Toggle for checkpointing support.
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.
void unlock(void) const
Unlocks the mutex on this context.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
void delete_variables(KnowledgeMap::iterator begin, KnowledgeMap::iterator end, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes variables.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
void add_logger(const std::string &filename)
Adds a file to the logger.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
const VariableReferenceMap & get_modifieds(void) const
Retrieves a list of modified variables.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the current value of a variable.
madara::expression::Interpreter * interpreter_
KaRL interpreter.
logger::Logger * logger_
Logger for printing.
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 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...
void apply_modified(void)
Changes all global variables to modified at current clock.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
std::unique_ptr< BaseStreamer > streamer_
Streaming provider for saving all updates.
void signal(bool lock=true) const
Signals that this thread is done with the context.
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...
const VariableReferenceMap & get_local_modified(void) const
Retrieves a list of modified local variables.
int set_index_unsafe_impl(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
MADARA_CONDITION_TYPE changed_
bool delete_expression(const std::string &expression)
Deletes the expression from the interpreter cache.
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.
madara::knowledge::KnowledgeRecord get_actual(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the underlying value of a variable, including history.
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.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
madara::knowledge::KnowledgeMap map_
Hash table containing variable names and values.
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.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
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...
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.
bool try_lock(void) const
Locks the mutex on this context.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
void mark_to_checkpoint_unsafe(VariableReference ref, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock for the purposes of checkpointing.
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.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
void mark_and_signal(VariableReference ref, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
method for marking a record modified and signaling changes
int set_unsafe_impl(const VariableReference &variable, const KnowledgeUpdateSettings &settings, Args &&... args)
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.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
VariableReferenceMap changed_map_
int get_log_level(void)
Gets the log level.
KnowledgeMap get_modifieds_current(const std::map< std::string, bool > &send_list, bool reset=true)
Retrieves the current modifieds map.
void lock(void) const
Locks the mutex on this context.
uint64_t set_clock(uint64_t clock)
Atomically sets the lamport clock.
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
VariableReferenceMap local_changed_map_
uint64_t inc_clock(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the Lamport clock and returns the new clock time (intended for sending knowledg...
void set_log_level(int level)
Sets the log level.
void mark_to_send_unsafe(VariableReference ref, 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 reset_checkpoint(void) const
Reset all checkpoint variables in the modified lists.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
uint64_t get_clock(void) const
Atomically gets the Lamport clock.
void reset_modified(void)
Reset all variables to be unmodified.
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
madara::knowledge::KnowledgeRecord * with(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Optimized reference to a variable within the knowledge base.
const char * get_name(void) const
Returns the name of the variable.
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
KnowledgeRecord * get_record_unsafe(void) const
Returns a pointer to the variable's KnowledgeRecord Do not use this pointer unless you've locked the ...
A multi-threaded logger for logging to one or more destinations.
int get_level(void)
Gets the maximum logging detail level.
void add_file(const std::string &filename)
Adds a file to the logger.
void set_level(int level)
Sets the maximum logging detail level.
Provides functions and classes for the distributed knowledge base.
std::map< const char *, VariableReference, utility::ComparisonLessThan > VariableReferenceMap
a map of variable references
::std::map< std::string, KnowledgeRecord > KnowledgeMap
std::vector< VariableReference > VariableReferences
a vector of variable references
Provides knowledge logging services to files and terminals.
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
Copyright(c) 2020 Galois.