OSVR-Core
AsyncAccessControl.h
Go to the documentation of this file.
1 
11 // Copyright 2014 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_AsyncAccessControl_h_GUID_4255BCEE_826C_4DB4_9368_9457ADBF9456
26 #define INCLUDED_AsyncAccessControl_h_GUID_4255BCEE_826C_4DB4_9368_9457ADBF9456
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <boost/noncopyable.hpp>
33 #include <boost/thread.hpp>
34 #include <boost/optional/optional.hpp>
35 
36 // Standard includes
37 // - none
38 
39 namespace osvr {
40 namespace connection {
45  class AsyncAccessControl : boost::noncopyable {
46  public:
49 
56  bool mainThreadCTS();
57 
64  bool mainThreadDeny();
65 
69 
70  private:
74  enum MainThreadMessages {
75  MTM_WAIT,
76  MTM_CLEAR_TO_SEND,
77  MTM_DENY_SEND,
78  };
79 
81  typedef boost::recursive_mutex MainMutexType;
82  typedef boost::unique_lock<MainMutexType> MainLockType;
83  MainMutexType m_mut;
84 
96  bool m_handleRTS(MainLockType &lock, MainThreadMessages response,
97  MainThreadMessages postCompletionState = MTM_WAIT);
98 
101  typedef boost::mutex DoneMutexType;
102  typedef boost::unique_lock<DoneMutexType> DoneLockType;
103  DoneMutexType m_mutDone;
104 
105  boost::optional<boost::thread::id> m_currentRequestThread;
106 
109  boost::condition_variable m_condMainThread;
112  boost::condition_variable_any m_condAsyncThread;
113 
115  volatile bool m_rts;
117  volatile bool m_done;
119  volatile MainThreadMessages m_mainMessage;
120 
121  friend class RequestToSend;
122  };
123 
129  class RequestToSend : boost::noncopyable {
130  public:
135 
137  ~RequestToSend();
138 
144  bool request();
145 
150  bool isNested() const { return m_nested; }
151 
152  private:
154  AsyncAccessControl::MainLockType m_lock;
155 
157  AsyncAccessControl::DoneLockType m_lockDone;
158 
160  bool m_calledRequest;
161 
163  bool m_nested;
164 
167  AsyncAccessControl &m_control;
168  volatile bool &m_sharedRts;
169  volatile bool &m_sharedDone;
170  volatile AsyncAccessControl::MainThreadMessages &m_mainMessage;
171 
172  boost::condition_variable &m_condMainThread;
173  boost::condition_variable_any &m_condAsyncThread;
175  };
176 } // namespace connection
177 } // namespace osvr
178 
179 #endif // INCLUDED_AsyncAccessControl_h_GUID_4255BCEE_826C_4DB4_9368_9457ADBF9456
RAII object for use by an async thread to manage a request to send.
Definition: AsyncAccessControl.h:129
bool mainThreadDenyPermanently()
Permanently deny present and future requests.
Definition: AsyncAccessControl.cpp:122
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
bool mainThreadDeny()
Check for waiting async thread, and deny it permission to send if found.
Definition: AsyncAccessControl.cpp:117
Internal class handling the synchronization of an asynchronous thread wishing to communicate that has...
Definition: AsyncAccessControl.h:45
bool isNested() const
Method to find out if this is a nested RTS - primarily for testing.
Definition: AsyncAccessControl.h:150
AsyncAccessControl()
Constructor.
Definition: AsyncAccessControl.cpp:109