MADARA  3.1.8
TimedEventThread.cpp
Go to the documentation of this file.
1 #include "TimedEventThread.h"
2 
5 #include "ace/Time_Value.h"
8 
9 #ifdef WIN32
10 
11 #include <process.h>
12 
13 unsigned __stdcall timed_thread_windows_glue (void * param)
14 {
16  static_cast <madara::knowledge::TimedEventThread *> (
17  param);
18 
19  if (caller)
20  return (unsigned) caller->svc ();
21  else
22  return 0;
23 }
24 
25 #endif
26 
27 
28 #include <iostream>
29 #include <algorithm>
30 
32  TimedEventThreadInfo & info)
33  : info_ (info)
34 {
35  int result;
36 
37 //#ifndef WIN32
38  result = this->activate ();
39 /*#else
40  result = 0;
41  _beginthreadex(NULL, 0, timed_thread_windows_glue, (void*)this, 0, 0);
42 
43 #endif*/
44 
45  if (result != -1)
46  {
48  "TimedEventThread::TimedEventThread:" \
49  " thread started (result = %d)\n", result);
50  }
51  else
52  {
54  "TimedEventThread::TimedEventThread:" \
55  " failed to create thread\n");
56  }
57 
58  std::stringstream expression;
59  expression << "thread.";
60  expression << info_.id;
61  expression << ".closed = 1";
62 
63 #ifndef _MADARA_NO_KARL_
64 
65  thread_closed_ = info_.control_plane->compile (expression.str ());
67  "queued > 0 || terminated");
68 
69 #endif // _MADARA_NO_KARL_
70 
71 }
72 
74 {
75 }
76 
77 
78 int
80 {
81  ACE_Time_Value sleep_time;
82  ACE_Time_Value zero_time;
83  TimedEvent cur_event;
84 
85  zero_time.set (0.0);
86 
87  std::stringstream buffer;
88  buffer << "TimedEventThread[";
89  buffer << info_.id;
90  buffer << "]::svc";
91  std::string print_prefix (buffer.str ());
92 
94  "%s: " \
95  "Service thread started.\n",
96  print_prefix.c_str ());
97 
98  while (!info_.executor->is_shutdown ())
99  {
101  "%s: " \
102  "Attempting to remove an event.\n",
103  print_prefix.c_str ());
104 
116  sleep_time = info_.executor->remove (cur_event);
117  Event * valid = cur_event.second;
118 
119  // if we had a timeout, we'll have a non-zero Event *
120  if (valid != 0)
121  {
122  ++valid->executions;
124 #ifndef _MADARA_NO_KARL_
125  valid->knowledge->evaluate (valid->root)
126 #else
128 #endif
129  );
130 
131  if (valid->intended_executions >= 0 &&
132  (size_t)valid->intended_executions == valid->executions)
133  {
135  "%s: " \
136  "Timed Event has expired. Deleting.\n",
137  print_prefix.c_str ());
138 
139  delete valid;
140  }
141  else if (valid->cancel_on_false && result.is_false ())
142  {
144  "%s: " \
145  "Timed Event returned false. User requested a cancel. Deleting.\n",
146  print_prefix.c_str ());
147 
148  delete valid;
149  }
150  else
151  {
153  "%s: " \
154  "Timed Event added back to queue.\n",
155  print_prefix.c_str ());
156 
157  cur_event.first += valid->period;
158  info_.executor->add (cur_event);
159  }
160  }
161  // otherwise, wait on changes to the queue
162  else
163  {
164  if (sleep_time > zero_time)
165  {
167  "%s: " \
168  "Not ready for timeout. Thread sleeping.\n",
169  print_prefix.c_str ());
170 
171  WaitSettings wait_settings;
172  wait_settings.poll_frequency = 0.5;
173  wait_settings.max_wait_time = 5.0;
174 
175 #ifndef _MADARA_NO_KARL_
176 
177  // inform sleeping threads of new queued events
178  info_.control_plane->wait (queued_or_terminated_, wait_settings);
179 #endif // _MADARA_NO_KARL_
180 
181  }
182  }
183  }
184 
185 #ifndef _MADARA_NO_KARL_
186 
188 #endif // _MADARA_NO_KARL_
189 
190 
191  return 0;
192 }
193 
This class encapsulates an entry in a KnowledgeBase.
std::pair< ACE_Time_Value, Event * > TimedEvent
Definition: TimedExecutor.h:69
TimedEventThread(TimedEventThreadInfo &info)
Constructor.
unsigned int id
thread identifier
TimedExecutor * executor
pointer to TimedExecutor caller, which provides thread-safe callbacks for shutdown information ...
KnowledgeBase * knowledge
knowledge base for executing the expression (should be same as compiled)
Definition: TimedExecutor.h:53
MADARA_Export utility::Refcounter< logger::Logger > global_logger
expression::ComponentNode * root
expression to be executed (rooted Tree)
Definition: TimedExecutor.h:46
Expression container for timed event.
Definition: TimedExecutor.h:36
Thread executing timed events.
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.
Definition: Logger.h:32
CompiledExpression queued_or_terminated_
expression for checking if events are queued or terminated
CompiledExpression thread_closed_
expression for letting control plane know the thread is closed
double poll_frequency
Frequency to poll an expression for truth (in seconds)
Definition: WaitSettings.h:108
TimedEventThreadInfo info_
info for service loop
static constexpr struct madara::knowledge::tags::string_t string
KnowledgeBase * control_plane
For barrier and other information.
ACE_Time_Value remove(TimedEvent &cur_event)
Removes a timed event from the queue.
madara::knowledge::KnowledgeRecord evaluate(const std::string &expression, const EvalSettings &settings=EvalSettings())
Evaluates an expression.
void add(const TimedEvent &new_event)
Adds a timed event to the queue.
ACE_Time_Value period
time between executions in seconds
Definition: TimedExecutor.h:42
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
unsigned int executions
executions so far
Definition: TimedExecutor.h:56
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.
int svc(void)
Service loop for the thread.
int intended_executions
intended executions
Definition: TimedExecutor.h:59
bool cancel_on_false
cancel on false return value
Definition: TimedExecutor.h:62