MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThreadPauser.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  ThreadPauser.h Structure allowing a thread to pause another
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef THREAD_PAUSER_H
28 #define THREAD_PAUSER_H
29 
30 #include "Mutex.h"
31 #include "Logger.h"
32 
33 namespace MFM
34 {
38  enum ThreadState
39  {
58  THREADSTATE_PAUSED,
59 
71  THREADSTATE_RUN_REQUESTED,
72 
84  THREADSTATE_RUN_READY,
85 
98  THREADSTATE_RUNNING,
99 
113  THREADSTATE_PAUSE_REQUESTED,
114 
130  THREADSTATE_PAUSE_READY
131 
132  };
133 
141  {
142  private:
143 
147  Mutex m_mutex;
148 
152  ThreadState m_threadState;
153 
157  ThreadState m_threadStatePrevious;
158 
162  bool m_threadStatePreviousOuter;
163 
167  pthread_t m_threadStatePreviousThreadId;
168 
173  bool m_ignoreThreadingProblems;
174 
178  struct StateIsRunRequested : public Mutex::Predicate
179  {
180  ThreadPauser & m_threadPauser;
181  StateIsRunRequested(ThreadPauser & tp) :
182  Predicate(tp.m_mutex),
183  m_threadPauser(tp)
184  { }
185 
186  virtual bool EvaluatePrecondition()
187  {
188  bool ret = m_threadPauser.m_threadState == THREADSTATE_PAUSED;
189  if (!ret)
190  {
191  LOG.Error("PAUSED precondition failed");
192  m_threadPauser.ReportThreadPauserStatus(Logger::ERROR);
193  }
194  return ret;
195  }
196 
197  virtual bool EvaluatePredicate()
198  {
199  return m_threadPauser.m_threadState == THREADSTATE_RUN_REQUESTED;
200  }
201  } m_stateIsRunRequested;
202 
206  struct StateIsRunning : public Mutex::Predicate
207  {
208  ThreadPauser & m_threadPauser;
209  StateIsRunning(ThreadPauser & tp) :
210  Predicate(tp.m_mutex),
211  m_threadPauser(tp)
212  { }
213 
214  virtual bool EvaluatePrecondition()
215  {
216  bool ret = m_threadPauser.m_threadState == THREADSTATE_RUN_READY;
217  if (!ret)
218  {
219  LOG.Error("RUN READY precondition failed");
220  m_threadPauser.ReportThreadPauserStatus(Logger::ERROR);
221  }
222  return ret;
223  }
224 
225  virtual bool EvaluatePredicate()
226  {
227  return m_threadPauser.m_threadState == THREADSTATE_RUNNING;
228  }
229  } m_stateIsRunning;
230 
231  public:
232 
233  ThreadState GetStateBlockingInner()
234  {
235  return GetAdvanceStateInner(false);
236  }
237 
238  ThreadState AdvanceStateInner()
239  {
240  return GetAdvanceStateInner(true);
241  }
242 
243  ThreadState GetAdvanceStateInner(bool innerReadyToAdvance) ;
244 
245  ThreadState GetStateNonblocking() ;
246 
256  ThreadState AdvanceStateOuter(ThreadState fromState) ;
257 
261  ThreadPauser();
262 
266  ~ThreadPauser();
267 
273  {
274  AdvanceStateOuter(THREADSTATE_RUNNING);
275  }
276 
283  void SetIgnoreThreadingProblems(bool value)
284  {
285  m_ignoreThreadingProblems = value;
286  }
287 
292  void RequestRun()
293  {
294  AdvanceStateOuter(THREADSTATE_PAUSED);
295  }
296 
301  void Run()
302  {
303  AdvanceStateOuter(THREADSTATE_RUN_READY);
304  }
305 
310  bool IsRunReady()
311  {
312  return GetStateNonblocking() == THREADSTATE_RUN_READY;
313  }
314 
320  {
321  return GetStateNonblocking() == THREADSTATE_PAUSE_READY;
322  }
323 
329  void Pause()
330  {
331  AdvanceStateOuter(THREADSTATE_PAUSE_READY);
332  }
333 
334  static const char * GetThreadStateName(ThreadState ts) ;
335 
336  void ReportThreadPauserStatus(Logger::Level level) ;
337 
338 #if 0
339 
343  bool IsPauseRequested()
344  {
345  return GetStateNonblocking() == THREADSTATE_PAUSE_REQUESTED;
346  }
347 
348 
353  bool IsPaused();
354 
359  void PauseReady();
360 
366  void WaitWhilePaused();
367 
368 #endif
369  };
370 }
371 
372 #endif /* THREAD_PAUSER_H */
ThreadState AdvanceStateOuter(ThreadState fromState)
Definition: ThreadPauser.cpp:20
Level
Definition: Logger.h:52
~ThreadPauser()
Definition: ThreadPauser.cpp:16
Definition: ThreadPauser.h:140
void Error(const char *format,...)
Definition: Logger.h:259
bool IsPauseReady()
Definition: ThreadPauser.h:319
void Run()
Definition: ThreadPauser.h:301
Definition: Mutex.h:42
ThreadPauser()
Definition: ThreadPauser.cpp:6
void RequestPause()
Definition: ThreadPauser.h:272
Definition: Mutex.h:99
void Pause()
Definition: ThreadPauser.h:329
void SetIgnoreThreadingProblems(bool value)
Definition: ThreadPauser.h:283
bool IsRunReady()
Definition: ThreadPauser.h:310
void RequestRun()
Definition: ThreadPauser.h:292