3 #include "ace/Thread.h" 10 return lhs.first > rhs.first;
15 : thread_info_ (0), threads_ (0), num_threads_ (0)
32 MADARA_GUARD_TYPE guard (
mutex_);
44 MADARA_GUARD_TYPE guard (
mutex_);
49 ACE_Time_Value cur_time = ACE_High_Res_Timer::gettimeofday ();
52 timed_event.first = cur_time + new_event.
delay;
53 timed_event.second =
new Event (new_event);
67 ACE_Time_Value cur_time = ACE_High_Res_Timer::gettimeofday ();
69 mutex_.MADARA_LOCK_LOCK ();
75 "events queue size is greater than zero\n");
80 if (cur_time >= cur_event.first)
83 "removing top element from events queue\n");
91 "earliest event is not ready yet\n");
94 cur_time = cur_event.first - cur_time;
101 mutex_.MADARA_LOCK_UNLOCK ();
106 mutex_.MADARA_LOCK_UNLOCK ();
109 "Nothing to do. Thread going to sleep\n");
115 #ifndef _MADARA_NO_KARL_ 118 #endif // _MADARA_NO_KARL_ 121 "Condition has changed. Thread waking up\n");
123 cur_event.second = 0;
195 ACE_Time_Value cur_time = ACE_High_Res_Timer::gettimeofday ();
198 cur_time =
events_.top ().first - cur_time;
207 mutex_.MADARA_LOCK_LOCK ();
213 mutex_.MADARA_LOCK_UNLOCK ();
219 double delay,
double period,
int executions)
225 new_event.
period.set (period);
226 new_event.
delay.set (delay);
228 #ifndef _MADARA_NO_KARL_ 232 #endif // _MADARA_NO_KARL_ 245 "Entering barrier\n");
247 #ifndef _MADARA_NO_KARL_ 251 ".i [0->threads) (thread.{.i}.closed => ++closed) ;>\n" 252 "closed == threads", settings);
254 #endif // _MADARA_NO_KARL_ 257 "TimedExecutor::enter_barrier: Leaving barrier\n");
263 MADARA_GUARD_TYPE guard (
mutex_);
268 delete cur_event.second;
276 MADARA_GUARD_TYPE guard (
mutex_);
void enter_barrier(void)
Enters the barrier.
double max_wait_time
Maximum time to wait for an expression to become true (in seconds)
MADARA_Export Event fill_event(KnowledgeBase &knowledge, const std::string &expression, double delay=0.0, double period=0.0, int executions=-1)
Fills an event struct.
VariableReference queue_size_
Reference to queue size in the control plane.
MADARA_LOCK_TYPE mutex_
Mutex for local changes.
std::pair< ACE_Time_Value, Event * > TimedEvent
bool operator<(const madara::knowledge::TimedEvent &lhs, const madara::knowledge::TimedEvent &rhs)
Comparison for higher event priorities.
unsigned int id
thread identifier
TimedExecutor * executor
pointer to TimedExecutor caller, which provides thread-safe callbacks for shutdown information ...
void unlock(void)
Unlocks the event queue.
knowledge::KnowledgeRecord::Integer num_threads(void)
Returns the number of threads.
KnowledgeBase * knowledge
knowledge base for executing the expression (should be same as compiled)
expression::ComponentNode * get_root(void)
Gets the root of the compiled expression.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
TimedEventThread ** threads_
Timed Event Threads.
expression::ComponentNode * root
expression to be executed (rooted Tree)
knowledge::KnowledgeRecord::Integer num_threads_
Number of thread.
void lock(void)
Locks the context.
MADARA_Export double sleep(double sleep_time)
Sleeps for a certain amount of time.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
Expression container for timed event.
Thread executing timed events.
~TimedExecutor()
Destructor.
A struct that is passed to Timed Event Thread Executors to control job handling, shutdowns, etc.
madara::knowledge::KnowledgeRecord wait(const std::string &expression, const WaitSettings &settings=WaitSettings())
Waits for an expression to be non-zero.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
This class provides a distributed knowledge base to users.
void shutdown(void)
Informs threads of a shutdown and waits for threads to exit.
ACE_Time_Value delay
delay before first execution
TimedExecutor()
Default constructor.
double poll_frequency
Frequency to poll an expression for truth (in seconds)
ACE_Time_Value time_until_next(void)
Returns the time.
PriorityQueue events_
Event queue.
VariableReference terminated_
Reference to terminated condition within control plane.
Integer to_integer(void) const
converts the value to an integer
TimedEventThreadInfo * thread_info_
Vector of thread info.
VariableReference threads_ref_
Reference to the number of threads in the control plane.
void launch_threads(unsigned int threads)
Launches threads.
KnowledgeBase * control_plane
For barrier and other information.
int set(const VariableReference &variable, const std::string &value, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to a string.
Provides functions and classes for the distributed knowledge base.
KnowledgeBase control_plane_
For barrier and other information.
ACE_Time_Value remove(TimedEvent &cur_event)
Removes a timed event from the queue.
void add(const TimedEvent &new_event)
Adds a timed event to the queue.
ACE_Time_Value period
time between executions in seconds
Encapsulates settings for a wait statement.
unsigned int executions
executions so far
void clear_queue(void)
Clears the event queue.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
bool is_shutdown(void)
Checks if the thread pool has been shutdown.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
int intended_executions
intended executions