CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
mythlocked.h
1/*
2 * Copyright (C) 2015 Jean-Luc Barriere
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#ifndef MYTHLOCKED_H
23#define MYTHLOCKED_H
24
25// Compatibility with C++98 remains
26#include <cstddef> // for NULL
27
28namespace Myth
29{
30
31 namespace OS {
32 class Latch;
33 }
34
35 class Lockable
36 {
37 public:
38 Lockable();
39 ~Lockable();
40
44 void Lock();
48 void Unlock();
49
53 void LockShared();
57 void UnlockShared();
58
59#if __cplusplus >= 201103L
60 Lockable(const Lockable&) = delete;
61 Lockable& operator=(const Lockable&) = delete;
62#endif
63
64 class ExclusiveGuard
65 {
66 Lockable& m_lock;
67#if __cplusplus < 201103L
68 ExclusiveGuard(const ExclusiveGuard&);
69 ExclusiveGuard& operator=(const ExclusiveGuard&);
70#endif
71 public:
72 ExclusiveGuard(Lockable& lock) : m_lock(lock) { m_lock.Lock(); }
73 ~ExclusiveGuard() { m_lock.Unlock(); }
74#if __cplusplus >= 201103L
75 ExclusiveGuard(const ExclusiveGuard&) = delete;
76 ExclusiveGuard& operator=(const ExclusiveGuard&) = delete;
77#endif
78 };
79
80 class SharedGuard
81 {
82 Lockable& m_lock;
83#if __cplusplus < 201103L
84 SharedGuard(const SharedGuard&);
85 SharedGuard& operator=(const SharedGuard&);
86#endif
87 public:
88 SharedGuard(Lockable& lock) : m_lock(lock) { m_lock.LockShared(); }
89 ~SharedGuard() { m_lock.UnlockShared(); }
90#if __cplusplus >= 201103L
91 SharedGuard(const SharedGuard&) = delete;
92 SharedGuard& operator=(const SharedGuard&) = delete;
93#endif
94 };
95
96 private:
97 OS::Latch* m_lock;
98
99#if __cplusplus < 201103L
100 Lockable(const Lockable&);
101 Lockable& operator=(const Lockable&);
102#endif
103 };
104
105 template<typename T>
106 class Locked
107 {
108 public:
109 Locked()
110 : m_val(), m_latch() {}
111
112 Locked(const T& val)
113 : m_val(val), m_latch() {}
114
115 ~Locked() {}
116
117 T Load()
118 {
119 Lockable::SharedGuard g(m_latch);
120 return m_val; // return copy
121 }
122
123 const T& Store(const T& newval)
124 {
125 Lockable::ExclusiveGuard g(m_latch);
126 m_val = newval;
127 return newval; // return input
128 }
129
130 class pointer
131 {
132 friend class Locked;
133 public:
134 T& operator* () const { return *m_val; }
135 T *operator->() const { return m_val; }
136
137 pointer() : m_val(NULL), m_x(NULL) { }
138 ~pointer() { if (m_x) m_x->Unlock(); }
139
140 pointer(const pointer& other)
141 : m_val(other.m_val), m_x(other.m_x) { m_x->Lock(); }
142
143 pointer& operator=(const pointer& other)
144 {
145 if (m_x) m_x->Unlock();
146 m_val = other.m_val;
147 m_x = other.m_x;
148 m_x->Lock();
149 return *this;
150 }
151
152#if __cplusplus >= 201103L
153 pointer(pointer&& other) noexcept
154 : m_val(other.m_val), m_x(other.m_x)
155 {
156 other.m_val = NULL;
157 other.m_x = NULL;
158 }
159
160 pointer& operator=(pointer&& other) noexcept
161 {
162 if (m_x) m_x->Unlock();
163 m_val = other.m_val;
164 m_x = other.m_x;
165 other.m_val = NULL;
166 other.m_x = NULL;
167 return *this;
168 }
169#endif
170
171 private:
172 pointer(T* val, Lockable* latch)
173 : m_val(val), m_x(latch) { m_x->Lock(); }
174 T* m_val;
175 Lockable* m_x;
176 };
177
178 pointer GetExclusive()
179 {
180 return pointer(&m_val, &m_latch);
181 }
182
183 class const_pointer
184 {
185 friend class Locked;
186 public:
187 const T& operator* () const { return *m_val; }
188 const T *operator->() const { return m_val; }
189
190 const_pointer() : m_val(NULL), m_s(NULL) { }
191 ~const_pointer() { if (m_s) m_s->UnlockShared(); }
192
193 const_pointer(const const_pointer& other)
194 : m_val(other.m_val), m_s(other.m_s) { m_s->LockShared(); }
195
196 const_pointer& operator=(const const_pointer& other)
197 {
198 if (m_s) m_s->UnlockShared();
199 m_val = other.m_val;
200 m_s = other.m_s;
201 m_s->LockShared();
202 return *this;
203 }
204
205#if __cplusplus >= 201103L
206 const_pointer(const_pointer&& other) noexcept
207 : m_val(other.m_val), m_s(other.m_s)
208 {
209 other.m_val = NULL;
210 other.m_s = NULL;
211 }
212
213 const_pointer& operator=(const_pointer&& other) noexcept
214 {
215 if (m_s) m_s->UnlockShared();
216 m_val = other.m_val;
217 m_s = other.m_s;
218 other.m_val = NULL;
219 other.m_s = NULL;
220 return *this;
221 }
222#endif
223
224 private:
225 const_pointer(const T* val, Lockable* latch)
226 : m_val(val), m_s(latch) { m_s->LockShared(); }
227 const T* m_val;
228 Lockable* m_s;
229 };
230
231 const_pointer GetShared()
232 {
233 return const_pointer(&m_val, &m_latch);
234 }
235
236#if __cplusplus >= 201103L
237 // Prevent copy
238 Locked(const Locked<T>& other) = delete;
239 Locked<T>& operator=(const Locked<T>& other) = delete;
240#endif
241
242 protected:
243 T m_val;
244 Lockable m_latch;
245
246#if __cplusplus < 201103L
247 // Prevent copy
248 Locked(const Locked<T>& other);
249 Locked<T>& operator=(const Locked<T>& other);
250#endif
251 };
252
253}
254
255#endif /* MYTHLOCKED_H */
void UnlockShared()
This is the main namespace that encloses all public classes.
Definition mythcontrol.h:30