CPPMyth
Library to interoperate with MythTV server
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 namespace Myth
26 {
30  class LockGuard
31  {
32  public:
33  struct Lockable;
39  LockGuard(Lockable* lock);
40  ~LockGuard();
41 
47  static Lockable* CreateLock();
52  static void DestroyLock(Lockable* lock);
57  static void Lock(Lockable* lock);
62  static void Unlock(Lockable* lock);
67  static void ClearLock(Lockable* lock);
68 
69  private:
70  Lockable* m_lock;
71  };
72 
73  template<typename T>
74  class Locked
75  {
76  public:
77  Locked(const T& val)
78  : m_val(val)
79  , m_lock(LockGuard::CreateLock()) {}
80 
81  ~Locked()
82  {
83  LockGuard::DestroyLock(m_lock);
84  }
85 
86  T Load()
87  {
88  LockGuard g(m_lock);
89  return m_val; // return copy
90  }
91 
92  const T& Store(const T& newval)
93  {
94  LockGuard g(m_lock);
95  m_val = newval;
96  return newval; // return input
97  }
98 
99  class value
100  {
101  public:
102  value(T& val, LockGuard::Lockable*& lock) : m_val(val), m_g(lock) {}
103  T& operator()() const { return m_val; }
104  private:
105  T& m_val;
106  LockGuard m_g;
107  };
108 
109  value Get()
110  {
111  return value(m_val, m_lock);
112  }
113 
114  protected:
115  T m_val;
116  LockGuard::Lockable* m_lock;
117 
118  // Prevent copy
119  Locked(const Locked<T>& other);
120  Locked<T>& operator=(const Locked<T>& other);
121  };
122 
123  template <typename T>
124  class LockedNumber : public Locked<T>
125  {
126  public:
127  LockedNumber(T val)
128  : Locked<T>(val) {}
129 
130  T Add(T amount)
131  {
133  return Locked<T>::m_val += amount;
134  }
135 
136  T operator+=(T amount)
137  {
138  return Add(amount);
139  }
140 
141  T Sub(T amount)
142  {
144  return Locked<T>::m_val -= amount;
145  }
146 
147  T operator-=(T amount)
148  {
149  return Sub(amount);
150  }
151  };
152 
153 }
154 
155 #endif /* MYTHLOCKED_H */
static void Lock(Lockable *lock)
Definition: mythlocked.cpp:56
static void DestroyLock(Lockable *lock)
Definition: mythlocked.cpp:51
static void ClearLock(Lockable *lock)
Definition: mythlocked.cpp:66
static Lockable * CreateLock()
Definition: mythlocked.cpp:46
static void Unlock(Lockable *lock)
Definition: mythlocked.cpp:61
LockGuard(Lockable *lock)
Definition: mythlocked.cpp:35
This is the main namespace that encloses all public classes.
Definition: mythcontrol.h:29