7 #include "madara_jni.h"
25 : name_(name), thread_(thread), control_(control), data_(data), hertz_(hertz)
29 std::stringstream base_string;
35 base_string << debug_to_kb.
to_string() <<
".";
78 "WorkerThread::~WorkerThread(%s):"
79 " thread wasn't joined before destruction\n",
84 catch (
const std::system_error& e)
87 "WorkerThread::~WorkerThread(%s):"
88 " error trying to detach: %s\n",
89 name_.c_str(), e.what());
112 template<
typename... Args>
114 -> decltype(pthread_setname_np(std::forward<Args>(args)...))
116 return pthread_setname_np(std::forward<Args>(args)...);
133 std::ostringstream os;
134 os << std::this_thread::get_id() <<
" spawned " <<
me_.get_id()
138 "WorkerThread::WorkerThread(%s):"
139 " thread started %s\n",
140 name_.c_str(), os.str().c_str());
142 catch (
const std::exception& e)
145 "WorkerThread::WorkerThread(%s):"
146 " failed to create thread: %s\n",
147 name_.c_str(), e.what());
155 "WorkerThread(%s)::svc:"
156 " checking thread existence\n",
169 madara::logger::Logger::set_thread_name(
name_);
180 int64_t min_duration = -1;
181 int64_t max_duration = 0;
182 int64_t last_duration = 0;
183 bool max_duration_changed =
true;
184 bool min_duration_changed =
true;
186 bool one_shot =
true;
187 bool blaster =
false;
197 hertz_, current, frequency, next_epoch, one_shot, blaster);
199 madara::logger::Logger::set_thread_hertz(
hertz_);
210 "WorkerThread(%s)::svc:"
211 " thread checking for pause\n",
219 "WorkerThread(%s)::svc:"
220 " thread calling run function\n",
225 int64_t start_time = 0, end_time = 0;
241 last_duration = end_time - start_time;
242 if (min_duration == -1 || last_duration < min_duration)
244 min_duration = last_duration;
245 min_duration_changed =
true;
247 if (last_duration > max_duration)
249 max_duration = last_duration;
250 max_duration_changed =
true;
261 if (max_duration_changed)
265 if (min_duration_changed)
272 catch (
const std::exception& e)
276 "WorkerThread(%s)::svc:"
277 " exception thrown: %s\n",
278 name_.c_str(), e.what());
291 *
new_hertz_, current, frequency, next_epoch, one_shot, blaster);
299 "WorkerThread(%s)::svc:"
300 " thread checking for next hertz epoch\n",
303 if (current < next_epoch)
307 "WorkerThread(%s)::svc:"
308 " thread past epoch\n",
311 next_epoch += frequency;
316 "WorkerThread(%s)::svc:"
317 " thread has been terminated\n",
322 "WorkerThread(%s)::svc:"
323 " calling thread cleanup method\n",
329 "WorkerThread(%s)::svc:"
330 " deleting thread\n",
334 "WorkerThread(%s)::svc:"
335 " setting finished to 1\n",
343 "WorkerThread(%s)::svc:"
344 " thread creation failed\n",
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
A thread-safe guard for a context or knowledge base.
This class provides a distributed knowledge base to users.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
int set(const VariableReference &variable, const std::string &value, const EvalSettings &settings=EvalSettings(true, false, true, false, false))
Atomically sets the value of a variable to a string.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically gets the current value of a variable (without any history).
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
bool is_false(void) const
Checks to see if the record is false.
Optimized reference to a variable within the knowledge base.
std::string get_name(void) const
Returns the name of the container.
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
bool is_false(void) const
Determines if the value is zero.
bool is_true(void) const
Determines if the value is true.
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Abstract base class for implementing threads.
std::string name
The unique name of your thread.
virtual void init_control_vars(knowledge::KnowledgeBase &control)
Initializes the thread implementation's control plane variables.
knowledge::containers::Double new_hertz_
thread safe hertz reference
knowledge::containers::Integer end_time_
timestamp of the last svc end
knowledge::containers::Integer max_duration_
maximum duration of all runs
void run(void)
Starts the thread, with entry point svc()
WorkerThread()
Default constructor.
knowledge::containers::Integer start_time_
timestamp of the initial svc start
knowledge::containers::Integer executions_
thread safe start flag that will be sent to the knowledge base on launch of the thread
knowledge::containers::Integer finished_
thread safe finished flag that will be sent to the knowledge base on completion of the thread
knowledge::KnowledgeBase data_
the data plane (the knowledge base)
knowledge::KnowledgeBase control_
the control plane to the knowledge base
knowledge::containers::Integer debug_
flag for whether or not to save debug information in control
knowledge::containers::Integer started_
thread safe start flag that will be sent to the knowledge base on launch of the thread
knowledge::containers::Integer last_duration_
duration of last run
std::unique_ptr< BaseThread > thread_
the contained thread
std::thread me_
Assignment operator.
double hertz_
hertz rate for worker thread executions
madara::knowledge::containers::Integer paused_
thread safe paused flag that will be sent to the knowledge base to indicate thread activities should ...
~WorkerThread() noexcept
Destructor.
std::string name_
the name of the contained thread
madara::knowledge::containers::Integer running_
thread safe paused flag that will be sent to the knowledge base to indicate thread activities should ...
knowledge::containers::Integer min_duration_
minimum duration of all runs
void change_frequency(double hertz, utility::TimeValue ¤t, utility::Duration &frequency, utility::TimeValue &next_epoch, bool &one_shot, bool &blaster)
Changes the frequency given a hertz rate.
knowledge::containers::Integer last_start_time_
timestamp of the last svc start
This class encapsulates attaching and detaching to a VM.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
Provides a quality-of-service-enabled threading library.
auto try_pthread_setname_np(Args &&... args) -> decltype(pthread_setname_np(std::forward< Args >(args)...))
std::chrono::time_point< Clock > TimeValue
time point
std::chrono::nanoseconds Duration
default clock duration
double sleep(double sleep_time)
Sleeps for a certain amount of time.
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
TimeValue get_time_value(void)
Returns a time of day as a chrono time value If simtime feature is enabled, this may be simulation ti...
Copyright(c) 2020 Galois.
static const EvalSettings DELAY_NO_EXPAND
Settings to delay send modifieds and not expand variables.