OSVR-Core
RunLoopManagerStd.h
Go to the documentation of this file.
1 
21 // Copyright Sensics, Inc. 2015.
22 // Distributed under the Boost Software License, Version 1.0.
23 // (See accompanying file LICENSE_1_0.txt or copy at
24 // http://www.boost.org/LICENSE_1_0.txt)
25 
26 #ifndef INCLUDED_RunLoopManagerStd_h_GUID_34945132_5355_45A8_A7D6_073C7C8C235A
27 #define INCLUDED_RunLoopManagerStd_h_GUID_34945132_5355_45A8_A7D6_073C7C8C235A
28 
29 
30 // Internal Includes
31 #include "RunLoopManager.h"
32 
33 // Library/third-party includes
34 // - none
35 
36 // Standard includes
37 #include <thread>
38 #include <mutex>
39 #include <condition_variable>
40 
41 namespace util {
42 
44  public:
45  RunLoopManagerStd() : currentState_(STATE_STOPPED) {}
46 
49  void signalStart();
50  void signalAndWaitForStart();
52 
55  void signalShutdown();
58 
59  private:
60  void reportStateChange_(RunningState s);
61  std::mutex mut_;
62  std::condition_variable stateCond_;
63 
64  typedef std::unique_lock<std::mutex> Lock;
65 
67  volatile RunningState currentState_;
68  };
69 
70  inline void RunLoopManagerStd::signalStart() {
71  Lock condGuard(mut_);
72  setShouldStop_(false);
73  }
74 
76  signalStart();
77  {
78  Lock condGuard(mut_);
79  while (currentState_ != STATE_RUNNING) {
80  stateCond_.wait(condGuard);
81  }
82  }
83  }
84 
85  inline void RunLoopManagerStd::signalShutdown() {
86  Lock condGuard(mut_);
87  setShouldStop_(true);
88  }
89 
91  Lock condGuard(mut_);
92  setShouldStop_(true);
93 
94  while (currentState_ != STATE_STOPPED) {
95  stateCond_.wait(condGuard);
96  }
97  }
98 
99  inline void
100  RunLoopManagerStd::reportStateChange_(RunLoopManagerBase::RunningState s) {
101  {
102  Lock condGuard(mut_);
103  currentState_ = s;
104  }
105  stateCond_.notify_all();
106  }
107 
108 } // end of namespace util
109 
110 #endif // INCLUDED_RunLoopManagerStd_h_GUID_34945132_5355_45A8_A7D6_073C7C8C235A
111 
void signalAndWaitForStart()
Set up for run loop to start, then block until we have confirmation that the run loop is running...
Definition: RunLoopManagerStd.h:75
Definition: RunLoopManagerStd.h:43
Definition: RunLoopManager.h:42
Base class for implementations of a RunLoopManager that use various synchronization libraries...
Definition: RunLoopManager.h:65
void signalAndWaitForShutdown()
Send a message to the run loop that it should stop, and wait until it does.
Definition: RunLoopManagerStd.h:90
void setShouldStop_(bool value)
internal utility function.
Definition: RunLoopManager.h:143