OSVR-Core
RunLoopManager.h
Go to the documentation of this file.
1 
25 // Copyright Iowa State University 2013.
26 // Distributed under the Boost Software License, Version 1.0.
27 // (See accompanying file LICENSE_1_0.txt or copy at
28 // http://www.boost.org/LICENSE_1_0.txt)
29 
30 #ifndef INCLUDED_RunLoopManager_h_GUID_cca4d4ff_064a_48bb_44db_b8414fb8d202
31 #define INCLUDED_RunLoopManager_h_GUID_cca4d4ff_064a_48bb_44db_b8414fb8d202
32 
33 // Internal Includes
34 // - none
35 
36 // Library/third-party includes
37 #include <boost/noncopyable.hpp>
38 
39 // Standard includes
40 // - none
41 
42 namespace util {
44  public:
45  virtual void signalStart() = 0;
46  virtual void signalAndWaitForStart() = 0;
47  };
48 
49  class LoopInterface {
50  public:
51  virtual void reportStarting() = 0;
52  virtual void reportRunning() = 0;
53  virtual bool shouldContinue() = 0;
54  virtual void reportStopped() = 0;
55  };
56 
58  public:
59  virtual void signalShutdown() = 0;
60  virtual void signalAndWaitForShutdown() = 0;
61  };
62 
66  public LoopInterface,
67  public ShutdownInterface,
68  boost::noncopyable {
69  public:
70  enum RunningState { STATE_STOPPED, STATE_STARTING, STATE_RUNNING };
71  RunLoopManagerBase() : shouldStop_(false) {}
72 
76  virtual void signalStart() = 0;
79  virtual void signalAndWaitForStart() = 0;
81 
85  virtual void signalShutdown() = 0;
88  virtual void signalAndWaitForShutdown() = 0;
90 
96  void reportStarting();
98  void reportRunning();
99  bool shouldContinue();
100  void reportStopped();
102 
103  protected:
106  virtual void reportStateChange_(RunningState s) = 0;
107 
109  void setShouldStop_(bool value);
110 
111  private:
113  volatile bool shouldStop_;
114  };
115 
117  class LoopGuard : boost::noncopyable {
118  public:
119  enum StartTime { REPORT_START_IMMEDIATELY, DELAY_REPORTING_START };
120  LoopGuard(LoopInterface &mgr, StartTime t = REPORT_START_IMMEDIATELY);
121  ~LoopGuard();
122 
123  private:
124  LoopInterface &mgr_;
125  };
126 
127  inline void RunLoopManagerBase::reportStarting() {
128  reportStateChange_(STATE_STARTING);
129  }
130 
132  reportStateChange_(STATE_RUNNING);
133  }
134 
135  inline bool RunLoopManagerBase::shouldContinue() {
136  return !shouldStop_;
137  }
138 
139  inline void RunLoopManagerBase::reportStopped() {
140  reportStateChange_(STATE_STOPPED);
141  }
142 
143  inline void RunLoopManagerBase::setShouldStop_(bool value) {
144  shouldStop_ = value;
145  }
146 
147  inline LoopGuard::LoopGuard(LoopInterface &mgr, LoopGuard::StartTime t)
148  : mgr_(mgr) {
149  mgr_.reportStarting();
150  if (t == REPORT_START_IMMEDIATELY) {
151  mgr_.reportRunning();
152  }
153  }
154 
155  inline LoopGuard::~LoopGuard() {
156  mgr_.reportStopped();
157  }
158 
159 } // end of namespace util
160 
161 #endif // INCLUDED_RunLoopManager_h_GUID_cca4d4ff_064a_48bb_44db_b8414fb8d202
Definition: RunLoopManager.h:42
Definition: RunLoopManager.h:57
Base class for implementations of a RunLoopManager that use various synchronization libraries...
Definition: RunLoopManager.h:65
void reportRunning()
Notify the run loop manager that the run loop is now running.
Definition: RunLoopManager.h:131
RAII class to signal loop start and end.
Definition: RunLoopManager.h:117
Definition: RunLoopManager.h:49
Definition: RunLoopManager.h:43
void setShouldStop_(bool value)
internal utility function.
Definition: RunLoopManager.h:143