Lunchbox  1.17.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
scopedMutex.h
1 
2 /* Copyright (c) 2006-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_SCOPEDMUTEX_H
19 #define LUNCHBOX_SCOPEDMUTEX_H
20 
21 #include <lunchbox/lockable.h> // used in inline method
22 #include <lunchbox/types.h>
23 #include <mutex>
24 
25 namespace lunchbox
26 {
34 template <class L>
36 {
37 public:
38  explicit UniqueSharedLock(L& lock)
39  : _lock(&lock)
40  {
41  lock.lock_shared();
42  }
43 
44  explicit UniqueSharedLock(L* lock)
45  : _lock(lock)
46  {
47  if (lock)
48  lock->lock_shared();
49  }
50 
51  template <typename LB>
52  explicit UniqueSharedLock(const LB& lockable)
53  : UniqueSharedLock(lockable.lock)
54  {
55  }
56 
59  {
60  if (_lock)
61  _lock->unlock_shared();
62  }
63 
64 private:
65  UniqueSharedLock() = delete;
66  UniqueSharedLock(const UniqueSharedLock&) = delete;
68  UniqueSharedLock& operator=(const UniqueSharedLock&) = delete;
69  UniqueSharedLock& operator=(UniqueSharedLock&&) = delete;
70 
71  L* const _lock;
72 };
73 
81 template <class L>
82 class UniqueLock : public std::unique_lock<L>
83 {
84 public:
85  UniqueLock(L* lock_)
86  : std::unique_lock<L>(lock_ ? std::unique_lock<L>(*lock_)
87  : std::unique_lock<L>())
88  {
89  }
90 
91  UniqueLock(L& lock_)
92  : std::unique_lock<L>(lock_)
93  {
94  }
95 
96  template <typename LB>
97  explicit UniqueLock(const LB& lockable)
98  : UniqueLock(lockable.lock)
99  {
100  }
101 
102 private:
103  UniqueLock() = delete;
104  UniqueLock(const UniqueLock&) = delete;
105  UniqueLock& operator=(const UniqueLock&) = delete;
106 };
107 
110 
113 
116 
119 }
120 #endif // LUNCHBOX_SCOPEDMUTEX_H
Basic type definitions not provided by the operating system.
A scoped mutex.
Definition: scopedMutex.h:82
~UniqueSharedLock()
Destruct the scoped mutex and unset the mutex.
Definition: scopedMutex.h:58
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
A scoped shared mutex.
Definition: scopedMutex.h:35