OSVR-Core
SharedMemoryObjectWithMutex.h
Go to the documentation of this file.
1 
11 // Copyright 2015 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_SharedMemoryObjectWithMutex_h_GUID_8A1DE87D_04F9_4689_6CAC_6D8579B0464D
26 #define INCLUDED_SharedMemoryObjectWithMutex_h_GUID_8A1DE87D_04F9_4689_6CAC_6D8579B0464D
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <boost/interprocess/interprocess_fwd.hpp>
33 #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
34 #include <boost/interprocess/sync/sharable_lock.hpp>
35 #include <boost/interprocess/sync/scoped_lock.hpp>
36 
37 // Standard includes
38 // - none
39 
40 namespace osvr {
41 namespace common {
42  namespace ipc {
43 
44  namespace bip = boost::interprocess;
45 
47  typedef bip::interprocess_upgradable_mutex mutex_type;
48  typedef bip::scoped_lock<mutex_type> exclusive_lock_type;
49  typedef bip::sharable_lock<mutex_type> sharable_lock_type;
50 
52  public:
53  sharable_lock_type getSharableLock() {
54  return sharable_lock_type(getMutex());
55  }
56  exclusive_lock_type getExclusiveLock() {
57  return exclusive_lock_type(getMutex());
58  }
59  mutex_type &getMutex() { return m_mutex; }
60 
62  void verifyWriterLock(exclusive_lock_type &lock) const {
63  if (!lock || (lock.mutex() != &m_mutex)) {
64  throw std::logic_error("Lock passed must be "
65  "exclusively locked and for the "
66  "correct mutex!");
67  }
68  }
71  template <typename LockType>
72  void verifyReaderLock(LockType &lock) const {
73  if (!lock || (lock.mutex() != &m_mutex)) {
74  throw std::logic_error(
75  "Lock passed must be "
76  "at least sharably locked and for the "
77  "correct mutex!");
78  }
79  }
80 
81  private:
82  mutex_type m_mutex;
83  };
84  } // namespace ipc
85 
86 } // namespace common
87 } // namespace osvr
88 
89 #endif // INCLUDED_SharedMemoryObjectWithMutex_h_GUID_8A1DE87D_04F9_4689_6CAC_6D8579B0464D
void verifyWriterLock(exclusive_lock_type &lock) const
Checks to make sure we have a writer (exclusive) lock.
Definition: SharedMemoryObjectWithMutex.h:62
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
void verifyReaderLock(LockType &lock) const
Checks to make sure we at least have a reader (sharable) lock.
Definition: SharedMemoryObjectWithMutex.h:72
bip::interprocess_upgradable_mutex mutex_type
Using it as a shared mutex - but that wasn&#39;t added until boost 1.52.
Definition: SharedMemoryObjectWithMutex.h:47
Definition: SharedMemoryObjectWithMutex.h:51